Skip to content

mhchem: \ce and \pu in the LaTeX reader - #19

Merged
simonebortolin merged 1 commit into
claude/content-mathmlfrom
claude/mhchem
Nov 9, 2026
Merged

simonebortolin merged 1 commit into
claude/content-mathmlfrom
claude/mhchem

Conversation

@simonebortolin

Copy link
Copy Markdown
Owner

Summary

  • \ce{...} and \pu{...} (mhchem) are now read: Internal/LaTeX/Mhchem.cs rewrites them as plain LaTeX before LaTeXReader runs, so they go through the existing UnicodeMath pipeline. Elements come out upright, counts and charges as scripts, mass and atomic numbers as prescripts (mmultiscripts). It also handles arrows with labels (->[a][b] → \xrightarrow/\overset/\underset, plus <=>, <-->, <=>> ⥂, <<=> ⥄), bonds (- = #, \bond{...}), states (aq)/(s), hydrate dots, v/^ precipitates and gases, $math$ and nested \ce. \pu handles a number, its e exponent as a power of ten, and upright units with powers (mol-1).
  • The rules of \ce follow mhchem.go of Wyatt915/treeblood (MIT, attributed in NOTICE), rewritten to write LaTeX instead of MathML. Where treeblood loses things, the port does better: in KCr(SO4)2.12H2O the 12 was dropped, a charge after a bracket went to an empty prescript, and H{}^3HO put the isotope on nothing.
  • Translator fix (UnicodeMathPreprocess.cs, the pre case): when a prescripted base had scripts of its own (^0_(-1) n^-), those scripts were preprocessed twice, so an operator in them came out as <mo>[object Object]</mo>. This was a bug before this PR too, for plain LaTeX/UnicodeMath.
  • Test data: the 76 chemistry cases of treeblood's test/TESTCASES.yaml (tests/data/latex/treeblood-mhchem.json, provenance in tests/data/README.md). LaTeX/MhchemTests checks that each one parses with Strict = true without placeholders, checks the MathML of 18 formulas by hand, and checks that malformed input is rejected.
  • No public API change. No new dependency. Text is handled as code points. VersionPrefix stays at 1.0.5, the bump the open PRs share.

Stacked on #13 (claude/content-mathml).

Test plan

  • dotnet test --solution SharpEquations.slnx: 6038/6038 pass on net10.0 and net481, RoundTripPropertyTests included

🤖 Generated with Claude Code

\ce{...} and \pu{...} are rewritten as plain LaTeX before the LaTeX is read
(Internal/LaTeX/Mhchem.cs): upright elements with counts and charges as
scripts, mass and atomic numbers as prescripts, reaction arrows with their
labels, bonds (\bond, - = #), states, hydrate dots, precipitates and gases.
The rules of \ce follow mhchem.go of treeblood (MIT, Copyright (c) 2024
Wyatt Sheffield), rewritten to write LaTeX instead of MathML.

Fixes the UnicodeMath preprocessing of a prescripted base with scripts of its
own (^0_(-1) n^-): its scripts were preprocessed twice, and an operator in
them came out as "[object Object]".

Test data: the 76 chemistry cases of treeblood's test/TESTCASES.yaml
(tests/data/latex/treeblood-mhchem.json), all read with Strict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonebortolin
simonebortolin merged commit 25ad9c4 into claude/content-mathml Nov 9, 2026
12 checks passed
@simonebortolin
simonebortolin deleted the claude/mhchem branch November 9, 2026 19:24
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