Skip to content

Commit

Permalink
Merge pull request #1146 from mathjax/issue3300
Browse files Browse the repository at this point in the history
Make over/under braces and matrices be full size, as in actual TeX (mathjax/MathJax#3300)
  • Loading branch information
dpvc authored Nov 12, 2024
2 parents 5625751 + be6f39e commit 7f08013
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 23 deletions.
10 changes: 2 additions & 8 deletions ts/core/MmlTree/MmlNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,8 @@ export abstract class AbstractMmlNode
delete attributes[key];
}
}
const displaystyle = this.attributes.getExplicit('displaystyle');
if (displaystyle === undefined) {
this.attributes.setInherited('displaystyle', display);
}
const scriptlevel = this.attributes.getExplicit('scriptlevel');
if (scriptlevel === undefined) {
this.attributes.setInherited('scriptlevel', level);
}
this.attributes.setInherited('displaystyle', display);
this.attributes.setInherited('scriptlevel', level);
if (prime) {
this.setProperty('texprimestyle', prime);
}
Expand Down
1 change: 0 additions & 1 deletion ts/core/MmlTree/MmlNodes/mtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export class MmlMtable extends AbstractMmlNode {
this.replaceChild(this.factory.create('mtr'), child).appendChild(child);
}
}
level = (this.getProperty('scriptlevel') as number) || level;
display = !!(
this.attributes.getExplicit('displaystyle') ||
this.attributes.getDefault('displaystyle')
Expand Down
5 changes: 1 addition & 4 deletions ts/core/MmlTree/MmlVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ export class MmlVisitor extends AbstractVisitor<MmlNode> {
);
}
}
if (
node.getProperty('scriptlevel') &&
node.getProperty('useHeight') === false
) {
if (node.getProperty('smallmatrix')) {
this.setDataAttribute(data, 'smallmatrix', 'true');
}
return data;
Expand Down
2 changes: 1 addition & 1 deletion ts/input/mathml/MathMLCompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class MathMLCompile<N, T, D> {
ignoreVariant = true;
break;
case 'smallmatrix':
mml.setProperty('scriptlevel', 1);
mml.setProperty('smallmatrix', true);
mml.setProperty('useHeight', false);
break;
case 'mathaccent':
Expand Down
5 changes: 3 additions & 2 deletions ts/input/tex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ export class TeX<N, T, D> extends AbstractInputJax<N, T, D> {
userOptions(parseOptions.options, rest);
configuration.config(this);
TeX.tags(parseOptions, configuration);
this.postFilters.add(FilterUtil.cleanSubSup, -6);
this.postFilters.add(FilterUtil.setInherited, -5);
this.postFilters.add(FilterUtil.cleanSubSup, -7);
this.postFilters.add(FilterUtil.setInherited, -6);
this.postFilters.add(FilterUtil.checkScriptlevel, -5);
this.postFilters.add(FilterUtil.moveLimits, -4);
this.postFilters.add(FilterUtil.cleanStretchy, -3);
this.postFilters.add(FilterUtil.cleanAttributes, -2);
Expand Down
30 changes: 30 additions & 0 deletions ts/input/tex/FilterUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ namespace FilterUtil {
}) {
arg.data.root.setInheritedAttributes({}, arg.math['display'], 0, false);
};

/**
* Removes unneeded mstyle elements that just set the scriptlevel
*/
export const checkScriptlevel = function (arg: { data: ParseOptions }) {
const options = arg.data;
const remove: MmlNode[] = [];
for (const mml of options.getList('mstyle')) {
if (mml.childNodes?.[0]?.childNodes?.length !== 1) {
continue;
}
const attributes = mml.attributes;
for (const key of ['displaystyle', 'scriptlevel']) {
if (attributes.getExplicit(key) === attributes.getInherited(key)) {
attributes.unset(key);
}
}
const names = attributes.getExplicitNames();
if (
names.filter((key) => key.substring(0, 10) !== 'data-latex').length ===
0
) {
const child = mml.childNodes[0].childNodes[0];
names.forEach((key) => child.attributes.set(key, attributes.get(key)));
mml.parent.replaceChild(child, mml);
remove.push(mml);
}
}
options.removeFromList('mstyle', remove);
};
}

export default FilterUtil;
18 changes: 14 additions & 4 deletions ts/input/tex/ParseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,20 @@ export const ParseUtil = {
let node: MmlNode = mml;
if (stack) {
// @test Overbrace 1 2 3, Underbrace, Overbrace Op 1 2
node = parser.create('node', 'TeXAtom', [mml], {
texClass: TEXCLASS.OP,
movesupsub: true,
});
node = parser.create(
'node',
'TeXAtom',
[
parser.create('node', 'mstyle', [mml], {
displaystyle: true,
scriptlevel: 0,
}),
],
{
texClass: TEXCLASS.OP,
movesupsub: true,
}
);
}
NodeUtil.setProperty(node, 'subsupOK', true);
return node;
Expand Down
4 changes: 2 additions & 2 deletions ts/input/tex/ams/AmsMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ new sm.EnvironmentMap('AMSmath-environment', ParseMethods.environment, {
ParseUtil.cols(0),
'0.1em',
'S',
1,
true,
],
smallmatrix: [
AmsMethods.Array,
Expand All @@ -216,7 +216,7 @@ new sm.EnvironmentMap('AMSmath-environment', ParseMethods.environment, {
ParseUtil.cols(1 / 3),
'.2em',
'S',
1,
true,
],
matrix: [AmsMethods.Array, null, null, null, 'c'],
pmatrix: [AmsMethods.Array, null, '(', ')', 'c'],
Expand Down
5 changes: 4 additions & 1 deletion ts/input/tex/base/BaseItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ export class ArrayItem extends BaseItem {
delete this.arraydef['scriptlevel'];
let mml = this.create('node', 'mtable', this.table, this.arraydef);
if (scriptlevel) {
mml.setProperty('scriptlevel', scriptlevel);
mml.setProperty('smallmatrix', true);
}
if (this.breakAlign.table) {
NodeUtil.setAttribute(mml, 'data-break-align', this.breakAlign.table);
Expand All @@ -1115,6 +1115,9 @@ export class ArrayItem extends BaseItem {
);
}
mml = this.handleFrame(mml);
if (scriptlevel !== undefined) {
mml = this.create('node', 'mstyle', [mml], { scriptlevel });
}
if (this.getProperty('open') || this.getProperty('close')) {
// @test Cross Product Formula
mml = ParseUtil.fenced(
Expand Down
2 changes: 2 additions & 0 deletions ts/input/tex/base/BaseMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,8 @@ const BaseMethods: { [key: string]: ParseMethod } = {
if (style === 'S') {
// @test Subarray, Small Matrix
array.arraydef['scriptlevel'] = 1;
} else {
array.arraydef['scriptlevel'] = 0;
}
if (raggedHeight) {
// @test Subarray, Small Matrix
Expand Down

0 comments on commit 7f08013

Please sign in to comment.