Skip to content

render-pipeline: the amsthm head separator is the package's glue, not a space - #197

Closed
GoKubar wants to merge 1 commit into
mainfrom
agent/linux-primary/amsthm-head-separator-pipeline
Closed

GoKubar wants to merge 1 commit into
mainfrom
agent/linux-primary/amsthm-head-separator-pipeline

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Finishes the second of the two amsthm items #190 left deliberately: the
head→body separator is a sentence space where amsthm uses its own glue
. The
first (the head punctuation's font) is #196, which is independent and also
based on main.

Together they let #190 leave draft: with all three in place the fixture's
theorem heads are exact rather than merely close (numbers below).

What the gap actually is

pdflatex, TeX Live 2025, oracle only, never in the product path.
\documentclass[11pt]{article} + [T1]{fontenc} + amsthm,
\showboxbreadth=100 \showboxdepth=100 \tracingoutput=1. Two different glues,
because proof is not a \newtheorem style:

\newtheorem environments end the head with \hskip\thm@headsep
(amsthm.sty \@begintheorem), and \newskip\thm@headsep \thm@headsep=5pt plus1pt minus1pt — re-asserted by \@thm for every style, so all three of
plain/definition/remark get it:

\T1/cmr/bx/n/10.95 .          % \the\thm@headpunct
\glue 5.0 plus 1.0 minus 1.0  % \hskip\thm@headsep
\T1/cmr/m/n/10.95 L e t       % the body

It is absolute — 5.0 pt at 10pt, 11pt and 12pt alike, unlike \labelsep
below — and it is glue, not a kern: stretch 1.0, shrink 1.0 are both there
and both matter on a line that is set loose.

proof is \item[\hskip\labelsep \itshape #1\@addpunct{.}], an ordinary
\trivlist item, so the glue after the head is the \hskip\labelsep that
\@item appends after the label box, and it is rigid:

\hbox(7.60416+2.12917)x67.05003     % \box\@labels
.\glue 0.0                          % \hskip\itemindent
.\glue 0.0                          % \hskip-\labelwidth  (\trivlist)
.\glue -5.475                       % \hskip-\labelsep
.\hbox(7.60416+2.12917)x67.05003    % \makelabel
..\glue 5.475                       %   \hskip\labelsep
..\OT1/cmr/m/it/10.95 P r o o f … .
.\glue 5.475                        % \@item's trailing \hskip\labelsep
\penalty 0
\OT1/cmr/m/n/10.95 A                % the body

\showthe\labelsep prints 5.0pt / 5.475pt / 5.87494pt at 10 / 11 / 12pt
(article's .5em at class-load size — note 12pt is not 6.0), which is
exactly what flashtex_document_style::list_level(...).labelsep already
resolves, so this PR reads it from there rather than inventing a constant.

What we emitted instead 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. Wrong width, and more than five
times the stretch.

The change

New src/amsthm.rs. The head is recognised from the compiler's own output,
not 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, so a block whose first inline is a Text whose span covers
exactly \begin and whose text is not those bytes is a theorem-like \item.
It returns the glue and the byte the head ends at (past {<env>} and any
[<note>]).

items_from_inlines_styled then emits that glue in place of the gap before
the first inline at or after that byte. In place of, because the head ends with
\ignorespaces: \begin{theorem}Body, \begin{theorem}\n Body and
\begin{theorem}\label{k}\n Body must all give the same head, and they do —
\label's \@bsphack/\@esphack takes the same glue off and puts it back with
\ignorespaces (a Inline::Label contributes no gap, so the glue simply lands
in front of the next real item).

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

This needs nothing new from crates/compiler, so it does not wait on a
vendor/ re-pin. It also merges cleanly with #190 (verified locally: git merge of both, no conflict) — #190 adds theorem_item/in_theorem to the same
function and the two do not overlap.

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. amsmath oracle corpus 59/59 within 0.5 bp.

cargo test --release --no-fail-fast on crates/render-pipeline, with the
font environment: 187 passed / 8 failed here; the origin/main control of
the same base is 181 / 10, and the two failing sets are identical apart from
the two new tests in this PR, which fail on the control because they measure
exactly what it does not do yet:

cm_less_symbols_are_painted_from_latin_modern_math
font_family_fixtures_match_pdflatex
footnotes_against_pdflatex
mathbb_paints_from_new_computer_modern_when_bundled
mathcal_sets_at_cmsy_metrics_and_paints_from_new_computer_modern_when_bundled
missing_required_metrics_are_a_blocking_diagnostic_not_a_silent_fallback
page_frame_against_pdflatex
varnothing_sets_at_msbm_width_and_paints_from_new_computer_modern_when_bundled

(+6 passing = 2 new integration tests + 4 new amsthm unit tests. Several of
those 8 are the known partial-font control tests that a global
FLASHTEX_TFM_DIRS contaminates, which is why the set, not the count, is the
signal, and the same 8 fail with or without FLASHTEX_LM_DIR exported — the
env spelling CI uses.)

lecture-notes word geometry against the committed reference.pdf
(difflib-aligned words, |dx| in bp, 419 aligned words and max |dx| 112.737 in
every row — the pre-existing math-run/extraction outliers):

build median |dx| ≤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 + #196 0.0048 235 284 339 38/56

(Line counts are this script's own grouping — words grouped by page and
baseline, compared with difflib — so 56 rather than #190's 51; the point is
that no line is lost in any combination, and none is gained: the residual
line differences are the extraction artifacts #190 documented.)

On the fixture's 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 #196 as well 155.3744 (+0.0027) — and the rest of that
line then lands inside 0.06 bp. Two errors of opposite sign again, exactly as
#190 warned; neither of these two PRs should be judged alone on this fixture.

No committed oracle data was regenerated — this box is TeX Live 2025, not
MacTeX. Nothing under crates/render-pipeline/vendor/ is committed.

Merge order

#190 → this PR gives the fixture's theorem heads their correct glue;
#196 (compiler) needs a vendor/compiler re-pin after it merges before its
half is visible here. All three are based on main and none conflicts with
another.

Left

  • the proof head is a box in TeX (\box\@labels), so the interword space
    inside Proof sketch. does not stretch with the line; ours does. Worth
    ~1.4 pt on a loose line and not addressed here.
  • \penalty 0 after \box\@labels — our rigid glue is already a legal
    breakpoint of the same cost, so the difference is only which side keeps the
    5.475 pt if a break ever lands there.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

… 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 added a commit that referenced this pull request Sep 14, 2026
The head glue was dropped whenever the body was glued to the `\begin`.

`pending_head_sep` is drained only by `push_gap`, and five of that closure's
twelve callers -- the ordinary text arm among them -- call it only inside
`if has_space`. That is fine for a *replacement*: if the source had a space,
the head glue goes in instead of it. But amsthm's head ends with
`\ignorespaces`, so `\begin{theorem}Body` has no source gap at all, nothing
calls `push_gap`, and the separator is silently discarded. The measured gap
between `Theorem 1.` and the body was 0.00 bp against pdflatex's
`\glue 5.0 plus 1.0 minus 1.0`.

The guard is now `has_space || pending_head_sep.get().is_some()`, so the glue
is emitted on its own when there is no space to substitute for. The three
spellings the module documents now agree exactly, all at `bp(5.0)` =
4.98132 bp:

    \begin{theorem}Body
    \begin{theorem}\n Body
    \begin{theorem}\label{k}\n Body

This is latent in #197 rather than introduced by the rebase: nothing on its
base exercised the no-gap path, because the adapter still invented an
interword space after the `\begin` control word there. `main` stopped doing
that, which is correct, and left the separator with nothing to replace.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
GoKubar added a commit that referenced this pull request Sep 14, 2026
render-pipeline: the amsthm head separator is the package's glue, not a space (rebase of #197 + the dropped-separator bug its tests found)
@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded — the work is already on main.

Replacement: 8a2b21a same title (PR #267)

Closed by the integration lane while clearing a 219-PR backlog (112 of them drafts). If this is wrong, reopen it and tell me — I would rather be corrected than leave stale work buried.

— orchestrator-astra (Commander)

@GoKubar GoKubar closed this Sep 15, 2026
@GoKubar
GoKubar deleted the agent/linux-primary/amsthm-head-separator-pipeline branch September 16, 2026 04:18
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