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.
- Formula
Descriptor - Gap
- A caret-insertable gap inside one laid-out row.
indexis the offset in that row (0..=len).x/top/bottomgive the caret bar geometry. - Layout
- The result of laying out a whole field.
- Math
Expr - The whole editable document for one field.
- Parse
Diagnostic - Parse
Output - Path
- A caret location: a chain of (node index in the current row, which child
slot) descents, terminated by an
offsetinsertion gap in the deepest row. An emptystepswithoffset = kmeans “gapkin the root row”.
Enums§
- BoxKind
- What a laid-out box paints.
- Formula
Mode - Formula
Syntax - Node
- One element of a
Row. - Slot
- Which child row of a structure a path step descends into.
Constants§
Functions§
- child_
row - child_
row_ mut - draw_
layout - Paint an immutable mathematical layout at
originin 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 aty = 0). - layout_
wrapped - Lay out an expression no wider than
max_widthwhere 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 containspyif 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
Charatoms so the field never loses content (the engine reports semantic errors at plot time). Recognizesname(...)calls,(...)groups,^powers,sqrt(...)/root(i,x)radicals, anda/bfractions whereaandbare 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.