Skip to content

render-pipeline: an amsthm theorem is a \trivlist — it has \topsep - #480

Merged
GoKubar merged 2 commits into
mainfrom
agent/nixos-cv-lecture/amsthm-topsep
Sep 15, 2026
Merged

GoKubar merged 2 commits into
mainfrom
agent/nixos-cv-lecture/amsthm-topsep

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

render-pipeline: an amsthm theorem is a \trivlist — it has \topsep

The pipeline knew amsthm's head separator (crate::amsthm) and that a
theorem's first line is not indented (adapter::opens_theorem_item), but gave
the environment no vertical space at all. Every \begin{theorem} and every
\end{theorem} was one \topsep too tight — 8 / 9 / 10 pt at a 10 / 11 / 12pt
base.

fixtures/real-world/lecture-notes has eight such boundaries on page 1 alone,
and its words were a staircase reaching −49.6 bp against the committed
reference: 9.65% of aligned words within 0.5 bp on dy.

The mechanism, from the oracle

pdfTeX 3.141592653-2.6-1.40.27 (TeX Live 2025), \showoutput, between the last
line of one theorem and the first line of the next:

...\hbox(8.2125+2.73749)x469.75502        % last line of the definition
...\penalty -51
...\glue 9.0 plus 3.0 minus 5.0           % \@endparenv's \@topsepadd
...\glue -11.73749 plus -3.0 minus -5.0   % \endtrivlist: -(that + depth)
...\penalty -51
...\glue 2.73749                          % + the depth back
...\glue 9.0 plus 3.0 minus 5.0           % \@item's \@topsep for the next
...\glue(\parskip) 0.0 plus 1.0
...\glue(\parskip) 0.0
...\glue(\baselineskip) 2.65002
...\hbox(8.2125+2.73749)x469.75502        % first line of the lemma

The four explicit glues net to one \topsep: \addvspace keeps the larger
of the new skip and \lastskip, it does not add them. So the separation is
\baselineskip + \topsep = 22.6 pt at 11pt, 20.0 at 10pt, 24.5 at 12pt.

The value is \topsep alone — never \partopsep, never \parskip — because
\@thm assigns \@topsep\thm@preskip and \@topsepadd\thm@postskip outright
(amsthm.sty 140–141) and \thm@space@setup sets both to \topsep. The
\@trivlist derivation that center/quote go through never runs for a
theorem. proof is the exception: it is a plain \trivlist opened from \par
under amsthm's own \topsep6\p@\@plus6\p@, so its closing \@topsepadd is
6pt + \partopsep (8.0 plus 7.0 minus 1.0, 9.0 plus 7.0 minus 1.0,
9.0 plus 8.0 minus 2.0 at 10 / 11 / 12pt).

EnvOpen therefore gains an optional EnvSkips: an environment that sets
\@topsep/\@topsepadd itself carries them, and the existing center/quote
machinery — which already models \addvspace's max on the opening side —
does the rest. env_close for a theorem is set from the \end{<theorem>} that
actually closed it, tracked as the unit loop runs so the later toc_lists
splice cannot shift the index.

\addvspace does not sum

Closing an environment used to add its \@endparenv skip to whatever the
block already left. That was invisible while nothing was ever left there, and
wrong the moment a theorem ended in a display: lecture-notes page 1 again,

...\hbox(7.60416+2.12917)..., display
...\penalty 0
...\glue(\belowdisplayskip) 11.0 plus 3.0 minus 6.0
...\glue -11.0 plus -3.0 minus -6.0       % \@endparenv takes it off
...\glue 11.0 plus 3.0 minus 6.0          % and puts back the larger

net 11.0 pt, not 11.0 + 9.0.

This takes the max only for an environment that declares its own skips. For the
rest, space_after is not necessarily \lastskip: the abstract head's
\vspace{-.5em} reaches the page through \vadjust, before the penalty and
the closing skip, so \addvspace cannot see it and the two genuinely do add
(SizedPara::vspace_after_em). Taking the max there cost hyperref-toc,
lab-report, natbib-review and plain-article 25–40 points of dy in an
intermediate state of this branch; the guard that caught it is the second test
below. Separating the two for every environment needs VBlock to carry
\vadjust vspace apart from glue — a change of its own, not made here.

Effect, against the committed reference PDFs

lecture-notes   dy   9.65% -> 61.41%   max |dy| 49.6 bp -> 1.08 bp
                dx  77.74% unchanged   pages 2/2
corpus          dy  84.91% -> 86.74%   dx 89.89% unchanged, pages 22/22

No other fixture's geometry moves. lecture-notes page 2 is now exact (max
|dy| 0.008 bp); the ≤1.08 bp left on page 1 is not a spacing mechanism but the
page-shrink ratio: pdflatex packs that page with glue set - 0.30122 and we
land near −0.276, so each \topsep differs by 0.124 bp and each proof close
by 0.025 — the 5:1 of their minus 5.0 / minus 1.0. Closing it means finding
the ~1.5 pt of natural height page 1 is still short of pdflatex's, whose
remaining candidates are the \tableofcontents block and the \section
before-skips.

Test: crates/render-pipeline/tests/amsthm_topsep.rs, every expected number
quoted from the trace above at all three class sizes. On main it says

10pt paragraph -> definition: 11.9552 bp, pdflatex 19.9253 bp
(off by -7.9701 bp)

pdflatex is an oracle only and never runs in the product path.

Gates: cargo test --release --no-fail-fast on crates/render-pipeline, 366
passed 0 failed; amsmath 59/59 within 0.5 bp; tabular 106/106; HW1 and HW2 3/3
pages, 0 errors, 0 overfull; corpus page counts 22/22 read from the reference
PDFs; zero font-FAILURE diagnostics.

Implementation-Agent: Claude Opus 5 (lane nixos-cv-lecture, linux-primary)
Commit-Executor: Claude Opus 5 (direct git, observed Cursor usage limit)
Co-Authored-By: Claude Opus 5 [email protected]
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

The pipeline knew amsthm's head separator (`crate::amsthm`) and that a
theorem's first line is not indented (`adapter::opens_theorem_item`), but gave
the environment no vertical space at all. Every `\begin{theorem}` and every
`\end{theorem}` was one `\topsep` too tight — 8 / 9 / 10 pt at a 10 / 11 / 12pt
base.

`fixtures/real-world/lecture-notes` has eight such boundaries on page 1 alone,
and its words were a staircase reaching −49.6 bp against the committed
reference: 9.65% of aligned words within 0.5 bp on `dy`.

## The mechanism, from the oracle

pdfTeX 3.141592653-2.6-1.40.27 (TeX Live 2025), `\showoutput`, between the last
line of one theorem and the first line of the next:

    ...\hbox(8.2125+2.73749)x469.75502        % last line of the definition
    ...\penalty -51
    ...\glue 9.0 plus 3.0 minus 5.0           % \@endparenv's \@topsepadd
    ...\glue -11.73749 plus -3.0 minus -5.0   % \endtrivlist: -(that + depth)
    ...\penalty -51
    ...\glue 2.73749                          % + the depth back
    ...\glue 9.0 plus 3.0 minus 5.0           % \@item's \@topsep for the next
    ...\glue(\parskip) 0.0 plus 1.0
    ...\glue(\parskip) 0.0
    ...\glue(\baselineskip) 2.65002
    ...\hbox(8.2125+2.73749)x469.75502        % first line of the lemma

The four explicit glues net to **one** `\topsep`: `\addvspace` keeps the larger
of the new skip and `\lastskip`, it does not add them. So the separation is
`\baselineskip` + `\topsep` = 22.6 pt at 11pt, 20.0 at 10pt, 24.5 at 12pt.

The value is `\topsep` alone — never `\partopsep`, never `\parskip` — because
`\@thm` assigns `\@topsep\thm@preskip` and `\@topsepadd\thm@postskip` outright
(amsthm.sty 140–141) and `\thm@space@setup` sets both to `\topsep`. The
`\@trivlist` derivation that `center`/`quote` go through never runs for a
theorem. `proof` is the exception: it is a plain `\trivlist` opened from `\par`
under amsthm's own `\topsep6\p@\@Plus6\p@`, so its *closing* `\@topsepadd` is
6pt + `\partopsep` (`8.0 plus 7.0 minus 1.0`, `9.0 plus 7.0 minus 1.0`,
`9.0 plus 8.0 minus 2.0` at 10 / 11 / 12pt).

`EnvOpen` therefore gains an optional `EnvSkips`: an environment that sets
`\@topsep`/`\@topsepadd` itself carries them, and the existing `center`/`quote`
machinery — which already models `\addvspace`'s max on the opening side —
does the rest. `env_close` for a theorem is set from the `\end{<theorem>}` that
actually closed it, tracked as the unit loop runs so the later `toc_lists`
splice cannot shift the index.

## `\addvspace` does not sum

Closing an environment used to *add* its `\@endparenv` skip to whatever the
block already left. That was invisible while nothing was ever left there, and
wrong the moment a theorem ended in a display: `lecture-notes` page 1 again,

    ...\hbox(7.60416+2.12917)..., display
    ...\penalty 0
    ...\glue(\belowdisplayskip) 11.0 plus 3.0 minus 6.0
    ...\glue -11.0 plus -3.0 minus -6.0       % \@endparenv takes it off
    ...\glue 11.0 plus 3.0 minus 6.0          % and puts back the larger

net 11.0 pt, not 11.0 + 9.0.

This takes the max only for an environment that declares its own skips. For the
rest, `space_after` is not necessarily `\lastskip`: the abstract head's
`\vspace{-.5em}` reaches the page through `\vadjust`, *before* the penalty and
the closing skip, so `\addvspace` cannot see it and the two genuinely do add
(`SizedPara::vspace_after_em`). Taking the max there cost `hyperref-toc`,
`lab-report`, `natbib-review` and `plain-article` 25–40 points of `dy` in an
intermediate state of this branch; the guard that caught it is the second test
below. Separating the two for every environment needs `VBlock` to carry
`\vadjust` vspace apart from glue — a change of its own, not made here.

## Effect, against the committed reference PDFs

    lecture-notes   dy   9.65% -> 61.41%   max |dy| 49.6 bp -> 1.08 bp
                    dx  77.74% unchanged   pages 2/2
    corpus          dy  84.91% -> 86.74%   dx 89.89% unchanged, pages 22/22

No other fixture's geometry moves. `lecture-notes` page 2 is now exact (max
|dy| 0.008 bp); the ≤1.08 bp left on page 1 is not a spacing mechanism but the
page-shrink ratio: pdflatex packs that page with `glue set - 0.30122` and we
land near −0.276, so each `\topsep` differs by 0.124 bp and each `proof` close
by 0.025 — the 5:1 of their `minus 5.0` / `minus 1.0`. Closing it means finding
the ~1.5 pt of natural height page 1 is still short of pdflatex's, whose
remaining candidates are the `\tableofcontents` block and the `\section`
before-skips.

Test: `crates/render-pipeline/tests/amsthm_topsep.rs`, every expected number
quoted from the trace above at all three class sizes. On main it says

  10pt paragraph -> definition: 11.9552 bp, pdflatex 19.9253 bp
  (off by -7.9701 bp)

pdflatex is an oracle only and never runs in the product path.

Gates: `cargo test --release --no-fail-fast` on `crates/render-pipeline`, 366
passed 0 failed; amsmath 59/59 within 0.5 bp; tabular 106/106; HW1 and HW2 3/3
pages, 0 errors, 0 overfull; corpus page counts 22/22 read from the reference
PDFs; zero font-FAILURE diagnostics.

Implementation-Agent: Claude Opus 5 (lane nixos-cv-lecture, linux-primary)
Commit-Executor: Claude Opus 5 (direct git, observed Cursor usage limit)
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
# Conflicts:
#	crates/render-pipeline/src/adapter.rs
@GoKubar
GoKubar merged commit a2ae138 into main Sep 15, 2026
21 checks passed
@GoKubar
GoKubar deleted the agent/nixos-cv-lecture/amsthm-topsep 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