Skip to content

compiler: diagnose the LaTeX pdflatex refuses instead of compiling it silently - #365

Closed
GoKubar wants to merge 1 commit into
mainfrom
agent/claude/engine-silent-latex
Closed

GoKubar wants to merge 1 commit into
mainfrom
agent/claude/engine-silent-latex

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Issue #2 reported that flashtex-render returns zero diagnostics for Let $a + $x^2$ + b$ hold., for \[…\] in a tabular cell and for \text{…} without amsmath — 15 of the 22 caret-context cases diverging from pdflatex, so we could not use our own engine as an oracle.

Re-measuring against pdflatex found two separate things, and only the second is an engine bug.

The 15 was a measurement bug

scripts/caret_context_oracle.py (on #350's branch) reads

diags = json.loads(out.read_text()).get("diagnostics", [])

from the --v2 file. That file is an envelope — {id, payload, protocol_version, type} — and diagnostics live under payload. The read returned [] for every case, the diagnosed ones included. Verified directly: on the nested-math case flashtex-render prints

error[compiler] math script marker used outside math mode (3:12)
    recovery: ignored the script marker and continued

Read from payload.diagnostics, the engine already reported an error for 13 of those 15. The one-line fix belongs to #350's branch; it is on the issue.

Eleven cases were genuinely silent

Across the whole family the issue names, and this PR closes them:

what pdflatex now
\( / \) anywhere ! LaTeX Error: Bad math environment delimiter lexed as inline-math delimiters; a misplaced one diagnosed like \[/\]
\[…\] / \begin{equation} in a table entry or a \caption ! Missing $ inserted restricted horizontal mode, set inline
\text{…} without amsmath ! Undefined control sequence \text requires \usepackage{amsmath}
a bare & outside any alignment ! Misplaced alignment tab character & the same, naming \&
\\ in vertical mode ! LaTeX Error: There's no line here to end the same, naming \vspace

\(…\) was not only undiagnosed but mis-set: the delimiters lexed as literal parentheses, so \(x^2\) — ordinary LaTeX — printed “(x2)” as text with no complaint. Diagnosing a misplaced one was half the fix; it now sets real inline math.

The oracle changed the design three times

Every expectation came from pdfTeX 3.141592653-2.6-1.40.27 (TeX Live 2025), not from reasoning about TeX, and three of my guesses were wrong:

  • A \section title and a \footnote accept a display. Both set their argument as a paragraph. So restricted_hbox counts a table entry and \caption (which measures its text in an \sbox) and deliberately not those. A flag meaning “inside a command argument” would have flagged valid documents.
  • \noindent \\ text compiles. \noindent leaves TeX in horizontal mode without typesetting anything — hence paragraph_forced. Testing paragraph emptiness alone would have been a false positive.
  • Let $a + $$x^2$$ + b$ hold. compiles, as alternating inline formulas.

All three are pinned as controls in tests/pdflatex_rejects.rs next to the 17 rejected cases, precisely because they are where the obvious guess is wrong.

Recovery is unchanged, by design

pdflatex reports every one of these and then recovers, producing a PDF anyway — none is fatal outside -halt-on-error. So the severity match is “error, and keep rendering”: each diagnostic carries a recovery, a span and a code, and the page still builds. a_diagnosed_document_still_produces_content asserts exactly that.

The bare-& rule tells \& from & by span width, not by text — the lexer gives both the same one-character Word — and stays silent whenever a word's span is wider than its own bytes (an escape, or macro expansion, whose span points at the invocation). Erring towards silence there is deliberate: a false “misplaced &” on a document that compiles is worse than a missed one.

Measured

crates/compiler against pdflatex over 79 probes — the 22 caret-context naive insertions plus 57 written for the families in the issue, 15 of them valid-LaTeX controls:

before: 68 agree, 11 silent where pdflatex errors, 1 over-eager
after:  78 agree,  0 silent where pdflatex errors, 1 over-eager

All 15 of 15 caret-context cases pdflatex rejects are now diagnosed. The single remaining divergence is pre-existing and honest: $a \footnote{x} b$ compiles in pdflatex, and this compiler reports \footnote is not supported in math mode — an unsupported_feature, not a false syntax error.

Real-world corpus, all 22 fixtures, this build against a control build of origin/main in the same directory: zero newly reported diagnostics, identical page counts — hyperref-toc, listings-manual and siunitx-tables included, the three most at risk from the & and \\ rules. So no fixture turned out to be invalid LaTeX: none of them gained a diagnostic at all.

Tests

crates/compiler suite green. Three math.rs unit tests moved from MathPackages::KERNEL to amsmath — they are about \text's spacing and undelimited-argument behaviour, not about whether \text is defined; without the package it is not, and the new diagnostic would have masked what they assert.

Gates

crates/compiler suite green · crates/render-pipeline 226 passed / 0 failed · amsmath 59/59 · amssymb 38/39 · tabular 106/106 · HW1 3 pages, HW2 3 pages, 0 errors, both via flashtex-render and both matching pdflatex.

One note on HW2, because I got it wrong in an earlier report and it is a fixture other lanes gate against. flashtex-render sets HW2 on 3 pages; flashtex-compiler's own layout sets it on 4. That is a pre-existing difference between the two producers, already tracked by the #[ignore]d crates/compiler/tests/hw2_gate.rs, and neither number moves here. The corpus A/B in this PR drives flashtex-compiler deliberately — it is the crate being changed, and flashtex-render reads vendor/compiler and so cannot see the change — but its page column is a control-vs-branch comparison, not the HW gate.

Reach

This does not reach flashtex-render, which builds against vendor/compiler. A re-pin is needed for that, and this branch deliberately does not make one. The pipeline half — the display list flattening every compiler code to the literal string "compiler", which destroys the syntax_error / unsupported_feature distinction before a consumer sees it — is split out separately.


Draft.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

… silently

Issue #2 reported that `flashtex-render` returns *zero* diagnostics for
`Let $a + $x^2$ + b$ hold.`, for `\[...\]` in a `tabular` cell and for
`\text{...}` without amsmath -- 15 of the 22 caret-context cases diverging
from pdflatex. Re-measuring against pdflatex found two separate things, and
only the second one is an engine bug.

**The 15 was a measurement bug.** `scripts/caret_context_oracle.py` (PR #350)
reads `json.loads(out.read_text()).get("diagnostics", [])` from the `--v2`
file. That file is an envelope -- `{id, payload, protocol_version, type}` --
and the diagnostics live under `payload`, so the read returned `[]` for every
case, including the ones the engine diagnosed correctly. Read from the right
key, the compiler already reported an error for 13 of those 15. The fix there
is one line in that script and belongs to that branch; noted on the issue.

**Eleven cases were genuinely silent**, across the whole family of "LaTeX
pdflatex refuses", and this commit closes them:

| what | pdflatex | now |
|---|---|---|
| `\(` / `\)` anywhere | `! LaTeX Error: Bad math environment delimiter` | lexed as inline-math delimiters; a misplaced one is diagnosed like `\[`/`\]` |
| `\[...\]` / `\begin{equation}` in a table entry or a `\caption` | `! Missing $ inserted` | `restricted horizontal mode`, set inline |
| `\text{...}` without amsmath | `! Undefined control sequence` | `\text requires \usepackage{amsmath}` |
| a bare `&` outside any alignment | `! Misplaced alignment tab character &` | the same, naming `\&` |
| `\\` in vertical mode | `! LaTeX Error: There's no line here to end` | the same, naming `\vspace` |

`\(...\)` was not only undiagnosed but *mis-set*: the delimiters lexed as
literal parentheses, so `\(x^2\)` -- ordinary LaTeX -- printed "(x2)" as text.
Diagnosing a misplaced one was half the fix; it now sets real inline math.

Every expectation was taken from pdfTeX 3.141592653-2.6-1.40.27 (TeX Live
2025) rather than assumed, which changed the design three times:

* a `\section` title and a `\footnote` accept a display -- both set their
  argument as a paragraph -- so `restricted_hbox` deliberately does not count
  them. A mode flag that simply meant "inside a command argument" would have
  been wrong.
* `\noindent \\ text` compiles: `\noindent` leaves TeX in horizontal mode
  without typesetting anything, hence `paragraph_forced`. Testing `para`
  emptiness alone would have flagged valid input.
* `Let $a + $$x^2$$ + b$ hold.` compiles, as alternating inline formulas.

All three are pinned as controls in `tests/pdflatex_rejects.rs`, next to the
17 rejected cases, precisely because they are the cases where the obvious
guess is wrong.

Recovery is unchanged by design: pdflatex reports every one of these and then
recovers, producing a PDF anyway (none is fatal outside `-halt-on-error`), so
each diagnostic here carries a `recovery`, a span and a code, and the page
still builds. `a_diagnosed_document_still_produces_content` asserts that.

The bare-`&` rule distinguishes `\&` from `&` by span width rather than by
text -- the lexer gives both the same one-character `Word` -- and stays silent
whenever a word's span is wider than its own bytes (an escape, or macro
expansion, whose span points at the invocation). Erring towards silence there
is deliberate: a false "misplaced &" on a document that compiles is worse
than a missed one.

Measured, `crates/compiler` against pdflatex over 79 probes (the 22
caret-context naive insertions plus 57 written for the families named in the
issue, 15 of them valid-LaTeX controls): 68 agreeing before, 78 after. All 15
caret-context cases pdflatex rejects are now diagnosed. The one remaining
divergence is pre-existing and honest: `$a \footnote{x} b$` compiles in
pdflatex and this compiler reports `\footnote is not supported in math mode`
-- an `unsupported_feature`, not a false syntax error.

Real-world corpus, all 22 fixtures, this build against a control build of
`origin/main` in the same directory: **zero newly reported diagnostics** and
identical page counts, including `hyperref-toc`, `listings-manual` and
`siunitx-tables`, the ones most at risk from the `&` and `\\` rules.

Three `math.rs` unit tests moved from `MathPackages::KERNEL` to amsmath.
They are about `\text`'s spacing and undelimited-argument behaviour, not
about whether `\text` is defined -- without the package it is not, and the
new diagnostic would have masked what they assert.

This does not reach `flashtex-render`, which builds against `vendor/compiler`;
that needs a re-pin, which this branch deliberately does not make.

Implementation-Agent: claude-opus-5 (linux-primary, engine diagnostics lane)
Commit-Executor: claude-opus-5 (direct git, Cursor usage limit in effect)
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@GoKubar

GoKubar commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Closed under the old-engine freeze (DESIGN.md D13): this is old-path feature or hand-port work, and the old engine retires at the P5 switch-over. The branch is kept; reopen it if it turns out to be a critical user-facing fix. — kabir-claude (Commander), nightly review 2026-09-30

@GoKubar GoKubar closed this Sep 30, 2026
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