Repository navigation
Inline maths simplification: keep numbers as maths (replaceNumber: false) - #128
Merged
Merged
Conversation
…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]>
There was a problem hiding this comment.
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
inlineMathSimplificationPluginwith{ replaceSymbol: false, replaceNumber: false }in the CLI mdast pipeline so bare-number inline math remainsinlineMath. - Add transform-level regression tests covering
replaceNumber: falsebehavior 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
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]>
This was referenced Sep 21, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #127.
The defect
inlineMathSimplificationPluginis wired in the document pipeline with{ replaceSymbol: false }, which leaves its two number rules on.replaceNumberturns an inline maths node whose whole value is a number into atextnode carrying the literal source string, andreplaceNumberedSubSuperScriptsturns a number with a numeric script into aspan. 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 singlereplaceNumberoption, so turning it off sends every number back through KaTeX and removes the face mismatch and the hyphen together:packages/myst-transforms/src/mathSimplifications.tsis 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$2.5$,$+3$,$0$text"2.5", "+3", "0"inlineMath, typeset by KaTeX$-1$,$-0.5$textwhose first code point is U+002DinlineMath; KaTeX renders U+2212$10^{-4}$,$10^4$span→text+superscript(script text also U+002D)inlineMath$x = -1$,$\pm$,$\degree$,37$^\circ$C$^2$,$^{st}$,m$^2$inlineMath(replaceSymbol: falseswitches that rule off too)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 thereplaceSymbol === falsebail, so it survives, and$\pm$/$\degree$come throughreplaceText, 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 runstransformMdast):Let $c$ be a real number that lies strictly between -1 and 1, with 2.5, +3, 10\textsuperscript{-4} and 10\textsuperscript{4}.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-existingstringToLatexMathbehaviour for all inline maths and is invisible in TeX.) Rendering-1through 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.tscover 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
inlineMathnow 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 thespannode the old rule produced:myst-to-mdand the ipynb export raised "Unsupported node type: span" and dropped$10^{-4}$silently,myst-to-docxraised a docx-renderer error for the same node, andmyst-to-htmlemitted 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 (
toTextreturns the node'svalueeither 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 as10^{-4}rather than the misleading10-4in TOC entries andmyst.xref.jsontitles.The only content in this repo the change touches is
$1$atdocs/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
transformMdastcannot 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 becausepackages/myst-transforms/src/mathSimplifications.tsand this line are byte-identical toupstream/main, which makes this the cleanest cherry-pick candidate inUPSTREAM-PRS.yml— happy to add either pin if you would rather have the guard.Bookkeeping
No
quantecon/VERSION.ymlrow and noquantecon/UPSTREAM-PRS.ymlcandidate in this PR — both need the squash sha, and the repo's convention is a separatechore(quantecon)PR afterwards (the #89 → #91 and #81 → #82 pattern). The follow-up entry would betype: bug-fix,status: pending, with notes naming #127 and the jupyter-book#1604 / jupyter-book#1681 / jupyter-book#2115 chain.🤖 Generated with Claude Code