Skip to content

render-pipeline: theorem layout like latex.ltx and amsthm (oracle 36/43; needs #149 re-pin) - #156

Closed
GoKubar wants to merge 1 commit into
mainfrom
agent/kabir-claude/theorem-fidelity
Closed

GoKubar wants to merge 1 commit into
mainfrom
agent/kabir-claude/theorem-fidelity

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Theorem environments in the main flow, checked against pdflatex (MacTeX 2026). This PR is based on main. Depends on #149 (compiler): it reads the new Parsed::theorems and Parsed::qed_marks, so it builds only after vendor/compiler is re-pinned to #149. No vendor edits here. I verified it with an uncommitted swap.

Fixes (render-pipeline only)

  • Heads come from the compiler record, not from source bytes.
    • Kernel \@begintheorem: a rigid label box {\bfseries Name Number (Note)}, then \hskip\labelsep and \penalty0.
    • amsthm \deferred@thm@head, unboxed into the paragraph: \thm@indent, name, number, the note in \thm@notefont, \thm@headpunct, then \thm@headsep glue, { } or \newline.
    • \swapnumbers.
    • \@upn's italic correction before the space (latex.ltx \check@icl / \sw@slant). This fixes italic heads such as remark and \itshape styles.
  • Body font is applied to body words and spaces. The head paragraph is not indented.
  • Vertical glue:
    • Before: \addvspace\@topsep then \addvspace{-\parskip}. That is amsthm \thm@preskip; kernel \topsep+\parskip, plus \partopsep from vertical mode; proof 6pt plus 6pt+\partopsep.
    • After: \@endparenv's \@topsepadd.
    • \@xaddvskip replaces a skip only when the new one is strictly larger.
    • Stretch and shrink go to the page builder.
    • After a heading, \@nbitem applies. A kernel theorem leaves \@endpetrue.
  • QED:
    • amsthm \qed is set as \unskip\penalty9999 \hbox{}\nobreak\hfill\quad. The \openbox is drawn as four 0.4pt rules (new BoxRec::Rules).
    • \qedhere works in text and in lists.
    • \qedhere in \[ or equation* is placed as \eqno (amsmath \displaymath@qed).
  • \noindent now also applies before a font command's opening, as in \noindent\textit{Sketch.}.

Oracle

  • tests/theorem_corpus: 43 fixtures and a pdflatex word+rule generator (oracle.py refs|check). Cargo never runs TeX.
  • Result: 36/43 within 0.5 bp for words and QED rules (main: 0/43). tests/theorem_oracle.rs gates the 36.
  • Pending (listed in the test):
    • List lane (render-pipeline: list layout fidelity vs pdflatex (list oracle 28/39) #143), 3 fixtures: the bullet label is 2.8–3.1 bp off, and 12pt list indents are 0.62 bp off.
    • Display lane, 1 fixture: the same display is 1 bp off without a theorem.
    • A theorem that opens with a list, 1 fixture: the head joining the first label (\@donoparitem) is not implemented.
    • Page builder, 1 fixture: pdflatex shrinks the theorem gaps and fits 41 lines on page 2; we fit 39. Two attempts did not change it.
    • A renewed \qedsymbol with math, 1 fixture: the compiler still emits U+220E.

Gates

All run with #149 swapped in.

  • render-pipeline cargo test --release: all green.
  • clippy: the warning counts are equal to main's.
  • HW1/HW2: every word is identical to main (3 pages, 0 overfull each). Against pdflatex: 566/676 and 477/716, the same as main.
  • Real-world corpus, main vs branch:
    • lecture-notes improves from 16 to 110 of 635 words within 0.5 bp of pdflatex.
    • Every other document is identical to main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

Theorem-like environments laid out from the compiler's new
Parsed::theorems and Parsed::qed_marks (compiler PR #149; this crate
needs vendor/compiler re-pinned to it to build):

- heads built from the record instead of source bytes: kernel
  \@BeginTheorem label box ({\bfseries Name Number (Note)}, rigid, then
  \hskip\labelsep \penalty0); amsthm \deferred@thm@head unboxed into the
  paragraph (\thm@indent, Name Number, note in \thm@notefont,
  \thm@headpunct, \hskip\thm@headsep or \newline), \swapnumbers, and
  \@upn's \check@icl italic correction (latex.ltx \sw@slant)
- body font on body words and spaces; no indent on the head paragraph
- vertical glue: \addvspace\@topsep \addvspace{-\parskip} before
  (amsthm \thm@preskip; kernel \topsep+\parskip, +\partopsep in vertical
  mode; proof 6pt plus 6pt + \partopsep), \@endparenv's \@topsepadd
  after, \@xaddvskip's strictly-larger rule, stretch and shrink carried
  to the page builder, \@nbitem after a heading, kernel \@endpetrue
- amsthm \qed: \unskip\penalty9999 \hbox{}\nobreak\hfill\quad and
  \openbox drawn as four 0.4pt rules (new BoxRec::Rules); \qedhere in a
  display as \eqno (amsmath \displaymath@qed)
- \noindent before a font command opening (\noindent\textit{...})

Oracle: tests/theorem_corpus, 43 fixtures vs pdflatex (MacTeX 2026) with
oracle.py refs|check; words and QED rules within 0.5 bp: 36/43 (main
0/43). tests/theorem_oracle.rs gates the 36; the 7 pending name their
cause (list lane #143: 3; display lane: 1; theorem opening with a list:
1; page builder: 1; custom \qedsymbol content: 1).

Gates (with the compiler PR swapped into vendor, uncommitted):
render-pipeline cargo test --release green; clippy warning counts equal
to main; HW1/HW2 words identical to main (3 pages, 0 overfull each);
real-world corpus: lecture-notes 16 -> 110 of 635 words within 0.5 bp of
pdflatex, every other document identical to main.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude theorem fidelity, mac-m5pro-kabir)
Commit-Executor: Claude Code 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 13, 2026
16 conflicts (adapter.rs 10, typeset.rs 5, incremental.rs 1). All but one were
misalignments: git shared common tails between this branch's additions and
#156's, so each side ended mid-construct and a plain "keep both" would not
compile. Rebuilt as complete separate pieces, as the merge log records for
#154 and #170.

- `Item` / `BoxRec` / `AItem` / `Paragraph`: both sides add variants and
  fields (#156's Glue, Rigid, Penalty, EmptyBox, Qed, Rules, LocalRule,
  `theorem_head`). Kept from both, with the enclosing enum, struct and match
  arms closed explicitly where git had shared a brace.

- **Incremental cache tags renumbered.** #156 numbered its five new
  `hash_items` arms 10-14, which this branch already uses for
  Footnote/TextBox/SetLength/LengthGlue/HSs. These tags key the incremental
  layout cache, so a collision serves one item's cached layout for another --
  the same hazard #170 hit, fixed the same way. #156's are now 19-23, the
  first free run. The two pre-existing collisions (Logo/Table at 9,
  Footnote/Rule at 10) are left for #182, which replaces the hand-numbering
  with `mem::discriminant`.

- `\qedhere` in a display: #156's block computes e/q before the display is
  placed, so it goes ahead of #157's fleqn/§1201 squeeze rather than the
  plain §1199 centring #156 carried with it. The symbol is built into a
  `NumberBox` (this branch's representation, which is where display_block
  reads the height and depth from) instead of #156's bare `(run, rec)` pair.

- `\addvspace`: kept main's `\@xaddvskip` rule (a negative amount after a
  non-negative `\lastskip` is added to it) together with #156's flex, so the
  surviving skip keeps its own stretch and shrink.

- `gap_has_list_end` came back. The origin/main merge had dropped it as
  having no caller; #156's theorem-skip code needs it. It is now written in
  terms of `LIST_ENVS` so it cannot drift from the other list predicates.

The one real disagreement, found by measurement, not by reading

#156 guards display continuation with `*prev_style == ParaStyle::Plain &&
prev_list.is_none()`. Merged as written, display-placement fell from 33/36 to
31/36: every new failure was a display inside a list or a quote, shifted 12
to 20 pt vertically (`20-enumerate-nested-display`, `29-quote-display`, and
`19-itemize-display` got worse).

Those two conjuncts are #156's older, coarser form of two conditions this
branch already states more precisely two lines above -- `same_list` (which
compares level and margins, not merely "no list") and `styled.unwrap_or_
default() == *prev_style`. Keeping them disabled display continuation inside
every list. Only `&& !theorem_head`, the genuinely new theorem condition, is
kept; display-placement is back to 33/36.

Gates (NixOS PC, TeX Live 2025, bundled tree plus nix-store `ec`; no
committed oracle data regenerated; zero font diagnostics on every run):

- amsmath 59/59, tabular 59/59, display-placement 33/36 -- all unchanged.
- theorem_oracle passes (#156's own corpus, MacTeX references).
- real-world 10/10 recovered, every page count still equal to its committed
  reference. lecture-notes drops from 7 diagnostics to 6: a theorem construct
  that was recovered is now supported.
- `cargo test --release` in render-pipeline: the same 11 known failures as
  before this merge, no more. Eight of them also fail on unmerged
  origin/main on this machine (the four NewCM controls, metrics_provenance,
  font_families_oracle, footnotes_oracle, page_frame); the other three
  (float_bodies, headings, list) have no baseline on a non-MacTeX host.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude integration, nixos-pc-kabir)
Commit-Executor: Claude Code 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 13, 2026
5 conflicts (typeset.rs 4, lib.rs 1), plus the usual catching-up where #160
branched before several of this lane's changes.

- `Context`: #160's `multicol: multicol::State` field and initialiser join
  this branch's; in lib.rs `multicol::attach` is added to this branch's
  Context setup, which reads the original (unmasked) sources for float
  bodies and also installs the maths colours and graphics environment.

- `build_with_floats`: git had aligned #160's copy of the signature against
  this branch's `stretched_left`/`Flow`/`layout_blocks`, so the conflict
  looked like two functions claiming one body. Kept this branch's block and
  injected #160's `multicol::outer_doc` early return into the one real
  `build_with_floats` instead.

- Pagination: #160's `multicol::paginate` branch runs before the float
  branches; `images` stays mutable, which the float path needs.

`multicol.rs` predates three later changes and did not compile against them:

- `Block::Paragraph` gained `addvspace_flex` (#156) and `shape` (the list
  shape work). `split_paragraph` now carries both across the split -- the
  first half keeps the original's leading glue and shape, the second starts
  mid-paragraph with no leading glue but the same shape.
- `Doc` gained `default_color`, `math_colors` and `page_color` (#150/#158).
  The synthesised outer and per-column documents inherit the outer
  document's colour state rather than defaulting it, so a `multicols` body
  inside `\color` keeps its colour.
- `rec_span` did not cover `BoxRec::ColorBox`/`Graphic`/`Transform`/`Rules`.
  They are listed explicitly rather than wildcarded, so the next variant is
  caught here too instead of silently losing its span (which is what feeds
  the column-break search).

Gates (zero font diagnostics on every run): multicol_oracle passes,
theorem_oracle passes, amsmath 59/59, tabular 59/59, display-placement
33/36, real-world 10/10 recovered with every page count still equal to its
committed reference. `cargo test --release` in render-pipeline: the same 11
known failures, no more.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude integration, nixos-pc-kabir)
Commit-Executor: Claude Code 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 13, 2026
15 conflicts (adapter.rs 12, typeset.rs 3), plus one API collision that git
did not flag at all.

#167 and #156 both extended `adapter::Item` with variants called `Glue` and
`Penalty`, with different shapes, and git auto-merged both definitions into
the enum without a conflict marker -- the build was the only thing that
noticed.

- `Penalty { penalty: i32 }` and `Penalty { value: i32 }` are the same thing
  under two field names; #167's emitters now use #156's, and its duplicate
  `hash_items` arm (tag 31) is gone, since #156's Penalty is already tag 21.
- `Glue` is not the same thing. #156's is `\thm@headsep`: points plus
  interword spaces of a given style. #167's is `\newblock`'s
  `\hskip .11em\@plus.33em\@minus.07em`: scaled by the font's quad, which is
  not known until typesetting. Neither can be expressed in the other, so
  #167's keeps its own variant as `Item::EmGlue`, with tag 30 (no collision
  with 19-23). It has exactly one emitter, so the rename is contained.

The rest, where #167 branched before this lane's list work:

- `ListGeom`: #167's `bibliography: Option<BibItemGeom>` joins this branch's
  `labelsep`/`label_bold`/`label_math`/`description`/`llap`, and the item
  builds one ListGeom carrying all of them. #167 called the three-argument
  `list_margins`; this branch's takes the twocolumn flag.
- `list_seps` had since been refactored into `class_seps` + `apply_sep_keys`
  + a version taking the list's own `\begin` options, so only #167's new
  `natbib_bibsep` is taken (natbib.sty 641-647: `\bibsep` is `\@listi`'s
  `\itemsep` plus `\parsep`), applied on top of the computed separations.
- The list-environment predicates already cover `thebibliography` through
  `LIST_ENVS`, so #167's three hand-written copies of the name list are
  dropped in favour of it.
- Item label layout: #167 still carried the `label_box` version that #152
  replaced with `label_words`/`label_bold`/`label_math`. Its bibliography
  branch is kept and its copy of the general branch dropped, so the two
  paths are `if bibliography { ... } else if label { ...this branch's... }`.
  The bibliography branch now passes the label's font to `label_box`, which
  gained those arguments with the list work.
- `theorem_items` (#156) still wraps the paragraph's items; #167's
  `bibliography_space_factors` (`\sfcode`\.\@m`, so the space after "Knuth."
  is an ordinary interword space) runs on the result.

Two markers came off, both because the work they waited on is already here

`bibliography_oracle` kept 14 fixtures behind `oracle_after_repin!`, ignored
with "needs the vendored compiler re-pinned to natbib citations and .bbl
input". Stage 1 re-pinned it, and all 14 pass, so the macro is gone and they
run. The oracle is 20 passing of 21; the one still ignored is the residual
#167 itself documents (`\Citet` upper-cases through natbib's `\NAT@Up`,
whose group ends between `V` and `an`, so pdfTeX sets no `Va` kern and the
pipeline does: 0.83 bp).

Gates (zero font diagnostics on every run): bibliography_oracle 20/21,
thebibliography_oracle passes, theorem_oracle, multicol_oracle and
siunitx_oracle pass, amsmath 59/59, tabular 59/59, display-placement 33/36,
real-world 10/10 recovered with every page count still equal to its
committed reference. `cargo test --release` in render-pipeline: the same 11
known failures, no more.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude integration, nixos-pc-kabir)
Commit-Executor: Claude Code 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 13, 2026
…#167 #168), and why #162 needs a lane

Records this session in the merge log: the oracle-harness bug that was
falsifying gate numbers for every lane, the parallel silent-skip in
`cargo test`, the four stage-3 merges with the techniques each needed, and
the measured reason #162 is a reconstruction rather than a merge.

Also records the control that separates pre-existing failures from this
session's work: eight of the eleven render-pipeline failures reproduce on
unmerged origin/main in this worktree, and the other three have no baseline
on a non-MacTeX host. page_frame_oracle, which the previous session's list
did not mention, is measured here as pre-existing.

Implementation-Agent: Claude Opus 5 (subagent kabir-claude integration, nixos-pc-kabir)
Commit-Executor: Claude Code direct Git
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar

GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Closing: this is the render-pipeline half of #149, pinned against #149's compiler vendor snapshot ("needs #149 re-pin"). #149 is closed as abandoned — the theorem lane landed on main via theorems.rs and the merged amsthm PRs (#190/#196/#480/#267) — so this branch's pin target will never exist. The layout comparisons (oracle 36/43) remain on agent/kabir-claude/theorem-fidelity for a rebuild on the landed model.

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