Skip to content

compiler(math): intertext math, nested $ in \text, \pod, \allowbreak (#791) - #797

Merged
d-q222 merged 1 commit into
mainfrom
agent/daniel-muse-lead/math-gaps-791
Sep 17, 2026
Merged

d-q222 merged 1 commit into
mainfrom
agent/daniel-muse-lead/math-gaps-791

Conversation

@d-q222

@d-q222 d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #791: 4 pre-existing gaps found while reviewing PR #783.

What was wrong

  1. Math inside \intertext{...}/\shortintertext{...} was silently
    flattened to plain text
    — the general text-mode inline parser
    (inlines_from_tokens, also used by headings/captions/style arguments)
    had no $...$/\(...\)/\[...\] handling at all.
  2. $..$ inside \text{} inside inline math broke the parse —
    math.rs's text-group reader already supported this correctly, but the
    outer scan that finds where a $...$/\(...\)/\[...\] region ends
    closed at the first inner delimiter regardless of what was inside
    \text{}'s braces.
  3. \pmod ignored display style even though a separate PR had already
    threaded a display: bool through the math parser specifically to
    drive this (the old code comment said so and was stale). \pod
    itself was entirely unimplemented.
  4. \allowbreak had no math-mode dispatch arm at all (unknown-command
    inside $...$), despite existing in text mode.

The fixes

  1. Added flat_math to inlines_from_tokens, mirroring the main token
    loop's math-shift handling.
  2. Added selective brace-depth tracking to the outer math-region scanners
    (dollar_math, paren_math, bracket_math, flat_math): a delimiter
    is only suppressed inside a group opened immediately by one of the six
    text-mode-switching commands (text, textit, textrm, textnormal,
    mbox, hbox), never for ordinary math grouping like x^{...}.
  3. \pmod now reads self.display for amsmath's real
    \if@display\mkern18mu\else\mkern8mu\fi. \pod implemented: same
    package gate and display split as \pmod, but (arg) with no "mod"
    text.
  4. \allowbreak in math is a documented, layout-neutral no-op (the math
    layout has no breakpoint concept — a formula is one unbreakable box).

A regression caught before it ever reached this branch

An earlier version of fix #2 tracked brace depth for every {, which
silently changed an existing recovery-diagnostic fixture for an unrelated,
already-broken (unclosed ordinary brace) case. Caught by running the
full cargo test suite (not a filtered subset) before publishing —
the fix was corrected to only track depth inside the six specific
text-switching commands before this ever reached the branch history.

Tests

cargo test --lib in crates/compiler: 436 passed, 0 failed (8 new).
Full cargo test (every binary, not a filtered subset): all green,
including recovery_evidence_is_current (no snapshot regen needed — the
final fix is byte-identical to pre-existing behavior outside the six
text-switching commands). Independently re-run in the exact worktree this
was published from, not just the authoring lane's self-report.

Provenance

Muse lane math-gaps-791 (muse-spark-1.3-contributor), slices 1-2 —
slice 1 implemented all 4 fixes, slice 2 fixed the brace-depth regression
I found during independent full-suite verification. I reviewed and
independently re-ran everything before publishing; full trailers in the
commit.

🤖 Generated with Claude Code

…791)

Fixes 4 pre-existing gaps found while reviewing PR #783:

1. Math inside `\intertext{...}`/`\shortintertext{...}` was silently
   flattened to plain text: `inlines_from_tokens` (the general text-mode
   inline parser also used by headings, captions and style arguments) had
   no `$...$`/`\(...\)`/`\[...\]` handling at all — the delimiters fell
   into a catch-all and vanished. Added `flat_math`, mirroring the main
   token loop's math-shift handling (including `$$...$$` display), with a
   brace-depth-aware close scan and an unclosed-delimiter diagnostic.

2. `$..$` inside `\text{}` inside inline math broke the parse:
   `required_text_group`/`required_text_group_styled` in `math.rs` already
   supported a nested `$` correctly, but the *outer* `dollar_math` (and its
   `\(...\)`/`\[...\]` siblings, plus the new `flat_math`) scan closed the
   outer formula at the first `$`/`\)`/`\]` regardless of what was inside
   `\text{}`'s braces. Added selective brace-depth tracking: a delimiter is
   only suppressed inside a group opened immediately by one of the six
   text-mode-switching commands (`text`, `textit`, `textrm`, `textnormal`,
   `mbox`, `hbox` — verified against `math.rs`'s actual dispatch, not
   assumed), never for ordinary math grouping (`x^{...}`, `\frac{...}{...}`).
   An earlier version of this fix tracked depth for every `{`, which
   silently changed a pre-existing recovery diagnostic for an unrelated,
   unclosed-ordinary-brace fixture; caught by running the FULL test suite
   (not a filtered subset) before publishing, and fixed before this push —
   the regression never reached this branch's history.

3. `\pmod` still opened with a fixed 8mu/18mu split regardless of display
   style, even though a separate, already-merged PR threaded a `display:
   bool` flag through the math parser specifically to drive this (the old
   code comment said so explicitly and was stale). Now uses `self.display`:
   amsmath's real `\if@display\mkern18mu\else\mkern8mu\fi`, kernel always
   18mu. `\pod` itself had no dispatch arm at all (unknown-command) —
   implemented it too: amsmath-gated like `\mod`, `(arg)` with no "mod"
   text and no leading 6mu, same display/inline split as `\pmod`.

4. `\allowbreak` had no math-mode dispatch arm (unknown-command inside
   `$...$`), despite existing in text mode. The math layout has no
   breakpoint concept (a formula lays out as one unbreakable box), so this
   is recognised as a documented, layout-neutral no-op: a zero-width space
   atom that `atom_class` skips like any other kern, byte-identical widths
   with or without it.

Regenerated `docs/user/compiler.md`, both `supported-latex.json` copies and
`supported/coverage.md` via `render_supported_latex.sh` (new `pod`/
`allowbreak` inventory rows), since `\pod` and math `\allowbreak` are now
real dispatch arms `tests/supported_latex.rs` checks for.

Tests: `cargo test --lib` in `crates/compiler`: 436 passed, 0 failed (8 new:
intertext-math, nested-delimiter x2 regression-fix pair, pmod-display,
pod x2, allowbreak, plus the two selective-brace-tracking regression
tests). Full `cargo test` (every binary, not a filtered subset): all green,
including `recovery_evidence_is_current` (the committed snapshot needed no
regeneration — the final fix is byte-identical to pre-existing behavior
outside the six text-switching commands). Independently re-run in this
exact worktree from this commit.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane math-gaps-791, slices 1-2)
Commit-Executor: daniel-muse-lead (Claude Sonnet — the lane's sandbox could not reach this worktree's git metadata to commit itself; verified and committed the lane's working-tree diff directly, unmodified)
@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Commander (daniel-parent): the independent review returned APPROVE. It was a fresh read-only Opus review.

  • Tests: compiler lib 436/0 with all 8 new tests present, recovery 5/0 (plus one pre-existing ignore), supported_latex 8/0.
  • pdflatex check: \pmod measures 44.29967pt inline and 49.85509pt display (18mu vs 8mu), and \pod matches too. Math inside \intertext and $..$ inside \text{} inside inline math are both fixed.
  • Brace-depth change: no regressions, and math in headings, captions and \href text is now kept.

Follow-ups, filed separately and not blocking: math inside \intertext should be display (5.56pt too narrow for \pmod), and the amsmath.sty line-number comments are stale. #797 goes into the next merge batch once main CI is green again. — daniel-parent (review by Claude Opus 5)

@d-q222
d-q222 merged commit 45ddaef into main Sep 17, 2026
21 of 22 checks passed
d-q222 added a commit that referenced this pull request Sep 17, 2026
#797 gave \allowbreak its math-mode inventory description; the completion
test still expected the old '\penalty0' detail and failed on main CI
(45ddaef). CompletionTests passes locally.

Commit-Executor: daniel-parent
Co-authored-by: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant