Repository navigation
mhchem: \ce and \pu in the LaTeX reader - #19
Merged
Merged
Conversation
\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>
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.
Summary
\ce{...}and\pu{...}(mhchem) are now read:Internal/LaTeX/Mhchem.csrewrites them as plain LaTeX beforeLaTeXReaderruns, 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.\puhandles a number, itseexponent as a power of ten, and upright units with powers (mol-1).\cefollowmhchem.goof Wyatt915/treeblood (MIT, attributed inNOTICE), rewritten to write LaTeX instead of MathML. Where treeblood loses things, the port does better: inKCr(SO4)2.12H2Othe12was dropped, a charge after a bracket went to an empty prescript, andH{}^3HOput the isotope on nothing.UnicodeMathPreprocess.cs, theprecase): 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.chemistrycases of treeblood'stest/TESTCASES.yaml(tests/data/latex/treeblood-mhchem.json, provenance intests/data/README.md).LaTeX/MhchemTestschecks that each one parses withStrict = truewithout placeholders, checks the MathML of 18 formulas by hand, and checks that malformed input is rejected.VersionPrefixstays 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,RoundTripPropertyTestsincluded🤖 Generated with Claude Code