val_latex_flutter

LaTeX math for Flutter.

Formulas as widgets, inline math in text, and wrapping to the width.
Pure Dart and native Flutter rendering, on every platform.

Pub Version Pub Points BSD-3-Clause license

โš™๏ธ Engine ยท ๐Ÿ“ฆ pub.dev


โœจ Why val_latex_flutter?

  • One widget per formula โ€” Math.tex(r'\frac{a}{b}'), sized and coloured by the surrounding text style.
  • Math inside text โ€” MathText renders a string with $โ€ฆ$ and $$โ€ฆ$$ formulas in it, wrapping formulas with the words, and is safe for text that is still streaming in (streaming: true). Math.tex treats its formula as complete, so mistakes show; pass ParseOptions(streaming: true) to it for partial input.
  • Fits any width โ€” long formulas break into lines the way a person would break them; the rest scroll, shrink or clip.
  • Native rendering โ€” math glyphs are drawn from the font's outlines and text goes through Flutter's text engine, so Arabic, Indic and every other script shape correctly. No WebView, no platform code.
  • Selectable and accessible โ€” formulas take part in SelectionArea and copy their LaTeX source; screen readers hear them spoken and can move through them part by part.
  • Fast โ€” parses and layouts are cached and shared, and each formula replays its drawing instead of rebuilding it on every repaint.

๐Ÿ› ๏ธ Quick start

flutter pub add val_latex_flutter
import 'package:val_latex_flutter/val_latex_flutter.dart';

// A formula; size and colour follow DefaultTextStyle.
Math.tex(r'x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}')

// Display style: centred, big operators, equation numbers at the margin.
Math.tex(r'\begin{align} a &= b \\ c &= d \end{align}', displayMode: true)

// Text with formulas in it.
MathText(r'The area of a circle is $\pi r^2$.')

// Inline in a paragraph, on the text baseline.
Text.rich(TextSpan(children: [
  const TextSpan(text: 'Area is '),
  MathSpan(r'\pi r^2'),
]))

This package re-exports val_latex, so one import gives the parser and layout engine too.

The example app is a tour of every feature, each live beside the code that makes it.

๐Ÿ“ Wrapping and overflow

Display formulas break to the available width. What cannot break follows overflow:

Math.tex(longFormula, displayMode: true, overflow: MathOverflow.shrink)

MathOverflow.scroll, .shrink (down to minShrink, then scroll) and .clip. Every formula widget reports its baseline, so it lines up with text in a baseline Row, in Text.rich, and when it scrolls or shrinks.

๐ŸŽจ Theming

MathTheme sets defaults for every formula below it: colour, error and selection colours, the math font, fallback fonts, and styles per \htmlClass name.

MathTheme(
  data: const MathThemeData(color: Colors.indigo),
  child: child,
)

Latin Modern text fonts are bundled for \text{โ€ฆ}, matching the math font; MathTextFonts chooses others.

๐Ÿ‘† Interaction

  • Taps and hover โ€” onTap, onLongPress and onHover report the tapped part with its source span.
  • Highlights โ€” highlights emphasise parts of a formula and animate when they change.
  • Selection โ€” inside a SelectionArea, selecting copies the LaTeX of what is selected (copyFormat for $โ€ฆ$-delimited source).
  • Keyboard navigation โ€” focusable: true moves through the parts with the arrow keys, announced to screen readers.

๐Ÿ” Coming from flutter_math_fork

import 'package:val_latex_flutter/flutter_math_compat.dart';

A drop-in replacement for package:flutter_math_fork/flutter_math.dart: the same Math.tex, SelectableMath, options and settings, backed by val_latex.

๐Ÿ–Œ๏ธ Your own canvas

layoutFormula gives the layout and MathPainter draws it on any Canvas, for charts, whiteboards and custom render objects.

โšก Performance

  • Cached โ€” MathCache shares parses and layouts between widgets, and a formula scrolled back into a list reuses its layout.
  • Repaints replay a recording โ€” each formula records its drawing once.
  • Work only when asked โ€” the spoken label and selection data are worked out when a screen reader or a selection needs them.
  • Off the UI thread โ€” layoutInBackground: true lays a very long formula out in a worker isolate.

Built, laid out and compared with flutter_math_fork on the same 120 formulas in a widget test, val_latex_flutter is about 3.9 times faster.

Built by Val

val_latex_flutter is part of Val, the live visual layer for AI agents.

๐Ÿ’ฌ Community

Found a bug, or have an idea or feedback? Open an issue at github.com/useval/val_latex. If the package helps your project, consider giving it a like on pub.dev.

๐Ÿ“„ License

BSD 3-Clause โ€” see LICENSE. The bundled Latin Modern text fonts keep their own licence (GUST Font License), in fonts/latin-modern/; THIRD_PARTY_NOTICES.md credits them. Apps show the notices of this package and of val_latex on Flutter's licence page (showLicensePage) without any setup.

Libraries

flutter_math_compat
Drop-in replacement for package:flutter_math_fork/flutter_math.dart.
val_latex_flutter
Flutter rendering for val_latex.