Skip to content

Commit b376402

Browse files
committed
Only replace mhchem arrows when mhchem versions are available. (mathjax/MathJax#3452)
1 parent 27c47f3 commit b376402

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

ts/input/tex/mhchem/MhchemConfiguration.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ 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<string | ((match: string, arrow: string) => string), RegExp>([
5761
[
5862
'\\mhchemx$3[$1]{$2}',
5963
/\\underset{\\lower2mu{(.*?)}}{\\overset{(.*?)}{\\long(.*?)}}/g,
@@ -72,7 +76,10 @@ export const MhchemReplacements = new Map<string, RegExp>([
7276
/\\rlap\{\\lower\.2em\{-\}\}\\rlap\{\\raise.2em\{-\}\}\\tripledash/g,
7377
],
7478
[
75-
'\\mhchem$1',
79+
(match: string, arrow: string) => {
80+
const mharrow = `mhchem${arrow}`;
81+
return mhchemChars.lookup(mharrow) || mhchemMacros.lookup(mharrow) ? `\\${mharrow}` : match;
82+
},
7683
/\\(x?(?:long)?(?:left|right|[Ll]eftright|[Rr]ightleft)(?:arrow|harpoons))/g,
7784
],
7885
]);
@@ -90,7 +97,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
9097
try {
9198
tex = mhchemParser.toTex(arg, machine);
9299
for (const [name, pattern] of MhchemReplacements.entries()) {
93-
tex = tex.replace(pattern, name);
100+
tex = tex.replace(pattern, name as string);
94101
}
95102
} catch (err) {
96103
throw new TexError(err[0], err[1]);
@@ -106,7 +113,7 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
106113
/**
107114
* The command macros
108115
*/
109-
new CommandMap('mhchem', {
116+
const mhchemMacros = new CommandMap('mhchem', {
110117
ce: [MhchemMethods.Machine, 'ce'],
111118
pu: [MhchemMethods.Machine, 'pu'],
112119
mhchemxrightarrow: [MhchemMethods.xArrow, 0xe429, 5, 9],
@@ -121,7 +128,7 @@ new CommandMap('mhchem', {
121128
/**
122129
* The character macros
123130
*/
124-
new CharacterMap('mhchem-chars', MhchemUtils.relmo, {
131+
const mhchemChars = new CharacterMap('mhchem-chars', MhchemUtils.relmo, {
125132
tripledash: ['\uE410', { stretchy: false }],
126133
mhchemBondTD: ['\uE411', { stretchy: false }],
127134
mhchemBondTDD: ['\uE412', { stretchy: false }],

0 commit comments

Comments
 (0)