Skip to content

Commit e4a8e28

Browse files
committed
Add support for delimiters that are specific to a mathvariant and use it for mhchem arrows. (mathjax/MathJax#3486)
1 parent 95faa0c commit e4a8e28

39 files changed

+306
-211
lines changed

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

Lines changed: 30 additions & 30 deletions
Large diffs are not rendered by default.

ts/input/tex/ams/AmsMethods.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
AbstractMmlTokenNode,
4242
} from '../../../core/MmlTree/MmlNode.js';
4343
import { NewcommandUtil } from '../newcommand/NewcommandUtil.js';
44+
import { OptionList } from '../../../util/Options.js';
4445

4546
/**
4647
* Utility for breaking the \sideset scripts from any other material.
@@ -541,14 +542,16 @@ export const AmsMethods: { [key: string]: ParseMethod } = {
541542
* @param {number} l Left width.
542543
* @param {number} r Right width.
543544
* @param {number} m Min width
545+
* @param {string} variant The variant for the arrow
544546
*/
545547
xArrow(
546548
parser: TexParser,
547549
name: string,
548550
chr: number,
549551
l: number,
550552
r: number,
551-
m: number = 0
553+
m: number = 0,
554+
variant: string = ''
552555
) {
553556
const def = {
554557
width: '+' + UnitUtil.em((l + r) / 18),
@@ -557,12 +560,14 @@ export const AmsMethods: { [key: string]: ParseMethod } = {
557560
const bot = parser.GetBrackets(name);
558561
const first = parser.ParseArg(name);
559562
const dstrut = parser.create('node', 'mspace', [], { depth: '.2em' });
560-
let arrow = parser.create(
561-
'token',
562-
'mo',
563-
{ stretchy: true, texClass: TEXCLASS.ORD }, // REL is applied in a TeXAtom below
564-
String.fromCodePoint(chr)
565-
);
563+
const attrs: OptionList = {
564+
stretchy: true,
565+
texClass: TEXCLASS.ORD, // REL is applied in a TeXAtom below
566+
};
567+
if (variant) {
568+
attrs.mathvariant = variant;
569+
}
570+
let arrow = parser.create('token', 'mo', attrs, String.fromCodePoint(chr));
566571
if (m) {
567572
arrow.attributes.set('minsize', UnitUtil.em(m));
568573
}

ts/input/tex/mhchem/MhchemConfiguration.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import { AmsMethods } from '../ams/AmsMethods.js';
3333
import { mhchemParser } from '#mhchem/mhchemParser.js';
3434
import { TEXCLASS } from '../../../core/MmlTree/MmlNode.js';
3535

36+
/**
37+
* The variant to use for mhchem arrows and bonds
38+
*/
39+
export const MHCHEM = '-mhchem';
40+
3641
/**
3742
* Creates mo token elements with the proper attributes
3843
*/
@@ -41,7 +46,7 @@ export const MhchemUtils = {
4146
const def = {
4247
stretchy: true,
4348
texClass: TEXCLASS.REL,
44-
mathvariant: '-mhchem',
49+
mathvariant: MHCHEM,
4550
...(mchar.attributes || {}),
4651
};
4752
const node = parser.create('token', 'mo', def, mchar.char);
@@ -121,13 +126,13 @@ export const MhchemMethods: { [key: string]: ParseMethod } = {
121126
const mhchemMacros = new CommandMap('mhchem', {
122127
ce: [MhchemMethods.Machine, 'ce'],
123128
pu: [MhchemMethods.Machine, 'pu'],
124-
mhchemxrightarrow: [MhchemMethods.xArrow, 0xe429, 5, 9],
125-
mhchemxleftarrow: [MhchemMethods.xArrow, 0xe428, 9, 5],
126-
mhchemxleftrightarrow: [MhchemMethods.xArrow, 0xe42a, 9, 9],
127-
mhchemxleftrightarrows: [MhchemMethods.xArrow, 0xe42b, 9, 9],
128-
mhchemxrightleftharpoons: [MhchemMethods.xArrow, 0xe408, 5, 9],
129-
mhchemxRightleftharpoons: [MhchemMethods.xArrow, 0xe409, 5, 9],
130-
mhchemxLeftrightharpoons: [MhchemMethods.xArrow, 0xe40a, 9, 11],
129+
mhchemxleftarrow: [MhchemMethods.xArrow, 0x27f5, 9, 5, 0, MHCHEM],
130+
mhchemxrightarrow: [MhchemMethods.xArrow, 0x27f6, 5, 9, 0, MHCHEM],
131+
mhchemxleftrightarrow: [MhchemMethods.xArrow, 0x27f7, 9, 9, 0, MHCHEM],
132+
mhchemxleftrightarrows: [MhchemMethods.xArrow, 0x21c4, 9, 9, 0, MHCHEM],
133+
mhchemxrightleftharpoons: [MhchemMethods.xArrow, 0x21cc, 5, 9, 0, MHCHEM],
134+
mhchemxRightleftharpoons: [MhchemMethods.xArrow, 0x1f8d2, 5, 9, 0, MHCHEM],
135+
mhchemxLeftrightharpoons: [MhchemMethods.xArrow, 0x1f8d3, 9, 11, 0, MHCHEM],
131136
});
132137

133138
/**
@@ -138,16 +143,16 @@ const mhchemChars = new CharacterMap('mhchem-chars', MhchemUtils.relmo, {
138143
mhchemBondTD: ['\uE411', { stretchy: false }],
139144
mhchemBondTDD: ['\uE412', { stretchy: false }],
140145
mhchemBondDTD: ['\uE413', { stretchy: false }],
141-
mhchemlongleftarrow: '\uE428',
142-
mhchemlongrightarrow: '\uE429',
143-
mhchemlongleftrightarrow: '\uE42A',
144-
mhchemlongrightleftharpoons: '\uE408',
145-
mhchemlongRightleftharpoons: '\uE409',
146-
mhchemlongLeftrightharpoons: '\uE40A',
147-
mhchemlongleftrightarrows: '\uE42B',
148-
mhchemrightarrow: '\uE42D',
149-
mhchemleftarrow: '\uE42C',
150-
mhchemleftrightarrow: '\uE42E',
146+
mhchemlongleftarrow: '\u27F5',
147+
mhchemlongrightarrow: '\u27F6',
148+
mhchemlongleftrightarrow: '\u27F7',
149+
mhchemlongrightleftharpoons: '\u21CC',
150+
mhchemlongRightleftharpoons: '\u{1F8D2}',
151+
mhchemlongLeftrightharpoons: '\u{1F8D3}',
152+
mhchemlongleftrightarrows: '\u21C4',
153+
mhchemrightarrow: '\u2192',
154+
mhchemleftarrow: '\u2190',
155+
mhchemleftrightarrow: '\u2194',
151156
});
152157

153158
/**

ts/output/chtml/FontData.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export type ChtmlCharData = CharDataArray<ChtmlCharOptions>;
6464
/**
6565
* The extra data needed for a Variant in CHTML output
6666
*/
67-
export interface ChtmlVariantData extends VariantData<ChtmlCharOptions> {
67+
export interface ChtmlVariantData extends VariantData<
68+
ChtmlCharOptions,
69+
ChtmlDelimiterData
70+
> {
6871
letter: string; // the font letter(s) for the default font for this variant
6972
}
7073

@@ -141,7 +144,7 @@ export class ChtmlFontData extends FontData<
141144
/**
142145
* Data about the delimiters used (for adpative CSS)
143146
*/
144-
public delimUsage: Usage<number> = new Usage<number>();
147+
public delimUsage: Usage<[number, string]> = new Usage<[number, string]>();
145148

146149
/**
147150
* New styles since last update
@@ -354,8 +357,8 @@ export class ChtmlFontData extends FontData<
354357
* @returns {StyleJson} The modified style list.
355358
*/
356359
public updateStyles(styles: StyleJson): StyleJson {
357-
for (const N of this.delimUsage.update()) {
358-
this.addDelimiterStyles(styles, N, this.getDelimiter(N));
360+
for (const [N, v] of this.delimUsage.update()) {
361+
this.addDelimiterStyles(styles, N, this.getDelimiter(N, v));
359362
}
360363
for (const [name, N] of this.charUsage.update()) {
361364
const variant = this.variant[name];
@@ -440,7 +443,7 @@ export class ChtmlFontData extends FontData<
440443
) {
441444
const HDW = data.HDW as ChtmlCharData;
442445
const [beg, ext, end, mid] = data.stretch;
443-
const [begV, extV, endV, midV] = this.getStretchVariants(n);
446+
const [begV, extV, endV, midV] = this.getStretchVariants(n, data.v);
444447
const Hb = this.addDelimiterVPart(styles, c, 'beg', beg, begV, HDW);
445448
this.addDelimiterVPart(styles, c, 'ext', ext, extV, HDW);
446449
const He = this.addDelimiterVPart(styles, c, 'end', end, endV, HDW);
@@ -524,7 +527,7 @@ export class ChtmlFontData extends FontData<
524527
) {
525528
const HDW = [...data.HDW] as ChtmlCharData;
526529
const [beg, ext, end, mid] = data.stretch;
527-
const [begV, extV, endV, midV] = this.getStretchVariants(n);
530+
const [begV, extV, endV, midV] = this.getStretchVariants(n, data.v);
528531
if (data.hd && !this.options.mathmlSpacing) {
529532
//
530533
// Interpolate between full character height/depth and that of the extender,

ts/output/chtml/Wrappers/mo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ export const ChtmlMo = (function <N, T, D>(): ChtmlMoClass<N, T, D> {
247247
*/
248248
protected stretchHTML(chtml: N[]) {
249249
const c = this.getText().codePointAt(0);
250-
this.font.delimUsage.add(c);
250+
this.font.delimUsage.add([c, this.variant]);
251251
this.childNodes[0].markUsed();
252252
const delim = this.stretch;
253253
const stretch = delim.stretch;
254-
const stretchv = this.font.getStretchVariants(c);
254+
const stretchv = this.font.getStretchVariants(c, this.variant);
255255
const dom: N[] = [];
256256

257257
//

ts/output/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export abstract class CommonOutputJax<
109109
WW, WF, WC, CC, VV, DD, FD, FC
110110
>,
111111
CC extends CharOptions,
112-
VV extends VariantData<CC>,
112+
VV extends VariantData<CC, DD>,
113113
DD extends DelimiterData,
114114
FD extends FontData<CC, VV, DD>,
115115
FC extends FontDataClass<CC, VV, DD>,
@@ -189,7 +189,7 @@ export abstract class CommonOutputJax<
189189
*/
190190
public static genericFont: FontDataClass<
191191
CharOptions,
192-
VariantData<CharOptions>,
192+
VariantData<CharOptions, DelimiterData>,
193193
DelimiterData
194194
>;
195195

0 commit comments

Comments
 (0)