From 7aeb2bad84cdd890783dff72a0906d4b165f2bef Mon Sep 17 00:00:00 2001 From: Ashp116 <42877289+Ashp116@users.noreply.github.com> Date: Wed, 17 Jul 2024 09:57:57 -0400 Subject: [PATCH] LatexToAsciiMath incorrect interval notation (#2455) * Fixed: #2454 * remove .js * Revert "Fixed: #2454" This reverts commit ab5355dbb94f80a5b3491e0c2ba2bdf5b01c4c61. * Fix * Added some js files back * Update vue-mathlive.js --------- Co-authored-by: root --- src/formats/atom-to-ascii-math.ts | 78 ++++++++++++++++--------------- src/formats/parse-math-string.ts | 18 ++++--- test/__mocks__/styleMock.js | 2 +- test/math-ascii.test.ts | 4 ++ 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/src/formats/atom-to-ascii-math.ts b/src/formats/atom-to-ascii-math.ts index 684e41f37..4aacff93c 100644 --- a/src/formats/atom-to-ascii-math.ts +++ b/src/formats/atom-to-ascii-math.ts @@ -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; @@ -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; diff --git a/src/formats/parse-math-string.ts b/src/formats/parse-math-string.ts index 1483752a3..66f1596a1 100644 --- a/src/formats/parse-math-string.ts +++ b/src/formats/parse-math-string.ts @@ -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); } @@ -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 [] @@ -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)); diff --git a/test/__mocks__/styleMock.js b/test/__mocks__/styleMock.js index 2fdaa42f5..c44663aec 100644 --- a/test/__mocks__/styleMock.js +++ b/test/__mocks__/styleMock.js @@ -1 +1 @@ -module.exports = 'MOCK_STYLE'; \ No newline at end of file +module.exports = 'MOCK_STYLE'; diff --git a/test/math-ascii.test.ts b/test/math-ascii.test.ts index a15f51880..323168d7e 100644 --- a/test/math-ascii.test.ts +++ b/test/math-ascii.test.ts @@ -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)") });