Getting started
Add val_latex_flutter to a Flutter app or val_latex to a Dart project, show your first formulas, and use the engine without Flutter.
Install
Section titled “Install”Flutter apps need only val_latex_flutter: it depends on val_latex and
re-exports it.
flutter pub add val_latex_flutterdart pub add val_lateximport 'package:val_latex_flutter/val_latex_flutter.dart';import 'package:val_latex/val_latex.dart';The Flutter import also gives you the whole engine (parseLatex,
layoutFormula, toSpeech and the rest), so you never need both.
| Package | Dart SDK | Flutter |
|---|---|---|
val_latex |
3.9 or later | not needed |
val_latex_flutter |
3.9 or later | 3.35 or later |
Neither package has platform code: no plugins, no WebView.
Your first formulas
Section titled “Your first formulas”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
/// The three widgets: a formula, a formula in display style, and text with/// formulas in it.Widget example() => const Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Math.tex(r'x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}'), SizedBox(height: 12), Math.tex(r'\int_0^1 x^2\,dx = \frac{1}{3}', displayMode: true), SizedBox(height: 12), MathText(r'The area of a circle is $\pi r^2$.'), ],);Math.texrenders one formula. Its size and colour come from the surroundingDefaultTextStyle, merged withtextStyleif you pass one.displayMode: truegives display style: larger operators, limits above and below, the formula centred on its own line, equation numbers at the margin.MathTextrenders a string with$…$and$$…$$formulas in it (and\(…\),\[…\]).MathSpanputs a formula insideText.rich, on the text baseline:
Text.rich( TextSpan( children: [ const TextSpan(text: 'Euler: '), MathSpan(r'e^{i\pi} + 1 = 0'), ], ),)Use raw strings (r'…') so backslashes reach the parser as written.
Without Flutter
Section titled “Without Flutter”The engine works the same in plain Dart. Sizes are in em: multiply by the font size for pixels.
import 'package:val_latex/val_latex.dart';
void main() { // Parse. Problems become diagnostics, not exceptions. final result = parseLatex(r'x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}'); print(result.diagnostics); // []
// Lay out with the bundled Latin Modern Math. final layout = layoutFormula(result); print(layout.width); // in em print(layout.items.length); // glyphs, rules and paths to draw
// Read it aloud, or write it back as normalised LaTeX. print(toSpeech(result)); print(toLatex(result)); // x=\frac{-b\pm\sqrt{b^{2}-4ac}}{2a}}toSpeech prints:
x equals the fraction with numerator minus b plus or minus the square root of b squared minus 4 a c end root and denominator 2 a end fractionSee Dart engine for parsing, layout and the rest.
There is nothing to configure.
- Math fonts. Latin Modern Math, and STIX Two Math for the symbols it
lacks, are embedded in
val_latex. - Text fonts.
val_latex_flutterbundles the Latin Modern text fonts (roman, sans, mono and small caps) for\text{…}and the like, so text inside a formula matches the math.
To use other fonts, see Styling and fonts.
Other imports
Section titled “Other imports”| Import | For |
|---|---|
package:val_latex_flutter/flutter_math_compat.dart |
A replacement for package:flutter_math_fork/flutter_math.dart. See Migrating. |
package:val_latex/advanced.dart |
Engine internals for tooling (editors, linters, custom front ends): the lexer, the macro expander, the parser class and the symbol tables. It may change more often than the main library. |
Licences
Section titled “Licences”The packages are BSD 3-Clause. The bundled fonts keep their own licences:
the GUST Font License for Latin Modern and the SIL Open Font License for STIX
Two Math. The work by others the packages contain is credited in each
package’s THIRD_PARTY_NOTICES.md.
In a Flutter app those notices are added to Flutter’s licence page
(showLicensePage) as soon as the first formula is built. To add them earlier,
call registerValLatexLicenses() yourself; calling it again does nothing.
API reference
registerValLatexLicenses()
Section titled “registerValLatexLicenses()”Adds the third-party notices of val_latex and val_latex_flutter
(valLatexNotices, valLatexFlutterNotices) to Flutter’s licence page
(showLicensePage, LicenseRegistry).
MathPainter calls it when it is first created, so an app that shows
a formula needs nothing more; calling it again does nothing.
void registerValLatexLicenses()addThirdPartyNotices()
Section titled “addThirdPartyNotices()”Adds notices to Flutter’s licence page under package.
void addThirdPartyNotices(String package, List<ThirdPartyNotice> notices)ThirdPartyNotice
Section titled “ThirdPartyNotice”Work by others that a package contains, and the notice its licence asks to go with it.
More about ThirdPartyNotice
val_latex_flutter adds these to Flutter’s licence page
(LicenseRegistry) the first time it paints a formula, so apps show
them with the rest. Each package’s THIRD_PARTY_NOTICES.md lists the
same.
| Constructor | Description |
|---|---|
ThirdPartyNotice({required name, required usedFor, required text}) |
Creates a notice. |
| Field | Type | Default | Description |
|---|---|---|---|
name |
String |
required |
The work, e.g. HarfBuzz. |
usedFor |
String |
required |
What the package uses it for. |
text |
String |
required |
The copyright and licence text, as its licence asks. |
valLatexNotices
Section titled “valLatexNotices”The third-party notices of val_latex
(THIRD_PARTY_NOTICES.md).
const List<ThirdPartyNotice> valLatexNotices = …;valLatexFlutterNotices
Section titled “valLatexFlutterNotices”The third-party notices of val_latex_flutter
(THIRD_PARTY_NOTICES.md).
const List<ThirdPartyNotice> valLatexFlutterNotices = [ ThirdPartyNotice( name: 'Latin Modern', usedFor: 'The bundled text fonts (fonts/latin-modern/) for \\text{…}.', text: '% This is a preliminary version (2006-09-30), barring acceptance from\n% the LaTeX Project Team and other feedback, of the GUST Font License.\n% (GUST is the Polish TeX Users Group, http://www.gust.org.pl)\n%\n% For the most recent version of this license see\n% http://www.gust.org.pl/fonts/licenses/GUST-FONT-LICENSE.txt\n% or\n% http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt\n%\n% This work may be distributed and/or modified under the conditions\n% of the LaTeX Project Public License, either version 1.3c of this\n% license or (at your option) any later version.\n% \n% Please also observe the following clause:\n% 1) it is requested, but not legally required, that derived works be\n% distributed only after changing the names of the fonts comprising this\n% work and given in an accompanying "manifest", and that the\n% files comprising the Work, as listed in the manifest, also be given\n% new names. Any exceptions to this request are also given in the\n% manifest.\n% \n% We recommend the manifest be given in a separate file named\n% MANIFEST-<fontid>.txt, where <fontid> is some unique identification\n% of the font family. If a separate "readme" file accompanies the Work, \n% we recommend a name of the form README-<fontid>.txt.\n%\n% The latest version of the LaTeX Project Public License is in\n% http://www.latex-project.org/lppl.txt and version 1.3c or later\n% is part of all distributions of LaTeX version 2006/05/20 or later.', ),];