Skip to main content

Module math

Module math 

Expand description

Shared structured mathematical typesetting.

The expression tree, recursive box layout, structural decorations, and hit-testable insertion gaps live here so read-only documents and live math editors use exactly the same geometry. Editing policy intentionally stays with the consuming application: a reader needs immutable MathML rendering, while a calculator additionally owns caret navigation and mutations.

Modules§

edit
Editing semantics for the math field: caret movement, word motion, selection, delete/backspace (with GeoGebra-style structure unwrapping), structured input building (/, ^, sqrt, pi→π, …) and clipboard.
layout
Recursive 2D box layout for the math model.
model
Structured model for the math editor.
render
Painter for the shared mathematical box layout.
tex
Best-effort TeX math parser.
typst
Typst math syntax lowered into the shared mathematical expression tree.

Structs§

Box2d
One positioned, paintable box.
FormulaDescriptor
Gap
A caret-insertable gap inside one laid-out row. index is the offset in that row (0..=len). x/top/bottom give the caret bar geometry.
Layout
The result of laying out a whole field.
MathExpr
The whole editable document for one field.
ParseDiagnostic
ParseOutput
Path
A caret location: a chain of (node index in the current row, which child slot) descents, terminated by an offset insertion gap in the deepest row. An empty steps with offset = k means “gap k in the root row”.

Enums§

BoxKind
What a laid-out box paints.
FormulaMode
FormulaSyntax
Node
One element of a Row.
Slot
Which child row of a structure a path step descends into.

Constants§

BASE_SIZE

Functions§

child_row
child_row_mut
draw_layout
Paint an immutable mathematical layout at origin in scene coordinates. Carets, selections, and clipping belong to the consuming editor or reader.
layout
Lay out a whole expression at the field origin. The fragment’s baseline is already at local y = ascent; boxes/gaps are emitted directly in content coords (top of the row at y = 0).
layout_wrapped
Lay out an expression no wider than max_width where it can be: a line that would run over breaks before a top-level relation or binary operator, and the continuation lines are indented by one em. Gaps keep their row indices, so a caret still lands where it did.
measure_atom
Full horizontal advance of one mathematical atom, including its TeX-style binary/relation spacing. Editors use this when mapping external geometry.
nearest_gap
Find the gap nearest a click point (px, py) (content-local). Picks the row whose vertical band contains py if any, else the globally nearest gap by Euclidean distance to the gap’s vertical extent.
node_slots
Slots a node exposes, in caret Up/Down navigation order (top → bottom).
parse
Parse a flat ASCII expression into a structured tree. Best-effort and total: anything it cannot classify is kept as literal Char atoms so the field never loses content (the engine reports semantic errors at plot time). Recognizes name(...) calls, (...) groups, ^ powers, sqrt(...)/root(i,x) radicals, and a/b fractions where a and b are simple immediate operands.
parse_tex
parse_typst
serialize_tex
Canonical TeX emitted after a user edits the parsed tree visually. Source-mode formulas retain their original bytes; this serializer is only used once the structured editor has made a semantic change.
serialize_typst
Canonical Typst math emitted after an actual structured edit.
to_linear
Serialize the whole expression to a flat ASCII string. Round-trips with parse.

Type Aliases§

Row
An ordered list of atoms. The editable unit.