Skip to content

render-pipeline: amsthm theorem-like environments are a \trivlist \item (lecture-notes line breaks) - #190

Merged
GoKubar merged 2 commits into
mainfrom
agent/linux-primary/lecture-notes-trivlist-breaks
Sep 13, 2026
Merged

GoKubar merged 2 commits into
mainfrom
agent/linux-primary/lecture-notes-trivlist-breaks

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes the remaining line-break differences in
fixtures/real-world/lecture-notes against pdflatex.

Re-measured after merging current main (31be6f9e). The lane was briefed
on three differences; main now has four — the Proof. Existence.
paragraph has drifted too (we push "take" down a line). The same fix closes
all four, and the numbers below are the current-main ones.

None of them is a breaker bug. Our Knuth–Plass implementation agrees
with TeX's own feasible breakpoints once it is fed the same horizontal list;
what differed was the content of the theorem lines. Two content errors were
partly cancelling each other — +17pt of spurious indent against ~8.4pt of
missing head width — which is why only 3 of 45 lines showed it, and why fixing
either one alone makes the fixture worse (measured: indent-only goes 44/51 →
46/51 but breaks the Example 1.2 line that used to match).

Method

pdflatex as an oracle only, TeX Live 2025, never in the product path.
\tracingparagraphs=1 on the whole fixture, \showbox on the paragraphs of
interest, \showthe for the individual dimensions, plus a temporary
TeX-format trace in our own breaker (reverted; nothing under
crates/render-pipeline/vendor/ is committed).

Every paragraph of this document is resolved in @firstpass — no second pass,
no hyphenation, no \emergencystretch. So \tolerance, hyphenation and the
emergency pass were never in play.

The cases

3. "Proof sketch. Run the Euclidean algorithm…" (prose only)

TeX's trace finds exactly one feasible break on line 1, after integer, at
b=0 p=0 d=100. We break after "is an" instead. The oracle line is
66.56969 + 404.93347 = 471.50316pt against \hsize 469.75502pt — it
shrinks by 1.74814pt, r = −0.124, badness 0. We had 17pt less room, so
"integer" could not fit at all.

\trivlist leaves \itemindent at \z@ (\showthe\itemindent inside both
proof and theorem prints 0.0pt), and \@item's \everypar takes the
\parindent box back off the first line (\setbox\z@\lastbox) before
unboxing \@labels, whose leading \hskip\itemindent \hskip-\labelwidth \hskip-\labelsep is -\labelsep here and exactly cancels the
\hskip\labelsep the head box begins with. The head sits flush on the left
margin: the oracle puts Proof at x=72.000, we put it at 88.936
(= +17pt). Only the following paragraphs of the environment take the
ambient \parindent — the oracle confirms this too (Uniqueness. at
x=88.936), and we already agreed there.

2. "…as in Theorem 2.1. Then", and 4. "The set is non-empty take"

Same mechanism (case 4 is the one that appeared on main after the brief). TeX has two feasible breaks: after Theorem 2.1. at b=19 d=841 and after Then at b=3 d=169, and takes the second (t=269 vs
941). With 17pt less measure the Then break is not feasible for us at
all.

1. "…with $d \mid a$ and $d \mid b$. We"

Not the indent — this paragraph follows a \section, so both sides already
start at x=72. TeX's two candidates are the break after the period
(b=2 d=144, stretching, glue set 0.2667) and the break after We
(b=4 d=196, shrinking), and it takes the cheaper first one for t=244.
We computed b=7 and b=0 and took the second.

\showbox gives the oracle's line exactly: natural 456.03478pt, stretch
51.44446pt. Ours was natural 447.32712, stretch 53.80032 — a constant
8.70766pt too narrow, identical at both candidate breaks, i.e. one item, not
a per-space error. Component-by-component against \showthe\wd:

piece pdflatex ours (before)
head box Definition 3.1. 77.85793 69.41731
For, not, both, zero,, is, the, largest, with, and, We — match to 2e-5 pt
$a, b \in \Z$ 36.03732 36.03000
$\gcd(a, b)$ 40.29578 40.20281
$d \mid a$ / $d \mid b$ 20.61227 / 19.52359 match

So the head box alone accounts for 8.44062 of the 8.70766. Two causes:

  • the head was set from the regular face. amsthm declares the head font
    (\bfseries; \itshape for remark/proof) and the plain style's italic
    body — none of it appears in the source at the head's span, which is the
    \begin command itself, so style_at read the enclosing style. The whole
    theorem, head and body, came out in LMRoman10-Regular.
  • the blank inside the synthesised head text was shaped as a character —
    the T1 slot-32 glyph, 5.44394pt, rigid — rather than the space token it
    stands for (4.17043pt plus 2.08443 minus 1.3896 in the bold face).

Changes

  1. theorem_environments / opens_theorem_item: a paragraph that opens a
    \newtheorem-declared environment or proof is that environment's
    \item, so it gets no first-line indent.
  2. in_theorem_environment: inside such an environment, weights come from the
    compiler's own scoping — the existing compiler_weight path, threaded
    through items_cached and its cache key.
  3. A blank in non-exact (synthesised / macro replacement) text becomes
    interword glue instead of a character. Scoped by the exact flag that was
    already computed, so the source's own bytes — verbatim included — keep a
    literal blank.

New tests/theorem_items.rs pins the flush head, the oracle's bold head width
(\hbox{\bfseries Definition} = 54.2265pt; we now produce 54.22656), the
head being two runs rather than one run holding a space character, and the
second paragraph of the environment still taking \parindent.

Gates

All on this NixOS host with the bundled apps/mac/Fonts and explicit
FLASHTEX_TFM_DIRS; no font diagnostic on any run (a fallback run would
change every break, so each was checked).

gate result
lecture-notes breaks vs oracle 39/51 → 48/51; all four real differences fixed, none introduced
lecture-notes overfull 0
lecture-notes geometry vs reference.pdf mean |dx| 17.4911 → 1.9871 bp, median 7.0431 → 0.1022, within 0.5 bp 118 → 260
HW1, HW2 display lists byte-identical to origin/main
cv, inline-math, input-bibliography, letter, math-sheet, unicode-accents byte-identical
article-twocolumn no break moves; median |dx| unchanged (0.0042 bp), within 0.5 bp 201 → 203
amsmath oracle corpus 59/59 within 0.5 bp
cargo test --release --no-fail-fast, render-pipeline, with fonts 170 passed / 8 failed vs origin/main control 169 / 8 — identical failing set, +1 = the new test

All three residual lines in the 48/51 are text-extraction artifacts with the
same words on the same line, not break differences: q′/r′ primes on two
page-1 lines (the oracle's prime glyph reads as 0, ours carries no ASCII
text) and the pre-existing B\'ezout accent composition.

The control was an origin/main checkout of adapter.rs in this same
worktree, per the coordinator's note that the ~8 "with fonts" failures are
partial-font control tests contaminated by the global env rather than ordinary
pre-existing breakage; the signal is the identical failing set.

Left deliberately, with numbers

Neither moves a break anywhere in this corpus, and both belong to amsthm
rendering rather than line breaking:

  • the head's trailing punctuation is emitted by the compiler with the
    default style (begin_theorem pushes "." as TextStyle::default()) where
    amsthm sets it in the head font — pdflatex shows
    \T1/cmr/bx/n/10.95 .. crates/compiler is vendored here, so this is the
    compiler owner's call.
  • the head-to-body separator is currently a sentence space
    (4.83948 plus 5.44014 minus 0.40297, because the head ends in .) where
    amsthm uses the \newtheoremstyle headspace — \showbox gives
    \glue 5.0 plus 1.0 minus 1.0 — and proof, which is not a
    \newtheorem style, uses \hskip\labelsep = 5.475pt, rigid (the oracle
    puts Run at x=138.326, i.e. 66.56969pt from the margin, which is
    exactly \labelsep + \wd{\itshape Proof sketch.} = 5.475 + 61.09468).

Draft because the amsthm head is now correct enough for every break in the
corpus but not yet exact; the two items above are the remaining known gap.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

The three remaining line-break differences in fixtures/real-world/lecture-notes
are not breaker bugs. Both sides of each break are decided by the *content* of
the theorem line, and two content errors were partly cancelling each other
(+17pt of spurious indent against ~8.4pt of missing head width), which is why
only 3 of 45 lines showed it.

Measured against pdflatex (TeX Live 2025, 11pt article, T1 ecrm1095/ecbx1095 --
the same TFMs this crate reads), with \tracingparagraphs=1 and \showbox:

1. `\trivlist` leaves `\itemindent` at `\z@` (`\showthe\itemindent` inside
   `proof` and `theorem` both print `0.0pt`), and `\@item`'s `\everypar` takes
   the `\parindent` box back off the first line before unboxing `\@labels`,
   whose leading `\hskip\itemindent \hskip-\labelwidth \hskip-\labelsep` is
   `-\labelsep` here and exactly cancels the `\hskip\labelsep` the head box
   starts with. The head therefore sits flush on the left margin: the oracle
   PDF puts "Proof" at x=72.000, we put it at 88.936 (= +17pt = \parindent).
   Only the *following* paragraphs of the environment take the ambient
   `\parindent`, which the oracle confirms ("Uniqueness." at x=88.936).

2. The head font (`\bfseries` for the `plain`/`definition` styles, `\itshape`
   for `remark`/`proof`) and the `plain` style's italic body are declared by
   amsthm, not written in the source at the head's span -- which is the
   `\begin` command itself -- so `style_at` read the enclosing (regular) style
   and the whole theorem was set from the regular face.
   `\hbox{\bfseries Definition 3.1.}` is 77.85793pt; we produced 69.41731pt.
   Weights now come from the compiler's own scoping inside a theorem-like
   environment (the existing `compiler_weight` path, threaded through
   `items_cached` and its key).

3. The blank inside synthesised head text ("Definition 3.1") was shaped as a
   character -- the T1 slot-32 glyph, 5.44394pt and rigid -- instead of the
   space *token* it stands for. It is now interword glue in the font in force,
   which for the bold head is `4.17043pt plus 2.08443 minus 1.3896`, exactly
   pdflatex's. Scoped by the existing `exact` flag, so the source's own bytes
   (verbatim included) keep a literal blank.

Evidence, all on this NixOS host with the bundled fonts and no font
diagnostics on any run:

* lecture-notes: all three break differences gone, no new ones. Line-by-line
  against the oracle PDF 44/51 -> 50/51; the single residual is the
  pre-existing `B\'ezout` accent-composition artifact, which puts the same
  words on the same line. 0 overfull. Word geometry vs the reference PDF:
  mean |dx| 11.6194 -> 1.4566 bp, median 2.0880 -> 0.0950 bp, words within
  0.5 bp 137 -> 263, max 452.94 -> 102.20.
* HW1 and HW2 display lists byte-identical to origin/main, as are cv,
  inline-math, input-bibliography, letter, math-sheet and unicode-accents.
* article-twocolumn: no line break moves; the `\cite` separator becomes glue
  instead of the slot-32 glyph (the oracle sets "[1,2]." with no gap at all,
  since the `cite` package compresses it, so glue is the closer of the two).
  Median |dx| unchanged at 0.0042 bp, words within 0.5 bp 201 -> 203.
* amsmath oracle corpus: 59/59 within 0.5 bp.
* `cargo test --release --no-fail-fast` on crates/render-pipeline WITH fonts
  configured: 163 passed / 8 failed, against an origin/main control in the
  same worktree at 162 passed / 8 failed -- the identical failing set (the
  partial-font control tests contaminated by the global env), plus the new
  test here.

Known deviations left, neither of which moves a break in this corpus: the
head's trailing punctuation is emitted by the compiler with the default style
where amsthm sets it in the head font, and the head-to-body separator is a
sentence space where amsthm uses its `headspace` (`5pt plus1pt minus1pt` for
`\newtheorem` styles) and `proof` uses `\hskip\labelsep` (5.475pt, rigid).

Implementation-Agent: engine-engineer (Claude Opus 5), linux-primary
Commit-Executor: engine-engineer (Claude Opus 5), direct git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar
GoKubar marked this pull request as ready for review September 13, 2026 23:36
@GoKubar
GoKubar merged commit 9854fcc into main Sep 13, 2026
16 of 20 checks passed
GoKubar added a commit that referenced this pull request Sep 14, 2026
… a space

The gap between a theorem-like head and its body was an interword space read
from the source newline after `\begin{...}` — and because the head ends in
`.`, a *sentence* space (`4.83948 plus 5.44014 minus 0.40297` at 11pt).
amsthm puts its own glue there and then `\ignorespaces`.

Measured against pdflatex (TeX Live 2025, oracle only, never in the product
path), `\documentclass[11pt]{article}` + `[T1]{fontenc}` + amsthm with
`\tracingoutput=1`:

* a `\newtheorem` environment's head ends with `\hskip\thm@headsep`
  (amsthm.sty `\@begintheorem`), `\thm@headsep = 5\p@ plus\p@ minus\p@`.
  The shipped page: `\T1/cmr/bx/n/10.95 .`, `\glue 5.0 plus 1.0 minus 1.0`,
  then the body. It is absolute — the same 5pt at 10, 11 and 12pt — and it
  stretches, which a sentence space's five-times-larger stretch does not
  reproduce on a line that is set loose;
* `proof` is not a `\newtheorem` style but a plain `\trivlist` `\item
  [\hskip\labelsep \itshape #1\@addpunct{.}]`, so the glue after its label
  is the `\hskip\labelsep` `\@item` appends after the label box, rigid:
  `\hbox(7.60416+2.12917)x67.05003` then `\glue 5.475` then `\penalty 0`.
  `\showthe\labelsep` prints 5.0pt / 5.475pt / 5.87494pt at 10 / 11 / 12pt,
  which is what `flashtex_document_style::list_level` already resolves.

New `src/amsthm.rs` recognises the head from the compiler's own output
rather than from a `\newtheorem` scan: `parser::begin_theorem`/`begin_proof`
are the only places that give a synthesised `Inline::Text` the span of a
`\begin` control word. It returns the glue and the byte the head ends at
(past `{<env>}` and any `[<note>]`); `items_from_inlines_styled` emits that
glue in place of the gap before the first inline at or after it, so the
source's own whitespace is dropped exactly as `\ignorespaces` drops it —
`\begin{theorem}Body`, `\begin{theorem}\n Body` and
`\begin{theorem}\label{k}\n Body` all give the same head (`\label`'s
`\@esphack` takes the glue off and puts it back with `\ignorespaces`).

`Item::HSpace` grows `stretch_pt`/`shrink_pt` and becomes `Glue::finite`
rather than `Glue::fixed`; `\hspace` keeps passing 0/0 and is unchanged.

## Gates

NixOS host, bundled `apps/mac/Fonts` with the explicit `FLASHTEX_TFM_DIRS`
spelling that reaches the rooted metrics reader; never the system TeX Live.

Real-world corpus, v2 display lists against an `origin/main` control: nine of
ten fixtures **byte-identical** (hw1, hw2, cv, inline-math,
input-bibliography, letter, math-sheet, unicode-accents, article-twocolumn),
`lecture-notes` the only one that moves. Its diagnostics are byte-identical
too (8, none of them a font substitution, 0 overfull); every status and page
count unchanged.

`lecture-notes` word geometry against the committed `reference.pdf`
(difflib-aligned words, |dx| in bp). The head is only *exact* with all three
parts in place, and the errors partly cancel, so every combination is
measured:

| build | median &#124;dx&#124; | ≤0.01 bp | ≤0.1 bp | ≤0.5 bp | lines |
|---|---|---|---|---|---|
| `main` | 7.6051 | 116 | 125 | 138 | 32/56 |
| + this PR | 7.2493 | 116 | 125 | 138 | 32/56 |
| + #190 | 0.1366 | 174 | 199 | 285 | 38/56 |
| + #190 + this PR | 0.0305 | 199 | 245 | 340 | 38/56 |
| + #190 + this PR + the compiler PR | **0.0048** | **235** | **284** | 339 | 38/56 |

No line is lost in any combination, and the 419 aligned words and the
`max |dx|` (112.737 bp, the pre-existing math-run/extraction outliers) are
the same throughout.

On the fixture's own `Definition 3.1.` line, body start against the oracle's
155.3717 bp: `main` + #190 155.8772 (+0.5055), with this PR 154.9534
(−0.4183), with the compiler PR as well **155.3744 (+0.0027)** — and the
whole rest of that line then lands inside 0.06 bp.

`cargo test --release --no-fail-fast` on `crates/render-pipeline`, with the
font environment: see the PR description for the failing-set comparison
against an `origin/main` control of the same base.

Implementation-Agent: engine-engineer (Claude Opus 5), linux-primary
Commit-Executor: engine-engineer (Claude Opus 5), direct git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar
GoKubar deleted the agent/linux-primary/lecture-notes-trivlist-breaks branch September 16, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant