Skip to content
Talk to an engineerRequest access

Interaction and accessibility

Taps, long presses and hover on parts of a formula, highlights, selection and copy, keyboard navigation and screen readers.

onTap, onLongPress and onHover on Math.tex and MathSpan report the part of the formula under the pointer as a MathTap. onHover reports null when the pointer leaves or is over empty space.

MathTap member What it is
tex The TeX source of the tapped node.
link The \href target, when the tap was on a link.
node The tapped node.
path The nodes from the root down to the tapped node.
hit.info.span The tapped node’s position in the source (SourceSpan).
hit.info.classes The \htmlClass names in effect.
source The formula’s whole source.

\href and \htmlClass are untrusted commands: they only make links and classes with ParseOptions(trust: true) or a trustCallback. See Errors and untrusted input.

The quadratic formula with the discriminant b² − 4ac on a light amber background

highlights emphasises parts of the source — the step being explained, the term a tutor is pointing at. MathHighlight(span) highlights the nodes within a SourceSpan of the source, on a translucent amber background by default; color recolours the highlighted items themselves. A change of highlights animates over highlightDuration (250 ms by default; zero for instant).

To highlight whatever the user taps, keep the tap and highlight its span:

class TapToHighlight extends StatefulWidget {
const TapToHighlight({super.key});
@override
State<TapToHighlight> createState() => _TapToHighlightState();
}
class _TapToHighlightState extends State<TapToHighlight> {
MathTap? _tapped;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Math.tex(
r'x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}',
displayMode: true,
onTap: (tap) => setState(() => _tapped = tap),
highlights: [
if (_tapped?.hit.info.span case final span?) MathHighlight(span),
],
),
Text(_tapped == null ? 'Tap a part.' : 'Tapped: ${_tapped!.tex}'),
],
);
}
}

Inside a SelectionArea, formulas take part in text selection. A selection inside a formula covers whole atoms — inside fractions, scripts and groups too — and a selection across groups takes the whole structures, so the copied LaTeX parses on its own. copyFormat chooses what is copied:

Format Copies
MathCopyFormat.latex The LaTeX source (the default).
MathCopyFormat.delimitedLatex The source in $…$, or $$…$$ for display formulas, ready to paste into Markdown.

A format is a name and a function of the copied formula (MathCopy, with tex, display and parsed), so you can write your own:

final parenthesised = MathCopyFormat(
'parenthesised',
(copy) => copy.display ? '\\[${copy.tex}\\]' : '\\(${copy.tex}\\)',
);
final formula = Math.tex(r'a^2 + b^2 = c^2', copyFormat: parenthesised);

MathTheme’s selectionColor sets the highlight; it defaults to the ambient DefaultSelectionStyle.

Each formula is announced as spoken math. The default label is toSpeech of the formula in speechLanguage, at speechVerbosity; set semanticsLabel to say something else.

Parameter Default Values
speechLanguage SpeechLanguage.english english, arabic; spanish, french, german, portuguese, bengali and hindi are drafts, not yet checked by a native reader
speechVerbosity SpeechVerbosity.normal brief (structures read inline), normal, verbose (every fraction, root and group announces its parts and end)

Semantics are covered by the package’s widget tests; they have not yet been checked with TalkBack or VoiceOver on a device.

With focusable: true the formula takes keyboard focus. The arrow keys move between its parts — ← and → along a row, ↓ into a fraction, root or group, ↑ back out — and Home and End jump to the ends of the row. The current part is outlined and announced to screen readers, and onFocusPart is called with each MathPart reached.

const Math.tex(
r'\sqrt{x^2 + y^2} \le \frac{a}{b}',
displayMode: true,
focusable: true,
)

A tap on a formula: the item under the finger and where it came from.

Constructor Description
MathTap(hit, source, {result}) Creates a tap.
Field Type Default Description
hit MathHit required The painted item and its metadata (span, node id, classes, link).
source String required The formula’s source.
result ParseResult? The parse the tap is in (when the formula was built from source).
Member Type Description
tex String? The TeX of the tapped node, or null when unknown.
link String? The \href target, when the tap was on a link.
path List<MathNode> The tapped node, from the root down: the fraction, then its numerator row, then the symbol. Empty without result.
node MathNode? The tapped node itself, or null.

A part of a formula to emphasise.

Constructor Description
MathHighlight(span, {background = const Color(0x55FFD54F), color}) Highlights the nodes within span of the source.
Field Type Default Description
span SourceSpan required The source span to highlight.
background Color? const Color(0x55FFD54F) Fill behind the highlighted items.
color Color? Colour for the highlighted items themselves, or null to keep theirs.

What copying a selected formula gives: latex (the default) or delimitedLatex. A format is a function of the copied formula (MathCopy), so others can be written the same way.

Constructor Description
MathCopyFormat(name, convert) A format named name that turns a MathCopy into text.
Field Type Default Description
name String required The format’s name.
convert String Function(MathCopy copy) required The text copied.
Member Type Description
static latex The LaTeX source of the selected nodes.
static delimitedLatex The LaTeX source in $…$ (or $$…$$ for display formulas), ready to paste into Markdown.

What is being copied, for a MathCopyFormat.

Constructor Description
MathCopy(tex, {…}) Describes a copy.
MathCopy: more
MathCopy(
this.tex, {
required this.display,
required ParseResult Function() parse,
})
Field Type Default Description
tex String required The LaTeX source of the selected nodes (whole nodes, so it parses on its own).
display bool required Whether the formula is display math.
Member Type Description
parsed ParseResult tex parsed.