Skip to content

compiler: the tie is a space, not a tilde - #332

Closed
GoKubar wants to merge 1 commit into
mainfrom
engine/nobreakspace-compiler
Closed

GoKubar wants to merge 1 commit into
mainfrom
engine/nobreakspace-compiler

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

LaTeX has three spellings of one thing, and this compiler got each of them wrong somewhere. ~ is catcode 13 and expands to \nobreakspace (latex.ltx 9413); \nobreakspace is \DeclareRobustCommand{\nobreakspace}{\leavevmode\nobreak\ } (latex.ltx 9411); a non-breaking space typed into a UTF-8 source is mapped onto the same command by inputenc. All three are an interword space of the current font with no legal line break at it.

written was now
~ at top level correct correct
~ in a \newcommand/\def body a literal tilde the tie
\nobreakspace error: not supported by this compiler version, and the space was deleted the tie, no diagnostic
\, \! \: \; \> in a macro body (text mode) the bare character — \newcommand{\ab}{A\,B} set A,B the kern
\textasciitilde correct correct, including inside a macro body
`\verb ~/.local `

How broad ~ actually is — measured, and narrower than it sounds

The report that reached this lane was that ~ prints literally in "almost every real document". It does not, and inflating it would have been the wrong call. Rendering all 22 fixtures/real-world fixtures and looking for a literal tilde in the output:

  • 98 ~ in the sources, exactly 1 typeset as a tilde (article-twocolumn, the pp.~1119--1184 continuation line of a \bibitem).
  • One more literal tilde is correct: listings-manual writes \texttt{\textasciitilde/.local/bin}.
  • Every other context was already right, verified individually: top level, \textbf, \emph, \section, itemize, footnote, center, quote, \title, before \ref/\cite, and ~ inside a macro argument.

The dividing line is exact: a ~ written directly in the source has a span that covers its own byte, and everything already worked from that. Broken were macro replacement text and \nobreakspace — which this corpus barely exercises and real documents do (\newcommand{\eg}{e.g.~}, \newcommand{\figref}[1]{Figure~\ref{#1}}).

The breakpoint test, against pdflatex

Width alone is not the claim; the tie must also remove the breakpoint. A paragraph was engineered to want to break exactly there, and run both ways under pdflatex TeX Live 2025 and through the engine:

xx xx xx xx xx aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll Figure 7 shows the rest of it.
pdflatex line 1 ends engine line 1 ends
plain space ... llll Figure ... llll Figure
~ ... llll Figure 7 ... llll Figure 7

Identical, 0 overfull/underfull in both pdflatex runs. The tie's width is the font's own interword space (3.321bp at 10pt) and is not inflated by a preceding sentence period (Dr.~Smith measures 3.321bp, not the space-factor-3000 width), which is what \ requires.

One cause behind all of it

The lexer records a control symbol's escape only in the width of its span — \, is the word , spanning two bytes — and a token copied out of a macro's replacement text carries the span of the invocation, whose width says nothing about how the token was written. Any decision made from token.span there is made from unrelated bytes.

~ was the same shape one level up: the source byte was carried through the text stream and a later stage was expected to recognise the tie by reading the source back. Replacement text makes that impossible, and it cannot be guessed either, because \textasciitilde produces the same character and must stay a tilde.

So the escape is no longer re-derived:

  • ExpandedToken::escape_span returns the token's own bytes (the recorded definition bytes for replacement text); control_symbol_kern's two call sites read that instead of token.span. token.span stays the invocation span, which is what diagnostics and incremental relayout need.
  • lexer::apply_text_ligatures — already the one place that turns source characters into the characters TeX sets (-- into an en dash, ` into a curly quote) — resolves ~ to lexer::NO_BREAK_SPACE (U+00A0). The text stream is self-describing from then on. \verb and verbatim never reach that function, which is exactly where pdflatex also stops treating ~ as active.
  • text_builtins::kernel_tie gives \nobreakspace the same character. It is deliberately not a row in TEXT_SYMBOLS: it is a kernel command, not a \DeclareTextCommand, so resolving it through the encoding tables is what produced unavailable in encoding OT1 for the commonest space in LaTeX. The *.dfu row naming U+00A0 describes only the other direction.

The math hand-off has the same span confusion and is a separate lane's fix (#286, parser::math_token). Nothing here touches it; this is the text-mode twin, which that PR does not cover — \newcommand{\ab}{A\,B} in ordinary prose still printed a comma.

Gates

  • crates/compiler cargo test --release --no-fail-fast: 498 passed, 0 failed, against the same-directory control built from the same base 5baa9e6e: 489 passed, 0 failed. The 9 new tests are tests/tie.rs.
  • Real-world corpus, all 22 fixtures, compiler producer, control vs branch in the same directory: 334 diagnostics before, 334 after — byte-identical, and the only output changes are 72 text runs where ~ became U+00A0. No fixture gained or lost a diagnostic. The single surviving literal tilde is listings-manual's \textasciitilde, which is correct.
  • supported_latex drift gate passes with \nobreakspace added to the inventory; supported-latex.json, coverage.md and docs/user/compiler.md were regenerated by scripts/render_supported_latex.sh, and the sha256-gated Mac copy by apps/mac/scripts/sync-supported-latex.sh.

Vendor re-pin needed for the render pipeline

crates/render-pipeline builds against vendor/compiler, a pinned mirror, so it does not see this change. Its own tie handling is fixed in a companion pipeline PR, which is written to be correct both before and after a re-pin; the macro-body case reaches the Latin Modern producer only once vendor/compiler is re-pinned past this commit. No vendor files are committed here.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git; no Cursor CLI is installed on this machine, so the direct-agent route is the only available one.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

LaTeX has three spellings of one thing and this compiler got each of them
wrong somewhere. `~` is catcode 13 and expands to `\nobreakspace`
(latex.ltx 9413); `\nobreakspace` is `\DeclareRobustCommand{\nobreakspace}
{\leavevmode\nobreak\ }` (latex.ltx 9411); a non-breaking space typed into a
UTF-8 source is mapped onto the same command by `inputenc`. All three are an
interword space of the current font with no legal line break at it.

* `~` inside a macro body printed a literal tilde. `\newcommand{\fig}
  {Figure~7}` set `Figure~7`.
* `\nobreakspace` raised `error: \nobreakspace is not supported by this
  compiler version` and typeset **nothing** -- the space was deleted.
* `\,` `\!` `\:` `\;` `\>` inside a macro body printed the bare character,
  so `\newcommand{\ab}{A\,B}` set `A,B` in text mode.

## One cause behind all three

The lexer records a control symbol's escape only in the *width* of its span
(`\,` is the word `,` spanning two bytes), and a token copied out of a
macro's replacement text carries the span of the **invocation**, whose width
says nothing about how the token was written. Every decision made from
`token.span` there is made from unrelated bytes. `~` had the same shape one
level up: the character was carried through the text stream as the source
byte `~` and a later stage was expected to recognise the tie by reading the
source back, which replacement text makes impossible -- and which cannot be
guessed either, because `\textasciitilde` produces the same character and
must stay a tilde.

So the escape is no longer re-derived:

* `ExpandedToken::escape_span` returns the token's own bytes (the recorded
  definition bytes for replacement text), and `control_symbol_kern`'s two
  call sites read that instead of `token.span`. `token.span` stays the
  invocation span, which is what diagnostics and incremental relayout need.
* `lexer::apply_text_ligatures` -- already the one place that turns source
  characters into the characters TeX sets (`--` into an en dash, `` ` ``
  into a curly quote) -- resolves `~` to `lexer::NO_BREAK_SPACE` (U+00A0).
  The text stream is self-describing from then on. `\verb` and `verbatim`
  never reach that function, which is exactly where pdflatex also stops
  treating `~` as active, so `\verb|~/.local|` keeps its tilde.
* `text_builtins::kernel_tie` gives `\nobreakspace` the same character.
  It is deliberately **not** a row in `TEXT_SYMBOLS`: it is a kernel
  command, not a `\DeclareTextCommand`, so resolving it through the
  encoding tables is what produced `unavailable in encoding OT1` for the
  commonest space in LaTeX. The `*.dfu` row naming U+00A0 describes only
  the other direction.

The math hand-off has the same span confusion and is a separate lane's fix
(PR #286, `parser::math_token`); nothing here touches it.

## How broad this actually was

Narrower than it looks, and worth saying so. Across the 22 real-world
corpus fixtures there are **98 `~` in the sources and exactly one** that was
typeset as a literal tilde (`article-twocolumn`, a `\bibitem` continuation
line). The other 97 were already correct, because a `~` written directly in
the source has an exact span. The broken cases are macro replacement text
and `\nobreakspace`, which the corpus happens barely to exercise and real
documents do.

## Verification

`\ `-width and breakpoint behaviour were checked against pdflatex (TeX Live
2025) rather than assumed. A paragraph engineered to want to break exactly
at the tie breaks after `Figure` with a space and keeps `Figure 7` together
with `~`, which is what pdflatex does with the same source; the tie's width
is the font's interword space (3.3209bp at 10pt) and is unaffected by the
preceding sentence period, as `\ ` requires.

`cargo test --release --no-fail-fast`: **498 passed, 0 failed**, 9 new tests
in `tests/tie.rs`. The regenerated `supported-latex.json` / `coverage.md` /
`docs/user/compiler.md` and the sha256-gated Mac copy come from
`scripts/render_supported_latex.sh` and `apps/mac/scripts/sync-supported-latex.sh`.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git; no Cursor CLI is installed on
this machine, so the direct-agent route is the only available one.

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