Installation & Setup
1. Add the Dependency
Add flutter_tex to your project using the Flutter CLI:
Or manually add it to your pubspec.yaml:
Then fetch packages:
2. Platform Configuration
flutter_tex is 100% offline, but on mobile and desktop platforms it uses a lightweight local server (http://localhost:<port>) to serve the bundled MathJax engine to an internal headless rendering worker. Modern operating systems require permissions to access localhost or load local HTTP traffic.
Follow the setup for each platform you are targeting:
Android
Open <project-directory>/android/app/src/main/AndroidManifest.xml:
- Allow cleartext HTTP traffic on the
<application>tag (required by Android 9+ to connect to the internallocalhostserver):
- Add internet permission and package queries outside
<application>(for localhost access and opening external links viaurl_launcher):
iOS
Open <project-directory>/ios/Runner/Info.plist and add the following keys inside <dict>:
Web
For Flutter Web, MathJax scripts and styles must be loaded by the browser host page.
Add the following tags inside the <head> block of <project-directory>/web/index.html:
Web Architecture
On Web, flutter_tex communicates directly with MathJax in the browser's JavaScript context via dart:js_interop. There is no WebView overhead on web builds.
macOS
macOS Flutter apps run in an App Sandbox by default. To allow the app to communicate with the local rendering server, open both:
macos/Runner/DebugProfile.entitlementsmacos/Runner/Release.entitlements
And add the client network entitlement:
3. Server Initialization in Dart
Before rendering formulas or widgets, start the rendering server in your main() entrypoint. This spins up the local MathJax worker on mobile/macOS or connects JS interop on Web.
Is TeXRenderingServer.start() safe on Web?
Yes! On Web, TeXRenderingServer.start() simply initializes JS interop listeners. Calling await TeXRenderingServer.start(); works uniformly across Android, iOS, Web, and macOS without conditional platform checks.
4. Verification Checklist
To confirm everything is configured correctly:
-
android:usesCleartextTraffic="true"is present in AndroidManifest.xml. -
NSAllowsArbitraryLoadsis set totruein iOSInfo.plist. -
<script>tags for MathJax are added toweb/index.html(if targeting Web). -
WidgetsFlutterBinding.ensureInitialized()andawait TeXRenderingServer.start()run inmain(). - Test a simple formula with
Math2SVG(math: r"E = mc^2").