Skip to content

Commit

Permalink
Merge pull request #773 from mathjax/issue2760
Browse files Browse the repository at this point in the history
Have physics package match nested parentheses, fix spacing issues (mathjax/MathJax#2760, mathjax/MathJax#2831)
  • Loading branch information
dpvc committed Mar 10, 2022
2 parents 4ced76d + 7ff21fe commit db04605
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
3 changes: 2 additions & 1 deletion ts/input/tex/physics/PhysicsConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export const PhysicsConfiguration = Configuration.create(
macro: [
'Physics-automatic-bracing-macros',
'Physics-vector-macros',
'Physics-vector-chars',
'Physics-vector-mo',
'Physics-vector-mi',
'Physics-derivative-macros',
'Physics-expressions-macros',
'Physics-quick-quad-macros',
Expand Down
46 changes: 38 additions & 8 deletions ts/input/tex/physics/PhysicsItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,25 @@

import {CheckType, BaseItem, StackItem} from '../StackItem.js';
import ParseUtil from '../ParseUtil.js';
import NodeUtil from '../NodeUtil.js';
import TexParser from '../TexParser.js';

import {AbstractMmlTokenNode} from '../../../core/MmlTree/MmlNode.js';

export class AutoOpen extends BaseItem {

/**
* @override
*/
protected static errors = Object.assign(Object.create(BaseItem.errors), {
'stop': ['ExtraOrMissingDelims', 'Extra open or missing close delimiter']
});

/**
* The number of unpaired open delimiters that need to be matched before
* a close delimiter will close this item. (#2831)
*/
protected openCount: number = 0;

/**
* @override
*/
Expand Down Expand Up @@ -64,20 +78,36 @@ export class AutoOpen extends BaseItem {
this.Push(new TexParser(right, parser.stack.env,
parser.configuration).mml());
}
let mml = super.toMml();
return ParseUtil.fenced(this.factory.configuration,
this.getProperty('open') as string,
mml,
this.getProperty('close') as string,
this.getProperty('big') as string);
let mml = ParseUtil.fenced(
this.factory.configuration,
this.getProperty('open') as string,
super.toMml(),
this.getProperty('close') as string,
this.getProperty('big') as string
);
//
// Remove fence markers that would cause it to be TeX class INNER,
// so it is treated as a regular mrow when setting the tex class (#2760)
//
NodeUtil.removeProperties(mml, 'open', 'close', 'texClass');
return mml;
}

/**
* @override
*/
public checkItem(item: StackItem): CheckType {
//
// Check for nested open delimiters (#2831)
//
if (item.isKind('mml') && item.Size() === 1) {
const mml = item.toMml();
if (mml.isKind('mo') && (mml as AbstractMmlTokenNode).getText() === this.getProperty('open')) {
this.openCount++;
}
}
let close = item.getProperty('autoclose');
if (close && close === this.getProperty('close')) {
if (close && close === this.getProperty('close') && !this.openCount--) {
if (this.getProperty('ignore')) {
this.Clear();
return [[], true];
Expand Down
7 changes: 5 additions & 2 deletions ts/input/tex/physics/PhysicsMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ new CommandMap('Physics-automatic-bracing-macros', {
/**
* Macros for physics package (section 2.2).
*/
new CharacterMap('Physics-vector-chars', ParseMethods.mathchar0mi, {
new CharacterMap('Physics-vector-mo', ParseMethods.mathchar0mo, {
dotproduct: ['\u22C5', {mathvariant: TexConstant.Variant.BOLD}],
vdot: ['\u22C5', {mathvariant: TexConstant.Variant.BOLD}],
crossproduct: '\u00D7',
cross: '\u00D7',
cp: '\u00D7',
// This is auxiliary!
gradientnabla: ['\u2207', {mathvariant: TexConstant.Variant.BOLD}],
gradientnabla: ['\u2207', {mathvariant: TexConstant.Variant.BOLD}]
});

new CharacterMap('Physics-vector-mi', ParseMethods.mathchar0mi, {
real: ['\u211C', {mathvariant: TexConstant.Variant.NORMAL}],
imaginary: ['\u2111', {mathvariant: TexConstant.Variant.NORMAL}]
});
Expand Down

0 comments on commit db04605

Please sign in to comment.