Flutter TeX
repository·main·Indexed 43 days ago
https://github.com/shahxad-akram/flutter_texA self-contained Flutter package for robust, fully offline rendering of mathematical and chemical notations using MathJax. It supports LaTeX, TeX, MathML, and AsciiMath through three distinct widgets: Math2SVG for high-performance pure Flutter rendering, TeXWidget for dedicated LaTeX rendering, and TeXView for complex content including HTML, JavaScript, and Markdown. Requires Flutter SDK 3.27.x.
What's inside Flutter TeX
- Flutter TeX is a self-contained Flutter package that uses the MathJax engine to provide robust, fully offline rendering of mathematical and chemical notations. It is designed for in-app scientific and mathematical visualization and supports multiple input formats including LaTeX, TeX, MathML, and AsciiMath. Beyond math, it also supports displaying rich content via complete HTML and JavaScript support.
Use TeXView for complex rich content
mainTeXViewis an advanced widget based onwebview_flutter_plusdesigned for rendering complex mathematical equations and rich dynamic content. It supports Inline HTML, JavaScript, and Markdown.!!! danger "Caution" Avoid using multiple
TeXViewinstances on a single page as it can lead to performance issues due to itswebviewfoundation. For displaying multiple TeX elements, useTeXWidgetorMath2SVGinstead.TeXView( child: TeXViewColumn(children: [ TeXViewInkWell( id: "id_0", child: TeXViewColumn(children: [ TeXViewDocument(r"""<h2>Flutter \( \rm\\TeX \)</h2>""", style: TeXViewStyle(textAlign: TeXViewTextAlign.center)), TeXViewContainer( child: TeXViewImage.network( 'https://raw.githubusercontent.com/Shahxad-Akram/flutter_tex/main/example/assets/flutter_tex_banner.png'), style: TeXViewStyle( margin: TeXViewMargin.all(10), borderRadius: TeXViewBorderRadius.all(20), ), ), TeXViewDocument(r"""<p> When \(a \ne 0 \), there are two solutions to \(ax^2 + bx + c = 0\) and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$</p>""", style: TeXViewStyle.fromCSS( 'padding: 15px; color: white; background: green')) ]), ) ]), style: TeXViewStyle( elevation: 10, borderRadius: TeXViewBorderRadius.all(25), border: TeXViewBorder.all(TeXViewBorderDecoration( borderColor: Colors.blue, borderStyle: TeXViewBorderStyle.solid, borderWidth: 5)), backgroundColor: Colors.white, ), );Choose the right Flutter TeX widget
mainFlutter TeX provides three distinct widgets depending on your performance needs and content complexity:
Math2SVG: A high-performance, pure Flutter-based widget (no WebView). Best for rendering mathematical formulas using LaTeX, MathML, or AsciiMath.TeXWidget: A specialized widget built on top ofMath2SVGspecifically dedicated to LaTeX rendering.TeXView: An advanced, WebView-based widget designed for rendering complex content that includes HTML and JavaScript alongside mathematical notation.
Configure MacOS for flutter_tex
mainBecause macOS apps run in a sandboxed environment, you must explicitly grant network client capabilities. Add the
com.apple.security.network.clientkey to bothmacos/Runner/DebugProfile.entitlementsandmacos/Runner/Release.entitlements.<key>com.apple.security.network.client</key> <true/>Install Flutter TeX
mainTo use Flutter TeX in your project, you must follow the platform-specific installation and setup instructions. Because the package leverages MathJax for offline rendering, setup requirements may vary depending on the target platform (Android, iOS, Web, etc.).
Refer to the official Installation Guide for detailed steps before attempting to run the package.
Configure Web for flutter_tex
mainFor Web support, you must include specific CSS and JavaScript assets in the
<head>section of your<project-directory>/web/index.htmlfile.<head> ... <link rel="stylesheet" href="assets/assets/flutter_tex.css" type="text/css"> <script src="assets/assets/flutter_tex.js" type="text/javascript"></script> <script src="assets/packages/flutter_tex/core/flutter_tex.js"></script> <script src="assets/packages/flutter_tex/core/mathjax_core.js"></script> </head>Configure custom fonts for TeXView
mainTo use custom fonts in
TeXView, you must define them in a CSS file located in your project's assets and then reference that CSS file in yourpubspec.yaml.- Create a file named
flutter_tex.cssin yourassets/directory. - Define your
@font-facerules within that CSS file, pointing to your font files (e.g.,fonts/Army.ttf). - Register the
assets/flutter_tex.cssfile in yourpubspec.yamlunder theflutter.assetssection. - Apply the font in your Dart code using
TeXViewStyleandTeXViewFontStyleby specifying thefontFamilyname defined in your CSS.
flutter: uses-material-design: true assets: - assets/flutter_tex.css@font-face { font-family: 'army'; src: url("fonts/Army.ttf"); }TeXViewStyle( fontStyle: TeXViewFontStyle( fontFamily: 'army'), )- Create a file named
Configure Android for flutter_tex
mainTo ensure proper functionality, update your
<project-directory>/android/app/src/main/AndroidManifest.xmlwith the following configurations:- Cleartext Traffic: Add
android:usesCleartextTraffic="true"to the<application>tag. - Permissions: Add the
android.permission.INTERNETpermission. - Queries Block: Add the required intent queries to allow the app to interact with various schemes (https, sms, tel, mailto, and CustomTabs).
<application ... android:usesCleartextTraffic="true"> </application> <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Queries --> <queries> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="https" /> </intent> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="sms" /> </intent> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="tel" /> </intent> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="mailto" /> </intent> <intent> <action android:name="android.support.customtabs.action.CustomTabsService" /> </intent> </queries>- Cleartext Traffic: Add
Customize iOS launch screen assets
mainTo change the launch screen image for the iOS version of your Flutter app, you can either replace the image files directly in the
example/ios/Runner/Assets.xcassets/LaunchImage.imageset/directory or use Xcode for a more visual approach.Using Xcode:
- Open the iOS project in Xcode by running
open ios/Runner.xcworkspacefrom your terminal. - In the Xcode Project Navigator, navigate to
Runner/Assets.xcassets. - Drag and drop your desired images into the asset catalog to replace the existing ones.
open ios/Runner.xcworkspace- Open the iOS project in Xcode by running
Apply a custom MathJax configuration
mainTo customize how MathJax renders mathematical notation (e.g., changing delimiters for inline or display math), you must provide a JavaScript configuration file named
flutter_tex.jslocated in your project'sassetsdirectory.Follow these steps:
- Create
assets/flutter_tex.jsin your project root. - Define your configuration using the
window.MathJaxobject. - Register the file in your
pubspec.yamlunder theassetssection.
flutter: uses-material-design: true assets: - assets/flutter_tex.js- Create
Configure iOS for flutter_tex
mainAdd the following keys to your
<project-directory>/ios/Runner/Info.plistto enable network transport security, embedded views, and application query schemes.<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> <key>io.flutter.embedded_views_preview</key> <true/> <key>LSApplicationQueriesSchemes</key> <array> <string>https</string> <string>http</string> <string>tel</string> <string>mailto</string> </array>Use TeXWidget for mixed text and math rendering
mainThe
TeXWidgetis a high-level wrapper designed to render LaTeX strings that contain a mixture of plain text and mathematical notation. It automatically identifies and handles both inline math (e.g.,\( ... \)) and display math (e.g.,$$ ... $$).TeXWidget( math: r"When \(a \ne 0 \), there are two solutions to \(ax^2 + bx + c = 0\) " r"and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$" )