Files
texrra-engine/docs/TEX_CORE.md
T

4.4 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, fixed character/space metrics and glue stretch/shrink;
  • 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 model + word-level breaker implemented; hyphenation/TFM/pdfTeX parity next
M2 page builder + \output, \vsize, \topskip, footnotes, floats one-column article matches reference pages
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 is word-level with fixed metrics; it does not yet hyphenate, honor all penalties, or use TFM font metrics.
  • Page breaking only handles vertical boxes/glue; vertical penalties are recorded but not interpreted.
  • 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.