Architecture
The engine is one Rust crate, matex, behind a thin CLI. Everything is a stage
with a named input and output; nothing reaches for a later stage's answer.
The pipeline
resolve imports, components, props, consts, types
expand components → markup, props substituted, consts evaluated
number document order after expansion, demand-driven, per citable kind
paginate page boundaries, line breaking, tables, formulas, floats
emit dynamic markup → one static file
render static markup → display list → PDF
| Command | Runs | Writes |
|---|---|---|
collect | resolve → expand → number | Stores |
compile | resolve → expand → paginate → emit | One static file |
render | layout → PDF | One PDF |
check | resolve → expand → number | Nothing |
collect is not optional for anything that prints a number — it is what
computes them — but it is a separate command: compile does not run it.
Why this order
- Resolve before expand, because expansion consumes imports and props.
- Number after expand, because the things counted are instances, not tags in a file.
- Paginate after number, so a number never depends on where a line lands.
- Emit before render, so drawing has no decisions left to make.
The source model
A .mtx file has a header of statements and then markup. Two shapes:
| Shape | Root | Holds |
|---|---|---|
| Document | <doc> | Markup, plus local declarations |
| Module | none | Imports, types, consts, components |
Statements are imports, type declarations, const declarations and
components. Each ends in ;. There is no token stream in the parser: markup and
expressions switch too often for one to earn its keep, and a cursor over
characters keeps spans exact.
Static source is defined by absence
compile emits one file. What is missing from it is the definition:
- no imports, components or declarations;
- no
flow; - no
<p>,<float>,<footnote>,<sup>or<sub>; - no interpolations and no unparsed math.
Everything else is the same vocabulary. A static page is a <page> with
geometry, a header, a footer and a <col> holding already-broken <line>
elements plus the structural blocks that cannot be reduced to lines — rows,
columns, tables, math and images. Each line has its space-before, height,
optional width, and one <span> per run with an optional x. Tables and formulas keep their structure so render repeats the
same arithmetic rather than reverse-engineering a picture.
That is what makes “render never invents a page” a structural claim rather than a promise.
Layout
Layout produces a display list: pages, each with four slots — header, content, notes, footer.
Page
├─ header (line blocks)
├─ content (blocks: lines, rows, columns, tables, math, images, floats)
├─ notes (footnote paragraphs)
└─ footer (line blocks)
Blocks split by default. A break never falls inside a line, an image or a rule.
A text block can refuse a break with keep; a heading always keeps with the
lines that follow. The fragment minimum requires N lines on both sides of a
break.
Pagination is iterative only when the document asks for it: pageCount() costs
one extra pass because the total must be known before the second layout.
Numbers and references
collect walks the expanded document in reading order and assigns numbers per
citable kind. Numbering is demand-driven: a heading is numbered when
{label()} appears in it, a figure or table when something reads its store
entry's label, an equation when its row carries \label{…}.
The result is written as one module per kind:
type Heading = "intro" | "method";
export const headings = { "intro": { label: "1" }, "method": { label: "2" } };
Three properties follow:
- Type safety. A citation's
tois a literal union of ids that exist. - Inspectability. The numbers are a file you can read.
- Stability. Numbers are positional, so they survive a rebuild.
compile reads labels from the stores; it never counts a second time.
Style resolution
One origin per property:
tag attribute → element.<tag> in config → engine default
There is no cascade, no selector and no inheritance from a container. An inline run inherits from the line it is part of because it is part of that line, not because a rule reached into it.
Crates and modules
The workspace has two members. matex-cli is a thin shell over matex.
| Module | Owns |
|---|---|
ast | Syntax tree: modules, types, components, expressions, markup |
parser | Hand-written parser for both dialects |
config | Project file and its defaults |
resolve | Loading files and binding imported names |
expand | Components, props, consts, expressions, demand tracking |
number | Labels, store modules, document order |
style | One-level appearance resolution |
font | Owned metrics, char-to-glyph map, named face set |
image | File paths and data URIs; PNG, JPEG, SVG, PDF/EPS rasterisation |
text | Inline spans, greedy line breaking, hyphenation |
math | Symbols, scripts, fractions, radicals, rows, cells |
layout | Pages, columns, tables, formulas, floats, targets |
emit | Serializing the static dialect |
pdf | PDF objects, embedded fonts, images, link annotations |
error | Problems as data, with spans and stable codes |
Invariants
- Nothing renders that was not resolved first;
renderaccepts static source only. rendernever invents a page.- A face is named by
font-family; measurement and drawing use the resolved face, never a default silhouette. - A line is broken in
compile, never at draw time. - One origin per property; no cascade and no selector.
- Every loss is reported: clipped content is a warning, a missing glyph an error.
- Numbers are computed once, by
collect, and read from the stores bycompile.
What exists, and what is designed
The design record distinguishes three states, and the distinction matters when reading this site:
| State | Meaning |
|---|---|
| Built | In the tree, exercised by the test suite |
| Decided | Written down, agreed, not implemented |
| Open | A question with no answer yet |
Class 1 is built. Class 2 and class 3 are designed. Some details inside class 1
— a repeated table header, running heads, footnotes — are built; others, such as
a per-container keep, are not.
Status is the authoritative list.