Skip to content

compiler: \ref in a heading or caption resolves, instead of typesetting its key - #635

Merged
GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/ref-in-headings
Sep 15, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/kabir-claude/ref-in-headings

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

The bug

\section{Intro}\label{sec:a}
\section{Back to \ref{sec:a} again}

typesets "Back to sec:a again". It should be "Back to 1 again". The same happened in captions, and \pageref/\eqref behaved identically:

before after
\section{Back to \ref{sec:a} again} Back to sec:a again Back to 1 again
\caption{See \ref{sec:a} here} See sec:a here See 1 here
\section{P \pageref{sec:a} E \eqref{sec:a}} P sec:a E sec:a P 1 E (1)
\section{Missing \ref{nope} here} Missing nope here Missing ?? here

Cross-references in headings and captions are ordinary LaTeX, so this is squarely user-facing.

Cause

Headings, captions and style arguments are not re-parsed — they are flattened into a token list and run through inlines_from_tokens, which dispatches only a handful of commands by name (\color, siunitx, style commands…). \ref was not among them, so the command token was dropped and its braced argument survived as ordinary text.

Running text was always fine, because the main token loop builds an Inline::Reference at parser.rs's "ref" | "pageref" | "eqref" arm. The new arm emits exactly that same inline, so resolution, \eqref's parentheses and the bold ?? for an undefined key are identical in both places.

It also fixes the unclosed-brace case

style_command re-enters a closed group properly ("so math and other commands inside it are parsed normally") but falls through to inlines_from_tokens when closed_group_start() finds no closing brace. So one unclosed \textbf{ in an \inputed file made every later \ref print its key:

before:  main.tex 66..71  'sec:b'      (just the key, argument span only)
after:   main.tex 61..72  '1'          (the whole \ref{...}, resolved)

That span-and-value pair is what the Mac's NavigationRealCompilerTests fixture asserts, so this clears one of the five known Mac test failures.

Tests

compiler: 737 passed, 0 failed, plus a new reference_in_a_heading_is_a_reference_not_text pinning that the heading's \ref is an Inline::Reference and that the key does not survive as text.

Implementation-Agent: orchestrator-astra
Commit-Executor: orchestrator-astra (direct; Cursor usage limit)

…ng its key

`\section{Back to \ref{sec:a} again}` typeset the literal "sec:a". So did
`\caption{See \ref{sec:a} here}`, and `\pageref`/`\eqref` behaved the same way.

Headings, captions and style arguments are flattened into a token list and run
through `inlines_from_tokens` rather than being re-parsed, and that converter
handles only a few commands by name. `\ref` was not among them, so the command
was dropped and its braced argument survived as ordinary text.

The arm added here emits the same `Inline::Reference` the main token loop builds,
so resolution, `\pageref`'s page number, `\eqref`'s parentheses and the bold `??`
for an undefined key are now identical in both places.

This is also why a cross-reference after an unclosed brace printed its key: the
recovery path in `style_command` falls through to `inlines_from_tokens` when
`closed_group_start()` finds no closing brace. That case is fixed by the same
arm, and `\ref{sec:b}` again carries the whole `\ref{...}` span with the resolved
number -- which is what the Mac's NavigationRealCompilerTests fixture expects.

compiler: 737 passed, 0 failed.

Implementation-Agent: orchestrator-astra
Commit-Executor: orchestrator-astra (direct; Cursor usage limit)
@GoKubar
GoKubar merged commit 094f25c into main Sep 15, 2026
10 of 21 checks passed
GoKubar added a commit that referenced this pull request Sep 15, 2026
/#635 grew the inventory

docs/user/compiler.md (generated) conflicted again; took main's copy and
regenerated via render_supported_latex.sh so \underbar and main's
additions both appear, sync --check clean.

Implementation-Agent: fable-rebase-a
Commit-Executor: fable-rebase-a (direct; Cursor usage limit)
@GoKubar
GoKubar deleted the agent/kabir-claude/ref-in-headings 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