Skip to content

render-pipeline: graphicx draft and demo reserve the space pdfTeX reserves - #377

Merged
GoKubar merged 1 commit into
mainfrom
agent/nixos-graphics/draft-demo
Sep 14, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/nixos-graphics/draft-demo

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Both graphicx options are ways of reserving an image's space without reading the file, which is why test documents use them. Four corpus fixtures write \includegraphics[draft] and two load \usepackage[demo]{graphicx}; we honoured neither, so each of those graphics reserved nothing and the document reflowed. This was the single biggest cause of page-count divergence on the real-world corpus (issue #2, comment 5662357740).

What pdfTeX does — measured, not assumed

Oracle: pdfTeX 3.141592653-2.6-1.40.27 (TeX Live 2025), \wd/\ht/\dp of the \hbox. pdflatex is an oracle only and is never in the product path.

demo

graphics.sty's \DeclareOption{demo} replaces the whole of \Ginclude@graphics with

\rule{\@ifundefined{Gin@@ewidth}{150pt}{\Gin@@ewidth}}%
     {\@ifundefined{Gin@@eheight}{100pt}{\Gin@@eheight}}

so no file is looked up at all, and \Gin@req@sizes — the only thing that scales a natural size, applies keepaspectratio and honours a leading scale= — is never used. All that reaches the rule is what \Gin@esetsize left in \Gin@@ewidth/\Gin@@eheight, and it reaches it literally:

options pdflatex why
(none) 150 × 100 pt the defaults
width=100pt 100 × 100 pt the height is pinned; the rule is not proportional
height=40pt 150 × 40 pt the mirror image
width=100pt,height=40pt 100 × 40 pt both keys survive
keepaspectratio,width=100pt,height=40pt 100 × 40 pt keepaspectratio never reaches \Gin@req@sizes
scale=2 150 × 100 pt so does the first scale, which only sets \if@tempswa
scale=2,width=100pt 99.998 × 66.666 pt …but the width after it is a \Gscale@@box around the default rule
width=100pt,scale=2 99.998 × 66.666 pt same: scale does not consume a pending width
angle=90 100 × 150 pt the rule rotates
width=100pt,angle=90 100 × 100 pt angle does consume the pending width into the rule first
angle=90,width=100pt 100 × 150 pt after an angle the width resizes the rotated box
angle=30 179.900 × 161.599 pt ordinary rotated bounding box of 150 × 100
angle=45,keepaspectratio,width=100pt,height=40pt 40 × 40 pt \Gscale@@box does honour keepaspectratio
width=100pt,angle=90,height=40pt 40 × 40 pt the whole chain

Depth is 0 throughout. All 24 measured rows are a unit test.

draft

draft changes no dimension: \Gin@setfile still reads the file's bounding box and still sets a box of \Gin@req@width × \Gin@req@height — it just fills it with

\hb@xt@\Gin@req@width{\vrule\hss
  \vbox to \Gin@req@height{\hrule\@width\Gin@req@width \vss
    \rlap{ \ttfamily <file>}\vss \hrule}\hss\vrule}

instead of the image. Measured with a real 150 × 100 bp PNG, every key combination gives byte-identical dimensions with and without draft.

What it does change is a file that is not there. pdftex.def's \Gread@pdftex leaves the bounding box at 0 0 72 72 and, under draft, warns instead of raising its package error:

options (file missing) pdflatex
draft 72.26999 × 72.26999 pt — one inch square
draft,width=100pt 100 × 100.005 pt — square, aspect preserved
draft,width=0.5\linewidth 172.5 × 172.510 pt
draft,angle=30 98.721 × 98.721 pt

That is where the corpus divergence came from: all six fixture graphics name files that do not exist.

draft is a key (draft=false turns it off for one graphic), a package option, and a class option — LaTeX passes the class options to every package. A final anywhere cancels it, in either list and in either order, because \ProcessOptions runs the declared options in declaration order and final is declared right after draft. demo beats draft, whose branch lives in \Gin@setfile, which under demo is never reached — that is exactly the hyperref-toc fixture, which writes both.

Result

Real-world corpus, flashtex-render producer, against a control build of origin/main bca6ce52 in the same worktree and the same target directory:

fixture pdflatex control branch
article-twocolumn 2 1 2 [demo], width=\columnwidth
hyperref-toc 4 3 4 [demo] + a per-image draft
lab-report 3 2 3 [draft,width=0.8\linewidth]{plot.png}
conf-paper 4 2 3 [draft,width=\linewidth]{fig.pdf} in a figure* — see below
thesis-chapter 5 4 4 [draft,width=0.7\linewidth] — box now correct, page not
twelvept-plain 3 4 4 unrelated to graphics

Page-count match 16/22 → 19/22. Render error-severity diagnostics 94 → 89; no document gains an error. The other 16 documents are byte-for-byte unchanged in page count.

conf-paper is still one page short for a reason that is not graphics: its figure is a figure*, where pdflatex's \linewidth is \textwidth (505.89 pt) rather than the 243.911 pt \columnwidth this engine resolves it to, because a double-column float is not modelled at all — floats::scan maps figure* to plain FloatKind::Figure and floatpage::build_box sets every float at style.text_width_pt. That is the next thing to fix for this fixture, and it is a float-placement change, not a graphics one.

Painting

The reserved box is painted: a solid rule for demo, the four-rule frame for draft. Two honest limits:

  • the frame's \rlapped file name is not set — it is centred inside the reserved box and contributes no dimension, so it costs nothing in space;
  • neither placeholder is painted when an angle= has turned the box, since the wire Rule item is axis-aligned. The space is still exact.

trim/clip/viewport stay recognised and deliberately unhonoured; no field was added to the wire Image item.

\includegraphics in running text is still dropped on main (#325 is open, not merged). parse_keys, mode and demo_box are shared, so that PR picks up the key parsing for free but will need the same demo/draft branch at its own call site.

Gates

Built with CARGO_BUILD_JOBS=4; control and branch in the same directory.

  • cargo test --release --locked --no-fail-fast: render-pipeline 244 passed / 0 failed / 1 ignored (control 241/0/1 — the three new tests), compiler 498 passed / 0 failed / 4 ignored (crate untouched).
  • amsmath oracle 59/59 within 0.5 bp.
  • tabular oracle 106/106 (words 0.5 bp, rules 0.1 bp).
  • HW1 3 pages, HW2 3 pages through flashtex-render, 0 errors, 0 overfull, 0 font-failure diagnostics — unchanged from control. (The flashtex-compiler producer still reports HW2 as 4 pages; that producer disagreement is untouched here.)
  • Font diagnostics across the corpus identical to control: 20 missing_glyph warnings, no ec_metrics_unavailable/font_unavailable/tfm_run_error.

No vendor directory was touched, so no re-pin is needed. Only crates/render-pipeline changes, so there is no compiler PR to split off.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

… reserves

Both options are ways of keeping an image's space without embedding the
file, which is why test documents use them: four corpus fixtures write
`\includegraphics[draft]` and two load `\usepackage[demo]{graphicx}`. We
honoured neither, so each of those graphics reserved nothing and the
document reflowed.

`demo` (`graphics.sty` `\DeclareOption{demo}`) replaces the whole of
`\Ginclude@graphics` with `\rule{\@ifundefined{Gin@@ewidth}{150pt}
{\Gin@@ewidth}}{\@ifundefined{Gin@@Eheight}{100pt}{\Gin@@Eheight}}`. No file
is looked up. `\Gin@req@sizes` -- the only thing that scales a natural size,
applies `keepaspectratio` and honours a leading `scale=` -- is never used,
so all that reaches the rule is what `\Gin@esetsize` left in
`\Gin@@ewidth`/`\Gin@@eheight`, and it reaches it literally: `[width=W]`
gives a `W` x 100pt rule, not a proportional one. From the first
`angle=`/`scale=` onwards the rule is wrapped in `\Gin@erotate`/
`\Gscale@@box` exactly as a real graphic is, and `keepaspectratio` does
apply there. `demo` beats `draft`, whose branch lives in `\Gin@setfile`,
which is never reached.

`draft` changes no dimension at all: `\Gin@setfile` still reads the file's
bounding box and still sets a box of `\Gin@req@width` x `\Gin@req@height`,
it just fills it with a frame of default-thickness rules and a `\rlap`ped
file name instead of the image. What it does change is a file that is not
there: `pdftex.def`'s `\Gread@pdftex` leaves the bounding box at `0 0 72 72`
and, under `draft`, warns instead of raising its package error -- so the
graphic reserves one inch square, scaled by the keys. That is where the
corpus divergence came from: all six fixture graphics name files that do
not exist. `draft` is now a key (`draft=false` turns it off for one
graphic), a package option, and a class option, since LaTeX passes the
class options to every package; a `final` anywhere cancels it, because
`\ProcessOptions` runs the declared options in declaration order and
`final` is declared after `draft`.

The reserved box is painted: a solid rule for `demo`, the four-rule frame
for `draft`. The frame's `\rlap`ped file name is not set yet -- it is
centred inside the reserved box and contributes no dimension -- and neither
placeholder is painted when an `angle=` has turned the box, since the wire
`Rule` item is axis-aligned. `trim`/`clip`/`viewport` stay recognised and
deliberately unhonoured; no field was added to the wire `Image` item.

Verified on linux-primary NixOS against a control build of origin/main
bca6ce5 in the same worktree and the same target directory, oracle
pdfTeX 3.141592653-2.6-1.40.27 (TeX Live 2025):

- 24 measured `demo` key combinations and the `draft` fallback size are
  unit tests in `graphics.rs`; three end-to-end tests through a `figure`
  are `tests/graphics_draft_demo.rs`.
- cargo test --release --locked --no-fail-fast: render-pipeline 244p/0f/1i
  (control 241p/0f), compiler 498p/0f/4i (crate untouched).
- amsmath oracle 59/59 within 0.5 bp; tabular oracle 106/106 (words 0.5 bp,
  rules 0.1 bp).
- HW1 and HW2 through `flashtex-render`: 3 pages each, 0 errors, 0
  overfull, 0 font-failure diagnostics -- unchanged from control. (The
  `flashtex-compiler` producer still reports HW2 as 4 pages; that
  disagreement is untouched here.)
- real-world corpus vs control, `flashtex-render` producer: page-count
  match 16/22 -> 19/22. article-twocolumn 1 -> 2 (= pdflatex 2),
  hyperref-toc 3 -> 4 (= 4), lab-report 2 -> 3 (= 3). conf-paper 2 -> 3
  (pdflatex 4, see below); thesis-chapter and twelvept-plain unchanged.
  Render error-severity diagnostics 94 -> 89; no document gains an error.
  Font diagnostics identical (only `missing_glyph`, same 20).
- conf-paper is still one page short for a reason that is not graphics:
  its figure is a `figure*`, where pdflatex's `\linewidth` is `\textwidth`
  (505.89pt) rather than the 243.91pt `\columnwidth` this engine resolves
  it to, because a double-column float is not modelled. thesis-chapter's
  and twelvept-plain's remaining page is also unrelated to graphics.

Implementation-Agent: engine-engineer (Claude Opus 5), linux-primary
Commit-Executor: this agent, direct Git under the observed Cursor-limit
fallback

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar
GoKubar merged commit cc3232b into main Sep 14, 2026
9 of 20 checks passed
GoKubar added a commit that referenced this pull request Sep 14, 2026
…name}`

report.cls and book.cls open `thebibliography` with
`\chapter*{\bibname\@mkboth{...}}`, not article.cls's
`\section*{\refname}`. The compiler synthesises one unnumbered level-1
heading reading `References` for every class, and the pipeline laid that
out as a section in the flow — so in a `report` the bibliography was set
under the wrong name at the bottom of whatever page the body ended on,
with no `\clearpage` and no `\@makeschapterhead` drop.

`fixtures/real-world/thesis-chapter` is exactly this shape, and it is why
it came out **4 pages against pdflatex's 5**: pages 1-4 were already
word-for-word in the right places; the reference simply starts a fifth
page for the bibliography that we folded onto page 4.

**This is not the missing-`[draft]`-graphic cause it was attributed to.**
That fixture's `\includegraphics[draft,width=0.7\linewidth]{network.pdf}`
sits inside a `figure`, and PR #377's reservation already handles it:
probing that float against pdflatex gives dy **-0.086 bp** for the caption
and the text below it. The reservation gap is real but confined to
*running-text* `\includegraphics`, which every corpus fixture avoids (all
six are inside a `figure`/`figure*`) — see the PR body.

Measured against pdflatex (TeX Live 2025) on a minimal report:

  pages                 1 -> 2 (reference 2)
  `Bibliography` head   not set -> x 72.000, baseline 162.660 (both exact)
  first `[1]` entry     x 77.424, baseline 216.060 (both exact)
  chapter body          unmoved on page 1

Corpus (22 real documents vs pdflatex, tolerance 0.5 bp):
**page counts 20/22 -> 21/22**, dx 84.47% -> 84.55%, dy 65.21% -> 65.40%,
both 58.67% -> 58.90%, aligned 13985 -> 14062. `thesis-chapter` is the
only fixture that moves: 4/5 -> **5/5** pages, dx 91.87 -> 92.33, dy 99.84
-> 99.85, both 91.87 -> 92.33, aligned 1279 -> 1356 (the whole fifth page
now compares). The one page count still wrong is `twelvept-plain` (4
against 3), a different cause and not touched here.

Gates: `cargo test --release --no-fail-fast` on render-pipeline 254 passed
/ 0 failed / 2 ignored (plus the new `report_bibliography` tests),
class-geometry 4/0, compiler 577/0/4; amsmath 59/59, tabular 106/106, HW1
and HW2 3 pages / 0 errors / 0 font-FAILURE diagnostics.

Implementation-Agent: Claude Opus 5 (lane nixos-graphics-sectioning, linux-primary)
Commit-Executor: Claude Opus 5 (direct git, observed Cursor usage limit)
GoKubar added a commit that referenced this pull request Sep 14, 2026
…name}` (#399)

report.cls and book.cls open `thebibliography` with
`\chapter*{\bibname\@mkboth{...}}`, not article.cls's
`\section*{\refname}`. The compiler synthesises one unnumbered level-1
heading reading `References` for every class, and the pipeline laid that
out as a section in the flow — so in a `report` the bibliography was set
under the wrong name at the bottom of whatever page the body ended on,
with no `\clearpage` and no `\@makeschapterhead` drop.

`fixtures/real-world/thesis-chapter` is exactly this shape, and it is why
it came out **4 pages against pdflatex's 5**: pages 1-4 were already
word-for-word in the right places; the reference simply starts a fifth
page for the bibliography that we folded onto page 4.

**This is not the missing-`[draft]`-graphic cause it was attributed to.**
That fixture's `\includegraphics[draft,width=0.7\linewidth]{network.pdf}`
sits inside a `figure`, and PR #377's reservation already handles it:
probing that float against pdflatex gives dy **-0.086 bp** for the caption
and the text below it. The reservation gap is real but confined to
*running-text* `\includegraphics`, which every corpus fixture avoids (all
six are inside a `figure`/`figure*`) — see the PR body.

Measured against pdflatex (TeX Live 2025) on a minimal report:

  pages                 1 -> 2 (reference 2)
  `Bibliography` head   not set -> x 72.000, baseline 162.660 (both exact)
  first `[1]` entry     x 77.424, baseline 216.060 (both exact)
  chapter body          unmoved on page 1

Corpus (22 real documents vs pdflatex, tolerance 0.5 bp):
**page counts 20/22 -> 21/22**, dx 84.47% -> 84.55%, dy 65.21% -> 65.40%,
both 58.67% -> 58.90%, aligned 13985 -> 14062. `thesis-chapter` is the
only fixture that moves: 4/5 -> **5/5** pages, dx 91.87 -> 92.33, dy 99.84
-> 99.85, both 91.87 -> 92.33, aligned 1279 -> 1356 (the whole fifth page
now compares). The one page count still wrong is `twelvept-plain` (4
against 3), a different cause and not touched here.

Gates: `cargo test --release --no-fail-fast` on render-pipeline 254 passed
/ 0 failed / 2 ignored (plus the new `report_bibliography` tests),
class-geometry 4/0, compiler 577/0/4; amsmath 59/59, tabular 106/106, HW1
and HW2 3 pages / 0 errors / 0 font-FAILURE diagnostics.

Implementation-Agent: Claude Opus 5 (lane nixos-graphics-sectioning, linux-primary)
Commit-Executor: Claude Opus 5 (direct git, observed Cursor usage limit)
@GoKubar
GoKubar deleted the agent/nixos-graphics/draft-demo 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