Skip to content

Commit

Permalink
LatexToAsciiMath incorrect interval notation (#2455)
Browse files Browse the repository at this point in the history
* Fixed: #2454

* remove .js

* Revert "Fixed: #2454"

This reverts commit ab5355d.

* Fix

* Added some js files back

* Update vue-mathlive.js

---------

Co-authored-by: root <[email protected]>
  • Loading branch information
Ashp116 and root authored Jul 17, 2024
1 parent ce51699 commit 7aeb2ba
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
78 changes: 41 additions & 37 deletions src/formats/atom-to-ascii-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,44 +238,44 @@ export function atomToAsciiMath(
break;

case 'genfrac':
{
const genfracAtom = atom as GenfracAtom;
if (genfracAtom.leftDelim || genfracAtom.rightDelim) {
result =
genfracAtom.leftDelim === '.' || !genfracAtom.leftDelim
? '{:'
: genfracAtom.leftDelim;
}
{
const genfracAtom = atom as GenfracAtom;
if (genfracAtom.leftDelim || genfracAtom.rightDelim) {
result =
genfracAtom.leftDelim === '.' || !genfracAtom.leftDelim
? '{:'
: genfracAtom.leftDelim;
}

if (genfracAtom.hasBarLine) {
result += '(';
result += atomToAsciiMath(genfracAtom.above, options);
result += ')/(';
result += atomToAsciiMath(genfracAtom.below, options);
result += ')';
} else {
// No bar line, i.e. \choose, etc...
result += '(' + atomToAsciiMath(genfracAtom.above, options) + '),';
result += '(' + atomToAsciiMath(genfracAtom.below, options) + ')';
}
if (genfracAtom.hasBarLine) {
result += '(';
result += atomToAsciiMath(genfracAtom.above, options);
result += ')/(';
result += atomToAsciiMath(genfracAtom.below, options);
result += ')';
} else {
// No bar line, i.e. \choose, etc...
result += '(' + atomToAsciiMath(genfracAtom.above, options) + '),';
result += '(' + atomToAsciiMath(genfracAtom.below, options) + ')';
}

if (genfracAtom.leftDelim || genfracAtom.rightDelim) {
result +=
genfracAtom.rightDelim === '.' || !genfracAtom.rightDelim
? '{:'
: genfracAtom.rightDelim;
}
if (genfracAtom.leftDelim || genfracAtom.rightDelim) {
result +=
genfracAtom.rightDelim === '.' || !genfracAtom.rightDelim
? '{:'
: genfracAtom.rightDelim;
}
}

break;

case 'surd':
result += !atom.hasEmptyBranch('above')
? 'root(' +
atomToAsciiMath(atom.above, options) +
')(' +
atomToAsciiMath(atom.body, options) +
')'
atomToAsciiMath(atom.above, options) +
')(' +
atomToAsciiMath(atom.body, options) +
')'
: 'sqrt(' + atomToAsciiMath(atom.body, options) + ')';
break;

Expand All @@ -284,14 +284,18 @@ export function atomToAsciiMath(
break;

case 'leftright':
{
const leftrightAtom = atom as LeftRightAtom;
const lDelim = leftrightAtom.leftDelim;
result += lDelim === '.' || !lDelim ? '{:' : lDelim;
result += atomToAsciiMath(leftrightAtom.body, options);
const rDelim = leftrightAtom.matchingRightDelim();
result += rDelim === '.' || !rDelim ? ':}' : rDelim;
}
{
const leftrightAtom = atom as LeftRightAtom;
let lDelim = leftrightAtom.leftDelim;
if (lDelim && SPECIAL_OPERATORS[lDelim])
lDelim = SPECIAL_OPERATORS[lDelim];
result += lDelim === '.' || !lDelim ? '{:' : lDelim;
result += atomToAsciiMath(leftrightAtom.body, options);
let rDelim = leftrightAtom.matchingRightDelim();
if (rDelim && SPECIAL_OPERATORS[rDelim])
rDelim = SPECIAL_OPERATORS[rDelim];
result += rDelim === '.' || !rDelim ? ':}' : rDelim;
}

break;

Expand Down
18 changes: 12 additions & 6 deletions src/formats/parse-math-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function parseMathExpression(
let result = '';
if (s[1] === '(') result = `${s[0]}\\left(${match}\\right)`;
else result = s[0] + match;
console.log(result);
return result + parseMathExpression(rest, options);
}

Expand Down Expand Up @@ -189,7 +190,12 @@ function parseMathExpression(

return s;
}

const SPECIAL_OPERATORS = {
'[': '\\lbrack',
']': '\\rbrack',
'\\{': '\\lbrace',
'\\}': '\\rbrace',
};
/**
* Parse a math argument, as defined by ASCIIMath and UnicodeMath:
* - Either an expression fenced in (), {} or []
Expand Down Expand Up @@ -225,20 +231,20 @@ function parseMathArgument(

if (level === 0) {
// We've found the matching closing fence
if (options.noWrap && lFence === '(')
if (options.noWrap && lFence === '(') {
match = parseMathExpression(s.substring(1, i - 1), options);
else {
console.log(match);
} else {
if (lFence === '{' && rFence === '}') {
lFence = '\\{';
rFence = '\\}';
}

match =
'\\left' +
lFence +
SPECIAL_OPERATORS[lFence] +
parseMathExpression(s.substring(1, i - 1), options) +
'\\right' +
rFence;
SPECIAL_OPERATORS[rFence];
}

rest = s.slice(Math.max(0, i));
Expand Down
2 changes: 1 addition & 1 deletion test/__mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = 'MOCK_STYLE';
module.exports = 'MOCK_STYLE';
4 changes: 4 additions & 0 deletions test/math-ascii.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ describe('ASCII MATH', function () {
// '\\begin{bmatrix}a & b & c \\\\ d & e & f\\end{bmatrix}',
// '[[a],[b],[c],[d],[e],[f]]'
// );

equalASCIIMath('\\left\\lbrack1,1\\right\\rbrack', "[1,1]")
equalASCIIMath('\\left\\lbrace1,1\\right\\rbrace', "{1,1}")
equalASCIIMath("\\left(1,1\\right)","(1,1)")
});

0 comments on commit 7aeb2ba

Please sign in to comment.