Expansion kept a single flat macro table, so a \def inside {...} replaced the
outer definition for the rest of the document. Keep a scope stack in
expand_macros instead: '{' and \begingroup push a scope, '}' and \endgroup pop
it, and lookup walks the scopes newest-first before the document table. A
top-level \def and every \gdef update the document table; a \def inside a group
only shadows until the group closes.
Runtime \def\name{body} is now parsed during expansion (the pre-expansion inline
capture is gone), and grouping braces are dropped after command parsing so the
scope braces do not print: plain pieces, link labels, inline runs and the
fallback face path all strip them at that point.
A test previews \def\a{one}{\def\a{two}\a}\a as "twoone" and the
\begingroup form likewise; the existing inline-definition test still passes.
Catcode state and a real token stream remain open.
The line-based capture treated any line that began with a definition command as
a definition and dropped the rest of the line, so
\makeatletter\def\@foo{bar}\makeatother\@foo lost everything after the body.
Only consume such a line when the definition actually ends it; otherwise leave
it for a new inline pass that captures simple \def\name{body} forms anywhere in
running text.
\makeatletter/\makeatother are stripped as no-ops and the macro expander now
scans @ as a letter, matching control_sequence, so \@foo expands to its body.
A test defines \@foo and \greet in one body line and checks the previewed text
"bar and Hello." with no warnings. Group scoping, catcode state and a token
stream remain open.
The table capture ended at the first \end{tabular}, so a nested table inside a
cell closed the outer table early and the remaining inner rows leaked into the
document. Track the nesting depth of \begin{tabular}/\end{tabular} and only
close the outer environment at depth zero.
Nested blocks are flattened before the outer halign parses them: inner rows are
split on \\ and cells on &, rule commands and \multicolumn/\multirow wrappers
are stripped, and the text is joined as "a b ; c d" inside the outer cell. The
outer structure, column count and row count survive; true nested-table layout
and \valign remain open.
A regression test wraps a 2x2 tabular in a 2-column outer table and checks one
captured table, two columns, two rows and the flattened inner text.
\alpha through \omega, the uppercase Greek commands and the varepsilon /
vartheta / varrho / varsigma / varphi / varpi / varkappa variants were not in
the math command table, so a formula such as $\alpha+\beta$ painted "?" glyphs.
Map them to their Unicode code points. The math renderer already treats the
Greek ranges as math letters and sends them through the math-italic fallback,
so the formula now previews as α+β=γ with no warnings; the widths still use the
TFM fallback constant rather than per-letter cmmi metrics.
A core test pins the parsed symbols and a route-D test checks the previewed
characters.
\sbox/\savebox now measure their content at definition with the Roman metrics
at the class base size and store width/height/depth. \usebox carries that size
into the no-break segment, which scales the box back to its definition size
inside a differently sized paragraph, and \wd/\ht/\dp\boxname expand to the
frozen metrics before paragraph parsing.
\mbox/\hbox/\makebox keep following the surrounding size (their marker carries
size 0). A core test checks that a box frozen at 20pt is twice as wide as the
10pt one, and a route-D test checks the measured dimensions and a
\hspace{\wd\box} expansion. Image-only boxes still measure as empty text.
Three savebox tests strip the internal markers (now with their size prefix)
before comparing visible text.
Add InlineSegment::NoBreak: it shapes each word and replaces the interword
glue with fixed-width boxes, so the run has no breakpoints and behaves like
TeX's unbreakable hbox rather than reflowing at the use site.
Route-D wraps \usebox bodies and \mbox/\hbox/\makebox arguments in private-use
markers; the paragraph text builder splits on them and emits NoBreak runs,
leaving ordinary text and styling on the existing path. \underline keeps its
old inline behaviour.
A 60pt measure now keeps \mbox{alpha beta gamma} and a stored
\sbox{alpha beta gamma} on one line, covered by a core segment test and a
route-D test. Savebox dimensions are still measured at the use site instead of
being frozen at definition, and \wd/\ht/\dp accessors remain open.
inline_math_segments returned None for every paragraph whose base size was not
10pt, so a 12pt or \Large paragraph kept \textcolor/\href/\textsuperscript
delimiters as literal text. Only inline math actually depends on the 10pt
metric baseline: build the segments at any size and fall back to literal
delimiters only when a math segment is present and the size differs.
The existing sized-href test moves from "href is stripped but not linked" to
"the sized run keeps its link", and a new 12pt test covers a red run, a link
and a superscript in one paragraph with no warnings.
Parse \pagestyle{fancy}, \fancyhf, \fancyhead/\fancyfoot with L/C/R positions,
the \lhead/\chead/\rhead/\lfoot/\cfoot/\rfoot shorthands and
\renewcommand{\headrulewidth}, and paint them on every page. Head fields are
placed at top margin - \headsep and feet at text height + \footskip, both in
content coordinates so the page origin shifts them with the body; left, centre
and right align against the text measure, \thepage is expanded per page, and
the head rule uses the requested width.
Size and shape declarations are stripped before macro expansion because class
files define \footnotesize in terms of itself; expanding it repeated
\@setfontsize internals on every pass. A title page with fancy style is treated
as \thispagestyle{empty}, matching class \maketitle definitions.
The arXiv template now shows "A Preprint" on the right and its short title in
the centre after the title page, with the page number in the footer; tests
cover field parsing, painted positions/alignment and the title-page
suppression.
The geometry package's top/bottom margins were only used to derive a text
height; the laid-out body still started at the very top of the sheet, so the
arXiv template's 1in top margin was ignored and text sat on the paper edge.
Record top/bottom margins on PageGeometry and add the top margin to the page
body's vertical offset, which both the SVG preview and the PDF writer honour;
the page number and footer baseline move with the body. An explicit textheight
still wins over the margin-derived one.
The arXiv template's first baseline now sits at 1in + 12pt, still with zero
warnings in two pages. Unit tests cover the margin parse/derive and a preview
whose first baseline shifts by exactly one inch.
The archived class file expands \maketitle, \section and \keywords into LaTeX
internals that route-D does not model, so the template's title block, headings
and keywords were replaced by LaTeX machinery and 20+ control words reached
layout.
Keep \maketitle, \thanks and the heading commands out of generic macro
expansion so the native title block and section parser see them; scrub the
author metadata (\href labels, ORCID \includegraphics, text-style wrappers,
line breaks and \And) into plain text; turn \keywords into a "Keywords: ..."
paragraph; expand \lipsum[n]/[a-b] to filler paragraphs; drop `%` comments and
external \bibliography/\bibliographystyle commands (an inline thebibliography
still supplies References); and start a new block when center/flushleft/
flushright follows text on the same chunk.
The arXiv fixture now previews with zero warnings in two pages, covered by a
JSON-level regression test. Remaining class support is still semantic rather
than a TeX VM.
The preamble parser is line-oriented, so a multi-line \newcommand, \def or
\newenvironment contributed only its first line to the macro table while the
remaining body lines fell through to the document text. In the assembled arXiv
source that also swallowed the \begin{document} marker, so the whole class
preamble was laid out as one page of \NeedsTeXFormat/\fancyhf text.
Join definition bodies at balanced braces before stripping: comments are
removed, a `%` line continuation joins without adding a space, and a blank line
becomes \par. Definitions now reach the existing single-line capture whole.
Resolve \ifhmode as true and \ifvmode/\ifmmode as false for the text expansion
path, so class macros such as \and expand.
The arXiv fixture body no longer contains NeedsTeXFormat, fancyhf or ifhmode and
the project preview lays out four pages instead of one; unit tests cover a
multi-line \newcommand/\def and the arXiv preamble containment. Remaining
warnings are now body commands (\maketitle, \thanks, \lipsum, \addvspace and the
footnote machinery), which are separate work.
Class files such as the arXiv template set the page block inside
\AtBeginDocument{\newgeometry{...}}. Route-D treated the hook body as document
text, so the setup commands were typeset at the top of page one and the page
measure stayed at its default.
Lift \AtBeginDocument bodies in front of \begin{document}, join a multi-line
\newgeometry argument into one line, and read it through the geometry option
parser. Explicit textheight/textwidth options now win over margins, so
textheight=9in with top=1in keeps 9in instead of recomputing it from the
paper height.
The arXiv project now applies its 6.5in/9in block and no longer reports a
\newgeometry warning; unit tests cover the multi-line hook, the option
precedence, and the no-leak case. A remaining leak from multi-line macro
definitions in the same class file is separate work.
TeX surrounds each display with \abovedisplayskip above and \belowdisplayskip
below, but route-D only reserved the paragraph's own spacing, so displays sat
directly against the surrounding text. Parse the four registers (including the
short forms), default to the article-class 10/11/12pt values, and add them to
the math unit and the unit that follows it.
The glue parser now also accepts the class-file spelling
(`\abovedisplayskip 7\p@ \@plus 2\p@ \@minus 5\p@`), register copies such as
`\belowdisplayskip \abovedisplayskip`, `\z@`/`\p@` dimensions and
space-separated assignments, which clears the display-skip warnings the arXiv
template produced.
A page oracle sets a 45pt above skip and pins the display moving to the second
page exactly as pdfLaTeX lays it out; unit tests cover the defaults, overrides
and the class-file spelling.
Route-D expanded a paragraph into one node per line without TeX's
\clubpenalty/\widowpenalty, so a page break could orphan the first line or
widow the last. Insert the penalties between line nodes (150 by default, read
from the source registers) and make the page builder reject any penalty of
10000 or more as a breakpoint. Forced -10000 values now break exactly where
pdfLaTeX does; two page oracle fixtures compare per-page text, and unit tests
cover the node order and the forbidden breakpoint.
The line-breaker rescanned items[start..end] for every break pair, so a 900-word paragraph took seconds and the 2,700-word edit fixture took about 30 s. Prefix sums over natural width, stretch, shrink, glue order, box count and the last penalty give each candidate its metrics in O(1); the search now costs O(breakpoints^2) and the same document lays out in about 0.3 s. Add a 900-word regression test and note the change in TEX_CORE.