Skip to content

compiler: retract the fabricated \sgn and make \DeclareMathOperator's spacing real - #286

Closed
GoKubar wants to merge 1 commit into
mainfrom
engine/math-operator-sgn
Closed

GoKubar wants to merge 1 commit into
mainfrom
engine/math-operator-sgn

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

compiler: retract the fabricated \sgn and make \DeclareMathOperator's spacing real

math::OPERATOR_NAMES carried 33 entries. 32 are LaTeX2e's log-like
functions (fontmath.ltx 455-503). The 33rd, \sgn, is defined by no LaTeX
layer at all: probed name by name under TeX Live 2025 with amsmath and
amssymb loaded, it is the only undefined one of the 33. FlashTeX rendered
it as upright "sgn" with zero diagnostics and published it as
"renders": true in the generated inventory the Mac editor completes from,
so a user could accept the completion, see it render here, and have pdflatex
answer "Undefined control sequence".

Deleting the row alone is not enough, because the edit-distance suggester
answers \sin for \sgn (one substitution) -- confidently wrong advice for
a name the author spelled correctly. UNDECLARED_OPERATORS gives it the
diagnostic it deserves instead: still unknown_command (pdflatex's own
answer), but with \DeclareMathOperator{\sgn}{sgn} as the suggestion --
amsopn's own documented example for exactly this name. It is deliberately an
if rather than a match arm, so tests/supported_latex.rs does not
require an inventory row for it and republish the claim being retracted.

\DeclareMathOperator is already a host-prelude definition
(expansion::HOST_PRELUDE): \cmd becomes \operatorname{text}, the
starred form \operatorname*{text}, and both land on the same
Nucleus::Operator { body, limits }. So the declared route was there -- but
amsopn's own worked example set the wrong glyphs:

\DeclareMathOperator*{\argmax}{arg\,max}   ->  arg,max
\operatorname*{arg\,max}                   ->  arg max

printing a comma the author never wrote. The cause is not in the operator
machinery: the lexer encodes a control symbol's escape in its span width
(\, becomes Word(",") spanning two source bytes) and math::MathParser:: atom, \| and the text kerns all tell \, from a literal comma by that
width alone. A token copied out of a macro's replacement text carries the
invocation's span, so the test failed and every \, \; \! \: in
every \newcommand body decayed into the bare character -- \DeclareMath Operator was only the most visible victim.

parser::math_token hands math the token's own definition bytes when they
really are \<c> (checked against the source text, not just the width).
Only the span moves, never the token kind, and definition/
maps_to_invocation are left alone so the expansion-site mapping is
untouched.

Oracle (pdflatex, TeX Live 2025, never in the product path): pdflatex sets
\DeclareMathOperator*{\argmax}{arg\,max} at glyph positions identical to
the written-out \operatorname*{arg\,max} -- arg at 133.768, max at
149.436 -- which is the equality this now reproduces.

  • crates/compiler cargo test --release --no-fail-fast: 0 failed; empty
    failing set, so nothing to compare against a control. 6 new tests.
  • tests/math_operators.rs proved non-vacuous: with the parser.rs hunk
    stashed, a_declared_operator_keeps_the_math_glue_written_in_its_text and
    control_symbols_survive_a_macro_body both FAIL.
  • real-world corpus, all 22 fixtures, compiler producer: status, page count
    and full diagnostic set byte-identical to a control build of the same base
    (5e6c65be). No fixture uses an undeclared \sgn or a control symbol in
    a macro body; math-sheet's \operatorname{sgn} is legal LaTeX and
    unaffected.
  • crates/render-pipeline and the amsmath/amssymb oracle corpora read
    vendor/compiler, not this crate, so they cannot move until an integrator
    re-pins; none of them is affected by this change either way.

apps/mac/Sources/FlashTeXMac/Resources/supported-latex.json is a
byte-identical copy of the compiler's inventory -- that is the whole point of
it, and CompletionTests.testBundledInventoryMatchesTheCompiler compares the
two by sha256, as does apps/mac/scripts/sync-supported-latex.sh --check in
make-app.sh. So this PR must carry the re-synced copy or it turns that gate
red; it is produced by that script and is exactly the one deleted row.

That copy is the completion vocabulary Completion.Vocabulary decodes, so
the concrete user-visible change is: the Mac editor no longer autocompletes
\sgn as a supported command. Accepting that completion used to produce a
document FlashTeX rendered cleanly and pdflatex refused -- the worst failure
shape for an engine whose contract is matching pdflatex.

This is a class of bug, not a one-off. Another lane found the same shape
in supported.rs for \rotatebox, \scalebox, \resizebox and
\reflectbox: advertised to the editor as supported when the pipeline never
applies them. \sgn differs only in being undefined everywhere rather
than defined but not applied; the fix is the same in both cases -- the
inventory must describe what actually happens, because it is API. Those four
are being handled separately.

Also found and left alone: all six real amsmath operators (\injlim,
\projlim, \varinjlim, \varprojlim, \varliminf, \varlimsup) are
absent everywhere, so real amsmath is reported as a typo -- the inverse gap.
amsopn.sty defines them in limits mode and sets \injlim as inj\,lim,
so adding them as plain text atoms would produce wrong output; it needs
doing properly.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git (Cursor usage-limit fallback).

Co-Authored-By: Claude Opus 5 [email protected]
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

@GoKubar
GoKubar force-pushed the engine/math-operator-sgn branch from f889465 to 0ac39dc Compare September 14, 2026 05:41
… spacing real

`math::OPERATOR_NAMES` carried 33 entries. 32 are LaTeX2e's log-like
functions (`fontmath.ltx` 455-503). The 33rd, `\sgn`, is defined by no LaTeX
layer at all: probed name by name under TeX Live 2025 with `amsmath` and
`amssymb` loaded, it is the only undefined one of the 33. FlashTeX rendered
it as upright "sgn" with zero diagnostics and published it as
`"renders": true` in the generated inventory the Mac editor completes from,
so a user could accept the completion, see it render here, and have pdflatex
answer "Undefined control sequence".

Deleting the row alone is not enough, because the edit-distance suggester
answers `\sin` for `\sgn` (one substitution) -- confidently wrong advice for
a name the author spelled correctly. `UNDECLARED_OPERATORS` gives it the
diagnostic it deserves instead: still `unknown_command` (pdflatex's own
answer), but with `\DeclareMathOperator{\sgn}{sgn}` as the suggestion --
amsopn's own documented example for exactly this name. It is deliberately an
`if` rather than a `match` arm, so `tests/supported_latex.rs` does not
require an inventory row for it and republish the claim being retracted.

`\DeclareMathOperator` is already a host-prelude definition
(`expansion::HOST_PRELUDE`): `\cmd` becomes `\operatorname{text}`, the
starred form `\operatorname*{text}`, and both land on the same
`Nucleus::Operator { body, limits }`. So the declared route was there -- but
amsopn's own worked example set the wrong glyphs:

    \DeclareMathOperator*{\argmax}{arg\,max}   ->  arg,max
    \operatorname*{arg\,max}                   ->  arg max

printing a comma the author never wrote. The cause is not in the operator
machinery: the lexer encodes a control symbol's escape in its *span width*
(`\,` becomes `Word(",")` spanning two source bytes) and `math::MathParser::
atom`, `\|` and the text kerns all tell `\,` from a literal comma by that
width alone. A token copied out of a macro's replacement text carries the
*invocation's* span, so the test failed and every `\,` `\;` `\!` `\:` in
every `\newcommand` body decayed into the bare character -- `\DeclareMath
Operator` was only the most visible victim.

`parser::math_token` hands math the token's own definition bytes when they
really are `\<c>` (checked against the source text, not just the width).
Only the span moves, never the token kind, and `definition`/
`maps_to_invocation` are left alone so the expansion-site mapping is
untouched.

Oracle (pdflatex, TeX Live 2025, never in the product path): pdflatex sets
`\DeclareMathOperator*{\argmax}{arg\,max}` at glyph positions identical to
the written-out `\operatorname*{arg\,max}` -- `arg` at 133.768, `max` at
149.436 -- which is the equality this now reproduces.

- `crates/compiler` `cargo test --release --no-fail-fast`: 0 failed; empty
  failing set, so nothing to compare against a control. 6 new tests.
- `tests/math_operators.rs` proved non-vacuous: with the `parser.rs` hunk
  stashed, `a_declared_operator_keeps_the_math_glue_written_in_its_text` and
  `control_symbols_survive_a_macro_body` both FAIL.
- real-world corpus, all 22 fixtures, compiler producer: status, page count
  and full diagnostic set byte-identical to a control build of the same base
  (`5e6c65be`). No fixture uses an undeclared `\sgn` or a control symbol in
  a macro body; `math-sheet`'s `\operatorname{sgn}` is legal LaTeX and
  unaffected.
- `crates/render-pipeline` and the amsmath/amssymb oracle corpora read
  `vendor/compiler`, not this crate, so they cannot move until an integrator
  re-pins; none of them is affected by this change either way.

`apps/mac/Sources/FlashTeXMac/Resources/supported-latex.json` is a
byte-identical copy of the compiler's inventory -- that is the whole point of
it, and `CompletionTests.testBundledInventoryMatchesTheCompiler` compares the
two by sha256, as does `apps/mac/scripts/sync-supported-latex.sh --check` in
`make-app.sh`. So this PR must carry the re-synced copy or it turns that gate
red; it is produced by that script and is exactly the one deleted row.

That copy is the completion vocabulary `Completion.Vocabulary` decodes, so
the concrete user-visible change is: the Mac editor no longer autocompletes
`\sgn` as a supported command. Accepting that completion used to produce a
document FlashTeX rendered cleanly and pdflatex refused -- the worst failure
shape for an engine whose contract is matching pdflatex.

**This is a class of bug, not a one-off.** Another lane found the same shape
in `supported.rs` for `\rotatebox`, `\scalebox`, `\resizebox` and
`\reflectbox`: advertised to the editor as supported when the pipeline never
applies them. `\sgn` differs only in being *undefined everywhere* rather
than *defined but not applied*; the fix is the same in both cases -- the
inventory must describe what actually happens, because it is API. Those four
are being handled separately.

Also found and left alone: all six real amsmath operators (`\injlim`,
`\projlim`, `\varinjlim`, `\varprojlim`, `\varliminf`, `\varlimsup`) are
absent everywhere, so real amsmath is reported as a typo -- the inverse gap.
`amsopn.sty` defines them in limits mode and sets `\injlim` as `inj\,lim`,
so adding them as plain text atoms would produce wrong output; it needs
doing properly.

Implementation-Agent: claude-opus-5 (engine lane, linux-primary/NixOS).
Commit-Executor: claude-opus-5 direct git (Cursor usage-limit fallback).

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