Skip to content
Talk to an engineerRequest access

MathSpan

A formula inside Text.rich, sitting on the text baseline, with taps, hover and copy like Math.

A sentence about a circle with the formulas r, πr², 2πr and r over 2 inline, on the text baseline

MathSpan is a WidgetSpan holding a Math.tex in text style, aligned on the alphabetic baseline, so it lines up with the words around it. Its constructor is not const.

const style = TextStyle(fontSize: 18);
Text.rich(
TextSpan(
style: style,
children: [
const TextSpan(text: 'Area '),
MathSpan(r'\pi r^2', style: style),
],
),
)
  • Size and colour come from style, merged over the ambient DefaultTextStyle. The span is not scaled a second time by the paragraph’s text scaler.
  • Taps: onTap, onLongPress and onHover report the part under the pointer as a MathTap, as on Math. With onTap or onLongPress the mouse cursor is a click cursor unless you pass mouseCursor.
  • Screen readers hear semanticsLabel, or the formula spoken.
  • Copy: inside a SelectionArea, copying gives the LaTeX source, or what copyFormat makes of it.
  • Unfinished input is an error, as on Math.tex; pass parseOptions: ParseOptions(streaming: true) for input still arriving.

A span does not break across lines: a long inline formula in a narrow paragraph overflows its line. For paragraphs whose formulas should wrap with the words, use MathText.

Inline math inside Text.rich, aligned on the text baseline.

More about MathSpan
Text.rich(TextSpan(children: [
const TextSpan(text: 'Area '),
MathSpan(r'\pi r^2'),
]))

onTap, onLongPress and onHover report the node under the pointer, as on Math.onTap. mouseCursor and semanticsLabel mean what they do on TextSpan: the cursor defaults to a click cursor when the span has onTap or onLongPress.

Creates an inline formula.

MathSpan(
String tex, {
TextStyle? style,
ParseOptions parseOptions = const ParseOptions(streaming: false),
MathTextFonts textFonts = MathTextFonts.latinModern,
MacroTable? macros,
void Function(MathTap tap)? onTap,
void Function(MathTap tap)? onLongPress,
void Function(MathTap? tap)? onHover,
MouseCursor? mouseCursor,
String? semanticsLabel,
MathCopyFormat copyFormat = MathCopyFormat.latex,
})

The other parameters mean what they mean on Math.