Skip to content

Commit

Permalink
Allow \middle to work when a style or color is open. (mathjax/MathJax…
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvc committed Mar 30, 2021
1 parent 8c569e8 commit acb40f1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
3 changes: 2 additions & 1 deletion ts/input/tex/ParseUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace ParseUtil {
* @param {string=} big Bigg command.
*/
export function fenced(configuration: ParseOptions, open: string, mml: MmlNode,
close: string, big: string = '') {
close: string, big: string = '', color: string = '') {
// @test Fenced, Fenced3
let nf = configuration.nodeFactory;
let mrow = nf.create('node', 'mrow', [],
Expand All @@ -145,6 +145,7 @@ namespace ParseUtil {
{fence: true, stretchy: true, symmetric: true, texClass: TEXCLASS.CLOSE},
closeNode);
}
color && mo.attributes.set('mathcolor', color);
NodeUtil.appendChildren(mrow, [mo]);
return mrow;
}
Expand Down
3 changes: 2 additions & 1 deletion ts/input/tex/StackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ export abstract class BaseItem extends MmlStack implements StackItem {
// @test ExtraCloseMissingOpen
close: ['ExtraCloseMissingOpen', 'Extra close brace or missing open brace'],
// @test MissingLeftExtraRight
right: ['MissingLeftExtraRight', 'Missing \\left or extra \\right']
right: ['MissingLeftExtraRight', 'Missing \\left or extra \\right'],
middle: ['ExtraMiddle', 'Extra \\middle']
};


Expand Down
1 change: 1 addition & 0 deletions ts/input/tex/base/BaseConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const BaseConfiguration: Configuration = Configuration.create(
[bitem.SubsupItem.prototype.kind]: bitem.SubsupItem,
[bitem.OverItem.prototype.kind]: bitem.OverItem,
[bitem.LeftItem.prototype.kind]: bitem.LeftItem,
[bitem.Middle.prototype.kind]: bitem.Middle,
[bitem.RightItem.prototype.kind]: bitem.RightItem,
[bitem.BeginItem.prototype.kind]: bitem.BeginItem,
[bitem.EndItem.prototype.kind]: bitem.EndItem,
Expand Down
63 changes: 57 additions & 6 deletions ts/input/tex/base/BaseItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class OpenItem extends BaseItem {


/**
* Item indicating an close brace. Collapses stack until an OpenItem is found.
* Item indicating a close brace. Collapses stack until an OpenItem is found.
*/
export class CloseItem extends BaseItem {

Expand Down Expand Up @@ -359,9 +359,9 @@ export class LeftItem extends BaseItem {
/**
* @override
*/
constructor(factory: StackItemFactory) {
constructor(factory: StackItemFactory, delim: string) {
super(factory);
this.setProperty('delim', '(');
this.setProperty('delim', delim);
}

/**
Expand All @@ -386,16 +386,66 @@ export class LeftItem extends BaseItem {
public checkItem(item: StackItem): CheckType {
// @test Missing Right
if (item.isKind('right')) {
//
// Create the fenced structure as an mrow
//
return [[this.factory.create('mml', ParseUtil.fenced(
this.factory.configuration,
this.getProperty('delim') as string, this.toMml(),
item.getProperty('delim') as string))], true];
item.getProperty('delim') as string, '', item.getProperty('color') as string))], true];
}
if (item.isKind('middle')) {
//
// Add the middle delimiter, with empty open and close elements around it for spacing
//
const def = {stretchy: true} as any;
if (item.getProperty('color')) {
def.mathcolor = item.getProperty('color');
}
this.Push(
this.create('node', 'TeXAtom', [], {texClass: TEXCLASS.CLOSE}),
this.create('token', 'mo', def, item.getProperty('delim')),
this.create('node', 'TeXAtom', [], {texClass: TEXCLASS.OPEN})
);
this.env = {}; // Since \middle closes the group, clear the environment
return [[this], true]; // this will reset the environment to its initial state
}
return super.checkItem(item);
}

}

/**
* Item pushed when a \\middle delimiter has been found. Stack is
* collapsed until a corresponding LeftItem is encountered.
*/
export class Middle extends BaseItem {

/**
* @override
*/
constructor(factory: StackItemFactory, delim: string, color: string) {
super(factory);
this.setProperty('delim', delim);
color && this.setProperty('color', color);
}

/**
* @override
*/
public get kind() {
return 'middle';
}


/**
* @override
*/
get isClose() {
return true;
}

}

/**
* Item pushed when a \\right closing delimiter has been found. Stack is
Expand All @@ -406,9 +456,10 @@ export class RightItem extends BaseItem {
/**
* @override
*/
constructor(factory: StackItemFactory) {
constructor(factory: StackItemFactory, delim: string, color: string) {
super(factory);
this.setProperty('delim', ')');
this.setProperty('delim', delim);
color && this.setProperty('color', color);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ts/input/tex/base/BaseMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ new sm.CommandMap('macros', {

left: 'LeftRight',
right: 'LeftRight',
middle: 'Middle',
middle: 'LeftRight',

llap: 'Lap',
rlap: 'Lap',
Expand Down
25 changes: 1 addition & 24 deletions ts/input/tex/base/BaseMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,30 +337,7 @@ BaseMethods.Spacer = function(parser: TexParser, _name: string, space: string) {
BaseMethods.LeftRight = function(parser: TexParser, name: string) {
// @test Fenced, Fenced3
const first = name.substr(1);
parser.Push(
parser.itemFactory.create(first)
.setProperty('delim', parser.GetDelimiter(name)));
};

/**
* Parses middle fenced expressions.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Middle = function(parser: TexParser, name: string) {
// @test Middle
const delim = parser.GetDelimiter(name);
let node = parser.create('node', 'TeXAtom', [], {texClass: TEXCLASS.CLOSE});
parser.Push(node);
if (!parser.stack.Top().isKind('left')) {
// @test Orphan Middle, Middle with Right
throw new TexError('MisplacedMiddle',
'%1 must be within \\left and \\right', parser.currentCS);
}
node = parser.create('token', 'mo', {stretchy: true}, delim);
parser.Push(node);
node = parser.create('node', 'TeXAtom', [], {texClass: TEXCLASS.OPEN});
parser.Push(node);
parser.Push(parser.itemFactory.create(first, parser.GetDelimiter(name), parser.stack.env.color));
};

/**
Expand Down

0 comments on commit acb40f1

Please sign in to comment.