Supported LaTeX
What val_latex understands — LaTeX and AMS math, environments, chemistry, units, physics, diagrams, text tables and lists, macros and Unicode input.
val_latex reads the LaTeX math that people and models write: the LaTeX
kernel and AMS math, the common packages, and the names KaTeX and
flutter_math_fork define. LatexCatalog.instance lists every name the
parser accepts, read from the same tables the parser uses — in 0.1.4 that is
more than 4,400 command names and 88 environments.
final catalog = LatexCatalog.instance;print(catalog.allCommands.contains(r'\frac')); // trueprint(catalog.environments.contains('align')); // trueprint(catalog.extension(LatexExtension.physics).contains(r'\dv')); // true| Area | Examples |
|---|---|
| Core LaTeX and AMS math | fractions, roots, scripts and limits, accents, \left…\middle…\right, delimiters at every size, every math alphabet (\mathbb, \mathcal, \mathfrak, \mathscr…), \boxed, \cancel, \overbrace |
| Environments | align, gather, multline, split, cases, the matrix family, array with rules and \multicolumn, subequations, CD |
| Equation numbers | automatic numbering in equation, align and the rest, \tag, \notag, \label with \ref/\eqref |
| Packages | mathtools, mhchem (\ce, \pu), siunitx, physics, diffcoeff and derivative, commath, nccmath, empheq, esint, tensor, stmaryrd, wasysym, unicode-math names |
| Diagrams | tikzcd commutative diagrams, CD, and bussproofs prooftree |
| Text | \text with math inside, tabular, tabularx and booktabs rules, itemize, enumerate and description lists |
| Macros | \def, \newcommand, \newenvironment, \NewDocumentCommand, \DeclareMathOperator, \DeclarePairedDelimiter, TeX conditionals and registers |
| Colour | \color, \textcolor, \colorbox; 148 CSS names and xcolor’s dvips names (NavyBlue), \definecolor, mixes (red!30!blue) |
| Unicode input | α, ∑, x², a₁ typed directly |
Common package commands such as \abs, \dv, \sfrac, \vv and
\celsius work without loading anything, wherever nothing else defines the
name (LatexCatalog.instance.fallbacks).
Environments and equation numbers
Section titled “Environments and equation numbers”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
/// Aligned equations with numbers and a tag, cases and a matrix.Widget example() => const Column( children: [ Math.tex( r'\begin{align} (a+b)^2 &= a^2 + 2ab + b^2 \\ ' r'(a-b)^2 &= a^2 - 2ab + b^2 \tag{*} \end{align}', displayMode: true, ), SizedBox(height: 8), Math.tex( r'|x| = \begin{cases} x & x \ge 0 \\ -x & x < 0 \end{cases} \qquad ' r'A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}', displayMode: true, ), ],);Chemistry, units and physics
Section titled “Chemistry, units and physics”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
/// mhchem and siunitx, on by default; physics notation with its/// extension turned on.Widget example() => const Column( children: [ Math.tex(r'\ce{2H2 + O2 -> 2H2O}', displayMode: true), SizedBox(height: 10), Math.tex(r'\ce{CO2 + C <=> 2CO}', displayMode: true), SizedBox(height: 10), Math.tex(r'g = \SI{9.81}{\metre\per\second\squared}', displayMode: true), SizedBox(height: 10), Math.tex( r'\dv{f}{x} + \pdv{g}{y} = \expval{H}', displayMode: true, parseOptions: ParseOptions( streaming: false, extensions: {...LatexExtension.defaults, LatexExtension.physics}, ), ), ],);Command sets that change what existing names mean are extensions, chosen
with ParseOptions.extensions:
LatexExtension |
Commands | Default |
|---|---|---|
mhchem |
\ce, \pu |
on |
siunitx |
\num, \si, \SI, \qty, \unit, \ang, ranges and lists (a subset) |
on |
physics |
\abs, \norm, \qty, \dv, \pdv, \expval, \mel, \comm, \vb, \grad… (a subset) |
off |
The physics extension is off because, like the LaTeX package, it redefines
\div, \Re, \Im and siunitx’s \qty. Its other commands work without
it. To turn it on, keep the defaults and add it:
const options = ParseOptions( extensions: {...LatexExtension.defaults, LatexExtension.physics}, physics: PhysicsOptions(italicDiff: true),);PhysicsOptions mirrors the package options: trig (default true),
italicDiff (default false) and arrowDel (default false).
Diagrams and proofs
Section titled “Diagrams and proofs”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
/// A commutative diagram (`tikzcd`) and a proof tree (`prooftree`).Widget example() => const Column( children: [ Math.tex(r'''\begin{tikzcd} A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\ C \arrow[r, "k"'] & D\end{tikzcd}''', displayMode: true), SizedBox(height: 12), Math.tex(r'''\begin{prooftree} \AxiomC{$A$} \AxiomC{$A \to B$} \RightLabel{$\to$E} \BinaryInfC{$B$}\end{prooftree}''', displayMode: true), ],);Some tikzcd options are read but not applied; see
Limitations.
Text, tables and lists
Section titled “Text, tables and lists”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
/// A booktabs table and a numbered list, as chat replies write them.Widget example() => const Column( children: [ Math.tex( r'\begin{tabular}{lcc} \toprule & \multicolumn{2}{c}{Accuracy} \\' r' \cmidrule(lr){2-3} Method & Train & Test \\ \midrule' r' Baseline & 88.1\% & 81.2\% \\ Ours & 97.0\% & \textbf{93.5\%} \\' r' \bottomrule \end{tabular}', displayMode: true, ), SizedBox(height: 12), Math.tex( r'\begin{enumerate} \item Expand $(x+1)^2$ \item Collect the terms' r' \item Solve for $x$ \end{enumerate}', displayMode: true, ), ],);Text inside a formula is shaped by Flutter’s text engine, so every script
your fonts cover shapes correctly; see Styling and fonts.
\caption in a table is unnumbered unless you set
ParseOptions.captionLabel (for example 'Table').
Macros
Section titled “Macros”Define macros in the source with \def, \newcommand, \newenvironment,
\NewDocumentCommand, \DeclareMathOperator or \DeclarePairedDelimiter.
A definition lasts until the end of its formula. To share definitions across
formulas, create one table with newMacroTable(), define into it with
\gdef, and pass it to every formula that should see them:
class Formulas extends StatelessWidget { const Formulas({super.key});
/// One table, passed to every formula that should see the definitions. static final macros = newMacroTable();
/// Defines the macros once. \gdef is global, so they stay in the table. static void define() => parseLatex( r'\gdef\RR{\mathbb{R}} \gdef\norm#1{\left\lVert #1 \right\rVert}', macros: macros, );
@override Widget build(BuildContext context) { return Column( children: [ Math.tex(r'f \colon \RR^n \to \RR', displayMode: true, macros: macros), Math.tex(r'\norm{x + y} \le \norm{x} + \norm{y}', macros: macros), ], ); }}Call Formulas.define() once, for example in main(), before the formulas
are built. MathText and MathSpan take macros too. A formula with a
shared table is not cached (its parse depends on what was defined before).
For a fixed set, ParseOptions(macros: {r'\RR': r'\mathbb{R}'}) is simpler:
the name with its backslash, and the replacement text, with #1…#9 for
arguments.
Input as models write it
Section titled “Input as models write it”Models write LaTeX that is almost right. Where the intent is clear, val_latex reads it and records a warning instead of failing:
| Input | Read as | Warning |
|---|---|---|
\\frac{a}{b} (backslashes doubled by JSON) |
\frac{a}{b} |
doubledBackslashes |
a &= b \\ &= c with no environment |
an aligned block |
implicitAlignment |
x^2' |
{x^2}' |
primeAfterSuperscript |
| a blank line inside math | a line break | paragraphInMath |
Warnings never stop a formula rendering. See Differences from KaTeX for the full list of choices like these.