Interaction and accessibility
Taps, long presses and hover on parts of a formula, highlights, selection and copy, keyboard navigation and screen readers.
Taps, long presses and hover
Section titled “Taps, long presses and hover”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.
Highlights
Section titled “Highlights”
import 'package:flutter/widgets.dart';import 'package:val_latex_flutter/val_latex_flutter.dart';
const _tex = r'x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}';const _discriminant = r'b^2-4ac';
/// The discriminant highlighted: a highlight names a span of the source.Widget example() { final start = _tex.indexOf(_discriminant); return Math.tex( _tex, displayMode: true, highlights: [ MathHighlight(SourceSpan(start, start + _discriminant.length)), ], );}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}'), ], ); }}Selection and copy
Section titled “Selection and copy”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.
Screen readers
Section titled “Screen readers”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.
Keyboard navigation
Section titled “Keyboard navigation”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,)API reference
Section titled “API reference”MathTap
Section titled “MathTap”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. |
MathHighlight
Section titled “MathHighlight”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. |
MathCopyFormat
Section titled “MathCopyFormat”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. |
MathCopy
Section titled “MathCopy”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. |