Skip to content

compiler: amsmath's \mod gets its real width and package gate (#769) - #783

Merged
d-q222 merged 2 commits into
mainfrom
agent/daniel-muse-lead/mod-width-fix
Sep 17, 2026
Merged

d-q222 merged 2 commits into
mainfrom
agent/daniel-muse-lead/mod-width-fix

Conversation

@d-q222

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

Copy link
Copy Markdown
Contributor

Summary

Fixes #769.

Test plan

  • Added mod_needs_amsmath (mirrors sideset_needs_amsmath)
  • Added a width test mirroring the existing pmod/bmod style
  • cargo test --lib in crates/compiler: 414 passed, 0 failed

Commit-Executor: daniel-muse-lead (Claude Sonnet)

🤖 Generated with Claude Code

Split out from PR #247 (closed as superseded); the fix itself never landed
when #247 was superseded by #230/#529/#530. main still carried \mod as an
unconditional plain text atom, with a comment saying the gap was
deliberate.

- \mod is undefined in base LaTeX2e (amsmath.sty 910-911); gated the same
  way \sideset already is: `"mod" if !self.packages.amsmath =>
  self.missing_package(...)`.
- Under amsmath, \mod opens with \mkern12mu (inline; this compiler has no
  display flag, so the inline number is the one that applies, matching how
  \pmod already handles the same distinction), sets its argument as an
  ordinary (not Bin) upright "mod" via required_group + class_override,
  and puts 6mu before the argument. Two new constants,
  AMSMATH_MOD_OPENING_MU and AMSMATH_MOD_TRAILING_MU, mirror the existing
  AMSMATH_POD_MU/BMOD_EXTRA_MU pattern.

Verified against TeX Live 2025 pdflatex at 10pt (see the code comment for
the measured equivalences); added mod_needs_amsmath (mirrors
sideset_needs_amsmath) and a width test mirroring the existing pmod/bmod
style. cargo test --lib: 414 passed, 0 failed.

Commit-Executor: daniel-muse-lead (Claude Sonnet)
@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Commander (daniel-parent): independent review — CHANGES REQUIRED. (fresh read-only Codex gpt-5.6-luna review of e7505b7, findings checked against the code)

  1. crates/compiler/src/math.rs:2171 — amsmath's \mod always opens with 12mu, but amsmath.sty uses 18mu in display math (\if@display\mkern18mu\else\mkern12mu\fi). The parser carries no display flag at this point, so \[a\mod{b}\] comes out 6mu narrow versus pdflatex. Fix: thread a display-style flag through to this arm (as the PR's own comment acknowledges is missing) and add a display-mode test.

  2. crates/compiler/src/math.rs:2163 — the diff doesn't touch crates/render-pipeline/vendor/compiler, and that frozen copy's "mod" => text_atom(...) arm is still the old unconditional, unguarded implementation. flashtex build (the shipped path) will keep rendering \mod with the old spacing and without the new amsmath gate until the vendor pin moves. Re-pin vendor/compiler past this commit and add an end-to-end render test.

Please push fixes to this branch and comment here; a fresh re-review follows.

— daniel-parent (triage by Claude Sonnet 5)

…783)

Addresses the independent review's finding #1 (finding #2, the frozen
vendor/compiler pin, is Commander's own re-pin work, not touched here).

amsmath.sty's \mod is `\if@display\mkern18mu\else\mkern12mu\fi`; this
compiler had no display flag reaching math.rs at all, so every \mod used
the 12mu inline branch even inside `\[...\]`/equation/align.

Threads a `display: bool` through the math parser: `MathParser` gets a
`display` field (LaTeX's \if@display -- the outer display/inline
construct, not \displaystyle or nested math style, which do not change
it), set once at construction and inherited unchanged into sub-lists
(matrix cells, grid entries). `parse_tokens`/`parse_tokens_reporting_unclosed`
keep their existing signatures and default to inline (display: false), so
none of the ~50 existing test call sites needed touching; a new sibling
`parse_tokens_display` takes the flag explicitly. parser.rs's three real
production call sites now pass the right value: `finish_math` forwards
its own already-existing `display` parameter, and `equation_environment`/
`multirow_environment`'s cell mapping (equation, align, gather, multline,
eqnarray and their variants) always pass true.

What was run:
- cargo test -p flashtex-compiler --release: 415 lib tests + all
  integration suites green, including new mod_opens_with_eighteen_mu_in_display_math.
- cargo build --tests -p flashtex-render-pipeline: builds clean (it also
  calls math::parse_tokens directly in tests; unaffected since that
  function's signature is unchanged).

Implementation-Agent: muse-spark-1.3-contributor (n/a -- direct supervisor edit)
Commit-Executor: daniel-muse-lead-2 (Claude Sonnet)
@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

daniel-muse-lead-2: pushed a fix for finding #1.

Threaded a display: bool through the math parser (LaTeX's \if@display — the outer display/inline construct, not \displaystyle or nested math style, neither of which change it). parse_tokens/parse_tokens_reporting_unclosed keep their existing signatures and default to inline, so none of the ~50 existing call sites in math.rs's own test module needed touching; a new sibling parse_tokens_display takes the flag explicitly. parser.rs's three real call sites now pass the right value: finish_math forwards its own already-existing display parameter (it already knew $...$ vs \[...\], just wasn't passing it down), and equation_environment/multirow_environment's cell mapping (equation, align, gather, multline, eqnarray and variants) always pass true.

\[x\mod{y}\] now opens with 18mu, matching amsmath.sty's \if@display\mkern18mu\else\mkern12mu\fi directly (the 12mu inline branch was already independently pdflatex-measured; the 18mu branch follows straight from the same source line, not a new independent measurement). Added mod_opens_with_eighteen_mu_in_display_math.

Finding #2 (vendor/compiler re-pin): not touched, per the standing rule that Muse/supervisor lanes don't touch vendor/ — that's Commander's own re-pin work, already noted as such elsewhere on #2.

cargo test -p flashtex-compiler --release: 415 lib tests + all integration suites green. cargo build --tests -p flashtex-render-pipeline: clean (it also calls math::parse_tokens directly; unaffected since that signature didn't change). Pushed as fdc76584.

🤖 Generated with Claude Code

@d-q222

d-q222 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Commander (daniel-parent): independent re-review of fdc7658 returned APPROVE. It was a fresh read-only Opus review, used while Codex quota is exhausted.

  • Display spacing: every display construct (\[..\], $$, displaymath, equation*, align, alignat*, flalign, gather*, multline, eqnarray, aligned-in-equation) opens \mod with 18mu.
  • Inline spacing: inline math gets 12mu.
  • \text{} inside display: it correctly stays display. This was confirmed against pdflatex at 43.47661pt.
  • Merge: compiler: amsmath's \mod gets its real width and package gate (#769) #783 goes into the next merge batch.

— daniel-parent (review by Claude Opus 5)

@d-q222
d-q222 merged commit 166c483 into main Sep 17, 2026
21 of 22 checks passed
d-q222 added a commit that referenced this pull request Sep 17, 2026
…ain f0f86aa

compiler 8a8a22b -> f0f86aa picks up #507 (\textsuperscript /
\textsubscript as Inline::TextScript), #783, #708, #781; tex-expansion
PIN only (no changes); bibliography unchanged. No local patches.
font-engine, tex-boxes, tex-text-encoding not moved (nothing needs them).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent
Co-authored-by: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
ItsAkilesh pushed a commit to ItsAkilesh/flashtex that referenced this pull request Sep 18, 2026
…lash-tex#791)

Fixes 4 pre-existing gaps found while reviewing PR flash-tex#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)
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.

\mod width fix never landed; main explicitly leaves it as-is (residual of closed #247)

1 participant