Skip to content

Commit 966653a

Browse files
committed
fix: enhance LaTeX parsing for limits with postfix operators
1 parent 5d19a72 commit 966653a

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#### Fixed
44

5+
- **LaTeX parsing: `\lim` with postfix operators**`\lim_{x\to 0}\left(x\right)^x`
6+
now correctly parses as `Limit(x^x)` instead of `Power(Limit(x), x)`. The
7+
`\lim` parser was using `parseArguments('implicit')` which stripped the
8+
delimiters and left the `^x` unconsumed; it now uses `parseExpression` so
9+
postfix operators are included in the limit body.
10+
511
- **LaTeX parsing: style, size, and color switch commands**`\displaystyle`,
612
`\textstyle`, `\scriptstyle`, `\scriptscriptstyle`, `\tiny`..`\Huge` (10 size
713
commands), and `\color{...}` were silently discarded during parsing. They now

src/compute-engine/latex-syntax/dictionary/definitions-arithmetic.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,11 +1319,16 @@ export const DEFINITIONS_ARITHMETIC: LatexDictionary = [
13191319
if (!parser.match('_')) return null;
13201320
const base = parser.parseGroup();
13211321
if (operator(base) !== 'To') return null;
1322-
const expr = parser.parseArguments('implicit');
1322+
// Use parseExpression instead of parseArguments('implicit') so that
1323+
// postfix operators like ^x are included in the limit body.
1324+
// e.g. \lim_{x\to 0}\left(x\right)^x → Limit(x^x) not Limit(x)^x
1325+
const expr = parser.parseExpression({
1326+
minPrec: MULTIPLICATION_PRECEDENCE,
1327+
});
13231328
if (!expr) return null;
13241329
return [
13251330
'Limit',
1326-
['Function', expr[0], operand(base, 1)],
1331+
['Function', expr, operand(base, 1)],
13271332
operand(base, 2),
13281333
] as MathJsonExpression;
13291334
},

test/playground.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {
1818
const ce = new ComputeEngine();
1919
const engine = ce;
2020

21-
console.log(parse('x + 2 = \\square').json);
22-
console.log(parse('x + 2 = \\placeholder').json);
21+
console.log(parse('\\lim_{x\\to 0}\\left(x\\right)^x').toMathJson());
2322

2423
parse('[5,7)');
2524
// -> ok, `["Interval", 5, ["Open", 7]]`

0 commit comments

Comments
 (0)