Skip to content

Commit 22effae

Browse files
authored
Merge pull request #1372 from mathjax/issue3452
Only replace mhchem arrows when mhchem versions are available. (mathjax/MathJax#3452)
2 parents 8c8c5eb + 90b1d89 commit 22effae

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

testsuite/tests/input/tex/Mhchem.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5588,7 +5588,7 @@ describe('Mhchem-Ams', () => {
55885588

55895589
/********************************************************************************/
55905590

5591-
it('Mhchem leftrightarrrow', () => {
5591+
it('Mhchem leftrightarrow', () => {
55925592
toXmlMatch(
55935593
tex2mml('\\ce{A\\leftrightarrow B}'),
55945594
`<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\ce{A\\leftrightarrow B}" display="block">
@@ -5607,6 +5607,25 @@ describe('Mhchem-Ams', () => {
56075607

56085608
/********************************************************************************/
56095609

5610+
it('Mhchem rightleftharpoons', () => {
5611+
toXmlMatch(
5612+
tex2mml('\\ce{A\\rightleftharpoons B}'),
5613+
`<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\ce{A\\rightleftharpoons B}" display="block">
5614+
<mrow data-mjx-texclass="ORD" data-latex="{\\mathrm{A}\\rightleftharpoons \\mathrm{B}}">
5615+
<mrow data-mjx-texclass="ORD" data-latex="\\mathrm{A}">
5616+
<mi mathvariant="normal" data-latex="A">A</mi>
5617+
</mrow>
5618+
<mo data-mjx-alternate="1" stretchy="false" data-latex="\\rightleftharpoons">&#x21CC;</mo>
5619+
<mrow data-mjx-texclass="ORD" data-latex="\\mathrm{B}">
5620+
<mi mathvariant="normal" data-latex="B">B</mi>
5621+
</mrow>
5622+
</mrow>
5623+
</math>`
5624+
);
5625+
});
5626+
5627+
/********************************************************************************/
5628+
56105629
});
56115630

56125631
/**********************************************************************************/

ts/input/tex/mhchem/MhchemConfiguration.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ export const MhchemUtils = {
5252
/**
5353
* Replace these constructs in mhchem output now that we have stretchy versions
5454
* of the needed arrows
55+
*
56+
* @param {string} match The matching macro name
57+
* @param {string} arrow The arrow name (without the backslash).
58+
* @returns {string} The mhchem arrow name, if there is one.
5559
*/
56-
export const MhchemReplacements = new Map<string, RegExp>([
60+
export const MhchemReplacements = new Map<
61+
string | ((match: string, arrow: string) => string),
62+
RegExp
63+
>([
5764
[
5865
'\\mhchemx$3[$1]{$2}',
5966
/\\underset{\\lower2mu{(.*?)}}{\\overset{(.*?)}{\\long(.*?)}}/g,
@@ -72,7 +79,12 @@ export const MhchemReplacements = new Map<string, RegExp>([
7279
/\\rlap\{\\lower\.2em\{-\}\}\\rlap\{\\raise.2em\{-\}\}\\tripledash/g,
7380
],
7481
[
75-
'\\mhchem$1',
82+
(match: string, arrow: string) => {
83+
const mharrow = `mhchem${arrow}`;
84+
return mhchemChars.lookup(mharrow) || mhchemMacros.lookup(mharrow)
85+
? `\\${mharrow}`
86+
: match;
87+
},
7688
/\\(x?(?:long)?(?:left|right|[Ll]eftright|[Rr]ightleft)(?:arrow|harpoons))/g,
7789
],
7890
]);
@@ -90,7 +102,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
90102
try {
91103
tex = mhchemParser.toTex(arg, machine);
92104
for (const [name, pattern] of MhchemReplacements.entries()) {
93-
tex = tex.replace(pattern, name);
105+
tex = tex.replace(pattern, name as string);
94106
}
95107
} catch (err) {
96108
throw new TexError(err[0], err[1]);
@@ -106,7 +118,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
106118
/**
107119
* The command macros
108120
*/
109-
new CommandMap('mhchem', {
121+
const mhchemMacros = new CommandMap('mhchem', {
110122
ce: [MhchemMethods.Machine, 'ce'],
111123
pu: [MhchemMethods.Machine, 'pu'],
112124
mhchemxrightarrow: [MhchemMethods.xArrow, 0xe429, 5, 9],
@@ -121,7 +133,7 @@ new CommandMap('mhchem', {
121133
/**
122134
* The character macros
123135
*/
124-
new CharacterMap('mhchem-chars', MhchemUtils.relmo, {
136+
const mhchemChars = new CharacterMap('mhchem-chars', MhchemUtils.relmo, {
125137
tripledash: ['\uE410', { stretchy: false }],
126138
mhchemBondTD: ['\uE411', { stretchy: false }],
127139
mhchemBondTDD: ['\uE412', { stretchy: false }],

0 commit comments

Comments
 (0)