Skip to content

Commit

Permalink
Better checking of improperly nested AMS environments. (mathjax/MathJ…
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvc committed Feb 25, 2023
1 parent 4464efc commit 7bb5fbd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
14 changes: 10 additions & 4 deletions ts/input/tex/ParseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,18 @@ namespace ParseUtil {
/**
* Check for bad nesting of equation environments
*/
export function checkEqnEnv(parser: TexParser) {
if (parser.stack.global.eqnenv) {
// @test ErroneousNestingEq
export function checkEqnEnv(parser: TexParser, nestable: boolean = true) {
const top = parser.stack.Top();
const first = top.First;
//
// The gather environment can include align and others, but only one level deep.
//
if (top.getProperty('nestable') && nestable && !first) {
return;
}
if (!top.isKind('start') || first) {
throw new TexError('ErroneousNestingEq', 'Erroneous nesting of equation structures');
}
parser.stack.global.eqnenv = true;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ts/input/tex/ams/AmsMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ AmsMethods.AlignAt = function(parser: TexParser, begin: StackItem,
*/
AmsMethods.Multline = function (parser: TexParser, begin: StackItem, numbered: boolean) {
// @test Shove*, Multline
parser.Push(begin);
ParseUtil.checkEqnEnv(parser);
parser.Push(begin);
const item = parser.itemFactory.create('multline', numbered, parser.stack) as ArrayItem;
item.arraydef = {
displaystyle: true,
Expand Down Expand Up @@ -170,8 +170,8 @@ AmsMethods.XalignAt = function(parser: TexParser, begin: StackItem,
AmsMethods.FlalignArray = function(parser: TexParser, begin: StackItem, numbered: boolean,
padded: boolean, center: boolean, align: string,
width: string, zeroWidthLabel: boolean = false) {
parser.Push(begin);
ParseUtil.checkEqnEnv(parser);
parser.Push(begin);
align = align
.split('')
.join(' ')
Expand Down
13 changes: 9 additions & 4 deletions ts/input/tex/base/BaseMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,8 @@ BaseMethods.AlignedArray = function(parser: TexParser, begin: StackItem) {
* @param {boolean} numbered True if environment is numbered.
*/
BaseMethods.Equation = function (parser: TexParser, begin: StackItem, numbered: boolean) {
parser.Push(begin);
ParseUtil.checkEqnEnv(parser);
parser.Push(begin);
return parser.itemFactory.create('equation', numbered).
setProperty('name', begin.getName());
};
Expand All @@ -1488,13 +1488,15 @@ BaseMethods.EqnArray = function(parser: TexParser, begin: StackItem,
numbered: boolean, taggable: boolean,
align: string, spacing: string) {
// @test The Lorenz Equations, Maxwell's Equations, Cubic Binomial
parser.Push(begin);
let name = begin.getName();
let isGather = (name === 'gather' || name === 'gather*');
if (taggable) {
ParseUtil.checkEqnEnv(parser);
ParseUtil.checkEqnEnv(parser, !isGather);
}
parser.Push(begin);
align = align.replace(/[^clr]/g, '').split('').join(' ');
align = align.replace(/l/g, 'left').replace(/r/g, 'right').replace(/c/g, 'center');
let newItem = parser.itemFactory.create('eqnarray', begin.getName(),
let newItem = parser.itemFactory.create('eqnarray', name,
numbered, taggable, parser.stack.global) as sitem.ArrayItem;
newItem.arraydef = {
displaystyle: true,
Expand All @@ -1504,6 +1506,9 @@ BaseMethods.EqnArray = function(parser: TexParser, begin: StackItem,
side: parser.options['tagSide'],
minlabelspacing: parser.options['tagIndent']
};
if (isGather) {
newItem.setProperty('nestable', true);
}
return newItem;
};

Expand Down
2 changes: 1 addition & 1 deletion ts/input/tex/empheq/EmpheqConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export const EmpheqMethods = {
parser.Push(parser.itemFactory.create('end').setProperty('name', 'empheq'));
} else {
ParseUtil.checkEqnEnv(parser);
delete parser.stack.global.eqnenv;
const opts = parser.GetBrackets('\\begin{' + begin.getName() + '}') || '';
const [env, n] = (parser.GetArgument('\\begin{' + begin.getName() + '}') || '').split(/=/);
if (!EmpheqUtil.checkEnv(env)) {
throw new TexError('UnknownEnv', 'Unknown environment "%1"', env);
}
begin.setProperty('nestable', true);
if (opts) {
begin.setProperties(EmpheqUtil.splitOptions(opts, {left: 1, right: 1}));
}
Expand Down

0 comments on commit 7bb5fbd

Please sign in to comment.