Skip to content

Inline maths simplification: keep numbers as maths (replaceNumber: false) - #128

Merged
mmcky merged 1 commit into
mainfrom
fix/inline-math-numbers-127
Sep 21, 2026
Merged

mmcky merged 1 commit into
mainfrom
fix/inline-math-numbers-127

Conversation

@mmcky

@mmcky mmcky commented Sep 21, 2026

Copy link
Copy Markdown

Closes #127.

The defect

inlineMathSimplificationPlugin is wired in the document pipeline with { replaceSymbol: false }, which leaves its two number rules on. replaceNumber turns an inline maths node whose whole value is a number into a text node carrying the literal source string, and replaceNumberedSubSuperScripts turns a number with a numeric script into a span. So a standalone $2.5$ is set in the prose face while the same value inside $x = 2.5$ is set by KaTeX, and $-1$ comes out with a U+002D hyphen-minus where KaTeX (and MathJax on the Sphinx sites) emits U+2212. The rewrite happens in the engine, before rendering, so no theme CSS can reach it. About 1,205 inline expressions across the eight Python lecture repositories are affected, 76 of them with a minus sign.

The change

One line in packages/myst-cli/src/process/mdast.ts — both number rules are gated on the single replaceNumber option, so turning it off sends every number back through KaTeX and removes the face mismatch and the hyphen together:

.use(inlineMathSimplificationPlugin, { replaceSymbol: false, replaceNumber: false })

packages/myst-transforms/src/mathSimplifications.ts is untouched: the option and its gates have been upstream since jupyter-book#2115, which stopped replacing symbols by default after the same complaint in jupyter-book#1604 and jupyter-book#1681. This extends that argument to numbers, which the maintainer's stated rationale for the transform — it helps where "people reach for latex math when they mean to write text, e.g. $^2$ $\pm$ $\degree$C" — never covered. In "let $c$ lie strictly between $-1$ and $1$" the numbers are mathematical objects, not typing shortcuts.

Source Before After
$2.5$, $+3$, $0$ text "2.5", "+3", "0" inlineMath, typeset by KaTeX
$-1$, $-0.5$ text whose first code point is U+002D inlineMath; KaTeX renders U+2212
$10^{-4}$, $10^4$ span → text + superscript (script text also U+002D) inlineMath
$x = -1$, $\pm$, $\degree$, 37$^\circ$C unchanged unchanged
$^2$, $^{st}$, m$^2$ already inlineMath (replaceSymbol: false switches that rule off too) unchanged

The blast radius is exactly the two regexes /^[+-]?[0-9.]+$/ and /^[+-]?[\d.]+(\^|_)(\{[+-]?[\d.]+\}|[+-]?[\d.]+)$/. Nothing else in the transform behaves differently: the change is strictly subtractive, and no dormant rule starts firing. The degree special case (37$^\circ$C → 37°C) sits above the replaceSymbol === false bail, so it survives, and $\pm$ / $\degree$ come through replaceText, which this does not touch.

Tested

The full gate passes locally: bun run build, bun run test (72/72 turbo tasks, including the 56-case end-to-end CLI suite), bun run lint:format, bun run lint, bun run lint:circular. No existing test or fixture changes — no tracked fixture in the repo contains bare-number inline maths, and the AST-level exporter suites bypass this pipeline.

End-to-end through the real CLI, same scratch project built before and after (myst build --tex, which runs transformMdast):

Output
before Let $c$ be a real number that lies strictly between -1 and 1, with 2.5, +3, 10\textsuperscript{-4} and 10\textsuperscript{4}.
after Let $c$ be a real number that lies strictly between $-1$ and $1$, with $2.5$, $+3$, $10^{ -4}$ and $10^4$.

Both builds left Unchanged: $x = -1$, 37°C, $\pm$, m$^2$, $\alpha$. identical, confirming the shortcuts are unaffected. (The space in $10^{ -4}$ is pre-existing stringToLatexMath behaviour for all inline maths and is invisible in TeX.) Rendering -1 through the resolved KaTeX 0.16.47 gives visible glyphs U+2212 U+0031 and a MathML <mo> of U+2212; the ASCII hyphen survives only in the <annotation> element that carries the TeX source for copy-paste.

New regression rows in packages/myst-transforms/src/mathSimplifications.spec.ts cover what nothing covered before: the bare-number rule and the superscript form of the numbered-script rule in their disabled state, plus the exact option pair this pipeline now passes, with $^{\circ}$ and $\pm$ as guards that the shortcuts still simplify under that pair. 38 tests pass.

Things a reviewer should know

Exports change too, not just the HTML site: a number that stays inlineMath now round-trips as maths — $2.5$ in LaTeX and Typst, <inline-formula> in JATS, {math}`2.5` in Markdown and ipynb, an OMML equation run in docx. That is closer to the source, which really was $2.5$, and it incidentally removes four pre-existing failures on the span node the old rule produced: myst-to-md and the ipynb export raised "Unsupported node type: span" and dropped $10^{-4}$ silently, myst-to-docx raised a docx-renderer error for the same node, and myst-to-html emitted a block <div> inside <h1>/<p>.

Page JSON grows, because each number now carries a baked KaTeX HTML string: measured at +35% raw and +37% gzipped on a synthetic page of 160 such nodes. Real lectures already carry heavy maths, so the relative growth there will be smaller.

Plain-text extraction is unaffected for bare numbers (toText returns the node's value either way), and heading anchors, page titles and image alt text are computed before this step, so no deep links change. The one plain-text difference is $10^{-4}$ in a heading or caption, which now extracts as 10^{-4} rather than the misleading 10-4 in TOC entries and myst.xref.json titles.

The only content in this repo the change touches is $1$ at docs/proofs-and-theorems.md:199, inside a live {myst} demo block — it moves from the prose face to KaTeX, and nothing asserts on it.

The wiring itself is still not pinned by a test: the option object is an inline literal and transformMdast cannot run without a Session, so a future upstream sync that drops the option would go unnoticed. Pinning it means hoisting the literal to an exported const, or adding an end-to-end fixture. I left the call site as a single line because packages/myst-transforms/src/mathSimplifications.ts and this line are byte-identical to upstream/main, which makes this the cleanest cherry-pick candidate in UPSTREAM-PRS.yml — happy to add either pin if you would rather have the guard.

Bookkeeping

No quantecon/VERSION.yml row and no quantecon/UPSTREAM-PRS.yml candidate in this PR — both need the squash sha, and the repo's convention is a separate chore(quantecon) PR afterwards (the #89 → #91 and #81 → #82 pattern). The follow-up entry would be type: bug-fix, status: pending, with notes naming #127 and the jupyter-book#1604 / jupyter-book#1681 / jupyter-book#2115 chain.

🤖 Generated with Claude Code

…lse)

The document pipeline wires `inlineMathSimplificationPlugin` with
`{ replaceSymbol: false }`, which leaves its two number rules on:
`replaceNumber` turns an inline maths node whose whole value is a number
into a `text` node carrying the literal source, and
`replaceNumberedSubSuperScripts` turns `$10^{-4}$` into a `span` of text
plus superscript. A standalone `$2.5$` is therefore set in the prose face
while the same value inside `$x = 2.5$` is set by KaTeX, and `$-1$` emits
U+002D hyphen-minus where KaTeX emits U+2212. About 1,205 inline
expressions across the eight Python lecture repositories are affected, 76
of them with a minus sign.

Both rules are gated on the single `replaceNumber` option, so passing
`replaceNumber: false` alongside the existing `replaceSymbol: false` sends
every number back through KaTeX and removes the face mismatch and the
hyphen together. The typing shortcuts the transform exists for are
untouched: `$\pm$`, `$\degree$` and `37$^\circ$C` still simplify, and
`$^2$` / `$^{st}$` were already left as maths by `replaceSymbol: false`.

The transform itself is unchanged. `replaceNumber` and its gates have been
in upstream since jupyter-book#2115, which stopped replacing
symbols by default after the same complaint in jupyter-book#1604
and jupyter-book#1681; this extends that argument to numbers.

Fixes #127.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Copilot AI lite review requested due to automatic review settings September 21, 2026 01:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The change is narrowly scoped, matches the stated defect, and is backed by targeted regression tests plus an appropriate changeset.

Review effort: Lite
Findings: None

What changed in this PR

This PR adjusts the MyST CLI document transform pipeline to stop rewriting inline-math expressions that are purely numeric into non-math nodes, ensuring they continue through KaTeX for consistent typography (notably proper minus signs) and more faithful round-tripping across export formats.

Changes:

  • Wire inlineMathSimplificationPlugin with { replaceSymbol: false, replaceNumber: false } in the CLI mdast pipeline so bare-number inline math remains inlineMath.
  • Add transform-level regression tests covering replaceNumber: false behavior for bare numbers and numbered super/subscript forms, including the exact option pair used by the CLI.
  • Add a changeset documenting the behavior change for myst-cli.
File Description
packages/​myst-cli/​src/​process/​mdast.ts Disables numeric inline-math rewrites in the CLI pipeline by passing replaceNumber: false.
packages/​myst-transforms/​src/​mathSimplifications.spec.ts Adds coverage ensuring numeric-only inline math stays inlineMath when replaceNumber is disabled (including the CLI option pair).
.changeset/​inline-math-numbers.md Documents the patch-level change in myst-cli behavior and its export implications.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mmcky mmcky added the bug Something isn't working label Sep 21, 2026
@mmcky
mmcky merged commit e92e08f into main Sep 21, 2026
7 checks passed
@mmcky
mmcky deleted the fix/inline-math-numbers-127 branch September 21, 2026 03:39
mmcky added a commit that referenced this pull request Sep 21, 2026
…ata (#129)

VERSION.yml gains row 16 for the inline-maths number fix (squash e92e08f,
local PR #128), and UPSTREAM-PRS.yml gains the file's first `bug-fix`
candidate — a fix for a defect in upstream's own wiring, whose notes name
the fork issue it settles and carry the upstream argument for when the PR
is opened.

The tag metadata is folded into the same PR: qe_version becomes qe-v12 and
row 16 is tagged, so the tree at the tagged commit is self-consistent.
upstream_base and upstream_commit are unchanged at 1.10.1 / d09d3e5 —
`git describe --tags --match 'mystmd@*'` on the merge-base still reports
[email protected], since no sync merge has landed since qe-v11.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
mmcky added a commit to QuantEcon/test-lecture-theme-mystmd that referenced this pull request Nov 2, 2026
…#10)

The fork moves from qe-v11 (83685347) to qe-v12 (53726083, cut
2026-09-21). qe-v12 carries one behaviour change over qe-v11, and no
upstream sync or dependency change. QuantEcon/mystmd#128 adds
`replaceNumber: false` beside `replaceSymbol: false` where myst-cli
wires inlineMathSimplificationPlugin. A number written as inline maths
($2.5$, $-1$, $10^{-6}$) therefore stays inlineMath and KaTeX typesets
it, in the maths face with a true minus sign (U+2212). qe-v11 rewrote
it to prose text, with a hyphen for the minus (QuantEcon/mystmd#127).

On this corpus that changes two pages and nothing else. Built with
both tags and diffed with per-build ids masked, math.json goes from 29
to 39 inlineMath nodes (the numbers-as-inline-maths paragraph and
table, #8) and lp-intro.json from 94 to 117 (its 23 bare numbers).
Those are the Sphinx site's counts for the same pages. The other 15
pages and the build messages are unchanged.

The install step's comment now says qe_version is on line 52 of
VERSION.yml at qe-v12, which grew four comment lines; the grep does not
depend on the line. The README's Building section names qe-v12.
lectures/ is untouched, so sync-check is unaffected.

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
mmcky pushed a commit to QuantEcon/lecture-python-programming that referenced this pull request Nov 5, 2026
Move the pinned QuantEcon/mystmd commit from c1f274b8 (2026-09-10, just
after qe-v10) to the qe-v14 tag, 8be13b73 (2026-11-04). In between, the
fork took three upstream syncs (to mystmd 1.12.0) and one fork change
that alters output (QuantEcon/mystmd#128, qe-v12). The fork's other
changes since are release metadata and a build fix
(QuantEcon/mystmd#125). myst-execute has no source change between the
two commits.

Built this branch's lectures with both engines (unexecuted, site JSON
diffed with per-build ids masked): 27 pages each, the 48 build
messages identical, 22 of 31 output files identical. Two changes reach
readers:

- Numbers written as inline maths stay maths, typeset by KaTeX
  (QuantEcon/mystmd#128): six numbers in five lectures, autodiff ($0$),
  numba ($1.6$), python_oop ($10$), sympy ($1$, $0$) and
  writing_good_code ($1.25$).
- Captioned code cells put their outputs before the caption
  (jupyter-book/mystmd#3059). workspace.md has three, captioned
  sine_wave.py, sine_wave.py and second_script.py. Rendered with
  execution, each filename label moves from between the code and its
  output to below the output (two plots, one "Writing sine_wave.py").
  The Sphinx site does not render these captions at all.


Claude-Session: https://claude.ai/code/session_01S8kjPDFugbLYr8XzNRAZCR

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline maths simplification rewrites numbers to text: prose face, and a hyphen for a minus sign (1,205 lecture uses; fix: replaceNumber: false)

2 participants