Files
texrra-engine/docs/TEX_CORE.md
T

6.0 KiB
Executable File

TeX-compatible incremental core (route D)

Status: M0 architecture spike + M1 paragraph builder implemented in crates/texrra-tex-core. The production v1 engine in src/rust_typeset remains the default and is kept as a fallback while the TeX-compatible core grows.

Why route D

The v1 engine parses LaTeX into DocumentModel and lays it out with hand-written algorithms. That gives true incremental preview, but it cannot be page-identical for arbitrary .cls/.sty files: class files are TeX programs, and their layout primitives (\vbox, \halign, \output, \twocolumn, insertions, math lists, font metrics) are not executed.

Route D is a TeX-compatible execution/layout core designed for incremental operation from the start. It does not embed pdfTeX, XeTeX or Tectonic.

M0 scope (this spike)

crates/texrra-tex-core currently provides:

  • VerticalItem::{Box, Glue, Penalty} and VerticalBox as the first vertical list model;
  • Unit::paragraph backed by the M1 box/glue/penalty paragraph builder;
  • Engine::layout that builds pages with a simple page goal;
  • Checkpoint before every top-level unit, containing the completed-page count, used vertical space and current page items;
  • Page hashes derived from both vertical items and the contributing unit sources;
  • Engine::edit which restores the checkpoint before the edited unit, re-executes only the suffix, compares page hashes, and reports recomputed_pages;
  • serde round-trip for checkpoints.

M0 proves the incremental contract; M1 starts replacing the placeholder with horizontal material and a paragraph breaker. The page builder, math, fonts and class execution are still ahead.

M1 scope (paragraph builder)

crates/texrra-tex-core/src/paragraph.rs adds:

  • HorizontalItem::{Box, Glue, Penalty} and HorizontalBox as the horizontal list model;
  • ParagraphStyle with line_width, par_indent, line_height and glue stretch/shrink;
  • Cmr10Metrics, a CMR10 ASCII width table extracted from cmr10.tfm with tftopl, plus the fontdimen space/stretch/shrink values;
  • an item-stream Knuth-Plass-style dynamic program over character boxes, glue and penalty breakpoints that minimizes badness from glue stretch/shrink, allows overfull lines with an explicit overfull penalty, applies par_indent to the first line, and emits justified glue;
  • Paragraph { lines, height, width } and a paragraph-backed Unit.

M1 still does not hyphenate, process \discretionary, use real TFM metrics or compare against pdfTeX. Those are the next paragraph milestones.

Target architecture

  • Persistent TeX VM state: macros, registers, fonts, counters, output queue.
  • Stable IDs for top-level units.
  • Checkpoint at page boundaries plus unit boundaries.
  • Dependency graph for labels, references, counters, TOC and citations.
  • Dirty propagation: after a local edit, replay from the nearest checkpoint and stop at the first unchanged page hash.
  • Float/footnote queues restored with the checkpoint.

Milestones

Milestone Content Acceptance
M0 vertical list, page builder, checkpoint, page hash, edit demo edit on page 2 rebuilds only page 2
M1 TeX box/glue/penalty + paragraph builder (\par, \hsize, \parindent) partial: item stream, CMR10 metrics, pdfTeX fixture and minimum line fill; hyphenation next
M2 page builder + \output, \vsize, \topskip, footnotes, floats partial: \vsize page goal, \topskip, forced penalties, Insertion reserve, running Marks, Rust OutputRoutine shipout hook; interpreted \output, float placements next
M3 \halign, \omit, \span, \multispan, booktabs/longtable table width/rules match reference
M4 math lists, scripts, fractions, radicals, accents, delimiters arXiv formulas render without overlap
M5 NFSS/TFM metrics, CM/Latin Modern same line/page breaks as reference
M6 class/package execution (article, sn-jnl) Overleaf page-count parity
M7 \special, PDF XObjects, hyperref annotations PDF structure parity

Current limits

  • The M1 paragraph builder uses a CMR10 ASCII metric table; it does not yet hyphenate, honor all penalties, apply kerning/ligatures, or parse arbitrary TFM files.

Multi-width pdfTeX comparison (same 24-word ASCII paragraph):

\hsize pdfTeX words per line current DP
120pt 5, 6, 7, 4 3, 4, 4, 4, 5
150pt 6, 7, 6, 4 4, 5, 6, 6
180pt 7, 7, 8 7, 7, 8
200pt 8, 7, 7 6, 7, 9

The remaining difference is the TeX tolerance/demerit model, not CMR10 metrics: TeX accepts moderately overfull lines that our DP rejects.

Concrete 120pt first-line candidates: a 3-word line has natural 109.4pt, stretch 4.6pt, badness 396; a 5-word line has natural 142.8pt, shrink 5.6pt, overfull ratio 4.11, badness 6942. TeX rejects the underfull candidate under its tolerance but still emits the moderately overfull 5-word line. Reproducing this needs a two-pass tolerance algorithm with an emergency overfull pass, not a single cost function.

  • Page breaking handles vertical boxes/glue, Document::top_skip and forced vertical penalties (penalty <= -10000); positive penalties are recorded but not yet part of page badness. VerticalItem::Insertion material is attached to Page::insertions and reserves height from the page goal. VerticalItem::Mark material is collected into Page::marks and included in the page hash. OutputRoutine::shipout runs before sealing and may mutate each page; this models the \output stage in Rust rather than as an interpreted token list, and cannot yet change page goals or place floats.
  • No macro state, output routine, math, fonts or PDF output in the new core yet.
  • The v1 server and project API still use src/rust_typeset and are unchanged.

Verification

cargo test -p texrra-tex-core --offline

The M0 tests cover page splitting, checkpoint serialization and page-local editing. Three M1 tests cover glue-based line breaking, paragraph indentation and empty paragraphs.