-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatexToMathJs.js
More file actions
261 lines (226 loc) · 10.5 KB
/
LatexToMathJs.js
File metadata and controls
261 lines (226 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Convert LaTeX to math.js expression
export const latexToMathJs = (latex) => {
let expr = latex.trim();
// Handle absolute values FIRST (before other conversions)
// Match \left| ... \right| or just | ... |
expr = expr.replace(/\\left\|([^|]+)\\right\|/g, 'abs($1)');
// Handle simple | ... | patterns (non-greedy)
let absCount = 0;
expr = expr.replace(/\|/g, () => {
absCount++;
return absCount % 2 === 1 ? '(abs(' : '))';
});
// Handle fractions recursively for nested cases
let prevExpr;
do {
prevExpr = expr;
expr = expr.replace(/\\frac\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g,
'(($1)/($2))');
} while (expr !== prevExpr);
// Handle roots
expr = expr.replace(/\\sqrt\[([^\]]+)\]\{([^}]+)\}/g, '(($2)^(1/($1)))');
expr = expr.replace(/\\sqrt\{([^}]+)\}/g, '(sqrt($1))');
// **CRITICAL: Handle inverse trig with arguments BEFORE converting to short forms**
// Handle patterns like \tan^{-1}xyz or \sin^{-1}abc
expr = expr.replace(/\\tan\^\{-1\}\s*([a-zA-Z]+)/g, (match, vars) => {
return 'atan(' + vars.split('').join('*') + ')';
});
expr = expr.replace(/\\sin\^\{-1\}\s*([a-zA-Z]+)/g, (match, vars) => {
return 'asin(' + vars.split('').join('*') + ')';
});
expr = expr.replace(/\\cos\^\{-1\}\s*([a-zA-Z]+)/g, (match, vars) => {
return 'acos(' + vars.split('').join('*') + ')';
});
// Handle inverse trig with explicit arguments in braces
expr = expr.replace(/\\tan\^\{-1\}\{([^}]+)\}/g, 'atan($1)');
expr = expr.replace(/\\sin\^\{-1\}\{([^}]+)\}/g, 'asin($1)');
expr = expr.replace(/\\cos\^\{-1\}\{([^}]+)\}/g, 'acos($1)');
// Handle inverse trig BEFORE regular trig (order matters!)
expr = expr.replace(/\\arctan/g, 'atan');
expr = expr.replace(/\\arcsin/g, 'asin');
expr = expr.replace(/\\arccos/g, 'acos');
expr = expr.replace(/\\tan\^\{-1\}/g, 'atan');
expr = expr.replace(/\\sin\^\{-1\}/g, 'asin');
expr = expr.replace(/\\cos\^\{-1\}/g, 'acos');
// Handle regular trig functions (but NOT reciprocal trig yet!)
expr = expr.replace(/\\tan/g, 'tan');
expr = expr.replace(/\\sin/g, 'sin');
expr = expr.replace(/\\cos/g, 'cos');
// Replace \sec, \csc, \cot with temporary placeholders
expr = expr.replace(/\\sec/g, 'SEC');
expr = expr.replace(/\\csc/g, 'CSC');
expr = expr.replace(/\\cot/g, 'COT');
// IMPORTANT: Handle trig functions with space and variable BEFORE removing spaces
expr = expr.replace(/tan\s+([a-zA-Z])/g, 'tan($1)');
expr = expr.replace(/sin\s+([a-zA-Z])/g, 'sin($1)');
expr = expr.replace(/cos\s+([a-zA-Z])/g, 'cos($1)');
expr = expr.replace(/atan\s+([a-zA-Z])/g, 'atan($1)');
expr = expr.replace(/asin\s+([a-zA-Z])/g, 'asin($1)');
expr = expr.replace(/acos\s+([a-zA-Z])/g, 'acos($1)');
expr = expr.replace(/SEC\s+([a-zA-Z])/g, 'SEC($1)');
expr = expr.replace(/CSC\s+([a-zA-Z])/g, 'CSC($1)');
expr = expr.replace(/COT\s+([a-zA-Z])/g, 'COT($1)');
expr = expr.replace(/log\s+([a-zA-Z])/g, 'log($1)');
expr = expr.replace(/log10\s+([a-zA-Z])/g, 'log10($1)');
expr = expr.replace(/sqrt\s+([a-zA-Z])/g, 'sqrt($1)');
expr = expr.replace(/exp\s+([a-zA-Z])/g, 'exp($1)');
expr = expr.replace(/abs\s+([a-zA-Z])/g, 'abs($1)');
// **NEW: Handle trig functions followed by numbers and variables (e.g., cos2x, sin3y)**
expr = expr.replace(/(sin|cos|tan|asin|acos|atan|SEC|CSC|COT)([0-9]+)([a-zA-Z]+)/g, '$1($2*$3)');
expr = expr.replace(/(sin|cos|tan|asin|acos|atan|SEC|CSC|COT)([0-9]+)/g, '$1($2)');
// Handle logarithms
expr = expr.replace(/\\ln/g, 'log');
expr = expr.replace(/\\log/g, 'log10');
// Handle constants
expr = expr.replace(/\\pi/g, 'pi');
expr = expr.replace(/\\e(?![a-zA-Z])/g, 'e');
expr = expr.replace(/\\infty/g, 'Infinity');
// **CRITICAL: Handle function^power variable patterns BEFORE general power handling**
// This catches patterns like sin^2 x, cos^2 y, SEC^2 z, etc.
expr = expr.replace(/(sin|cos|tan|asin|acos|atan|SEC|CSC|COT)\^([0-9]+)\s*([a-zA-Z])/g, '($1($3)^$2)');
expr = expr.replace(/(sin|cos|tan|asin|acos|atan|SEC|CSC|COT)\^\{([^}]+)\}\s*([a-zA-Z])/g, '($1($3)^($2))');
// NOW convert SEC, CSC, COT to their reciprocal forms
expr = expr.replace(/SEC\(([^)]+)\)/g, '(1/cos($1))');
expr = expr.replace(/CSC\(([^)]+)\)/g, '(1/sin($1))');
expr = expr.replace(/COT\(([^)]+)\)/g, '(1/tan($1))');
// Handle brackets and braces
expr = expr.replace(/\\left\(/g, '(');
expr = expr.replace(/\\right\)/g, ')');
expr = expr.replace(/\\left\[/g, '(');
expr = expr.replace(/\\right\]/g, ')');
expr = expr.replace(/\\left\\{/g, '(');
expr = expr.replace(/\\right\\}/g, ')');
// Handle operators
expr = expr.replace(/\\cdot/g, '*');
expr = expr.replace(/\\times/g, '*');
expr = expr.replace(/\\div/g, '/');
expr = expr.replace(/\\pm/g, '+');
// Handle exponentials and powers
expr = expr.replace(/\\exp\{([^}]+)\}/g, 'exp($1)');
// Handle e^{...} before general powers
expr = expr.replace(/e\^\{([^}]+)\}/g, 'exp($1)');
// Handle general powers with braces
expr = expr.replace(/([a-zA-Z0-9]+)\^\{([^}]+)\}/g, '($1^($2))');
// Handle simple powers without braces (e.g., x^2)
expr = expr.replace(/([a-zA-Z0-9]+)\^([a-zA-Z0-9])/g, '($1^$2)');
// IMPORTANT: Handle implicit multiplication BEFORE converting braces to parens
// This preserves the structure and prevents )( patterns
// Handle {expr1}{expr2} -> convert to (expr1)*(expr2) before general brace conversion
expr = expr.replace(/\}\s*\{/g, ')*(');
// Clean up remaining braces (convert to parentheses)
expr = expr.replace(/\{/g, '(');
expr = expr.replace(/\}/g, ')');
// Remove extra spaces BEFORE processing implicit multiplication
expr = expr.replace(/\s+/g, '');
// SMARTER APPROACH: Add multiplication between adjacent variables, but NOT within function names
const knownFuncs = ['sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'log', 'log10', 'sqrt', 'exp', 'abs', 'pi'];
// Step 1: Add implicit multiplication between consecutive single-letter variables
let newExpr = '';
let i = 0;
while (i < expr.length) {
// Check if current position starts a known function
let matchedFunc = null;
for (let func of knownFuncs) {
if (expr.substr(i, func.length) === func) {
// Verify it's actually the function (check what comes after)
const nextIdx = i + func.length;
const nextChar = expr[nextIdx];
// Function if followed by '(' or operator or end
if (!nextChar || nextChar === '(' || '+-*/^),'.includes(nextChar)) {
matchedFunc = func;
break;
}
}
}
if (matchedFunc) {
// Copy the entire function name
newExpr += matchedFunc;
i += matchedFunc.length;
} else if (/[a-z]/i.test(expr[i])) {
// Single letter variable
newExpr += expr[i];
// Check if next char is also a single letter (not start of function)
if (i + 1 < expr.length && /[a-z]/i.test(expr[i + 1])) {
// Check if next position starts a function
let nextIsFunc = false;
for (let func of knownFuncs) {
if (expr.substr(i + 1, func.length) === func) {
const afterIdx = i + 1 + func.length;
const afterChar = expr[afterIdx];
if (!afterChar || afterChar === '(' || '+-*/^),'.includes(afterChar)) {
nextIsFunc = true;
break;
}
}
}
// If next is NOT a function, add multiplication
if (!nextIsFunc) {
newExpr += '*';
}
}
i++;
} else {
// Not a letter, just copy
newExpr += expr[i];
i++;
}
}
expr = newExpr;
// Now handle other implicit multiplication cases
// 1. Number followed by letter
expr = expr.replace(/(\d+\.?\d*)([a-zA-Z])/g, '$1*$2');
// 2. Closing paren followed by number
expr = expr.replace(/\)(\d)/g, ')*$1');
// 3. Number followed by opening paren
expr = expr.replace(/(\d)\(/g, '$1*(');
// 4. Closing paren followed by opening paren (but not for functions)
expr = expr.replace(/\)\s*\(/g, (match, offset) => {
const before = expr.substring(Math.max(0, offset - 15), offset);
for (let func of knownFuncs) {
if (before.endsWith(func)) {
return match;
}
}
return ')*(';
});
// 5. Closing paren followed by letter (variable, not function)
expr = expr.replace(/\)([a-zA-Z])/g, (match, letter, offset) => {
const afterMatch = expr.substring(offset + match.length);
if (afterMatch.startsWith('(')) {
const potentialFunc = expr.substring(offset + 1).match(/^[a-zA-Z]+/);
if (potentialFunc && knownFuncs.includes(potentialFunc[0])) {
return match;
}
}
return ')*' + letter;
});
// 6. Letter followed by opening paren (only for variables, not functions)
expr = expr.replace(/([a-zA-Z])(\()/g, (match, letter, paren, offset) => {
const before = expr.substring(Math.max(0, offset - 15), offset + 1);
for (let func of knownFuncs) {
if (before.endsWith(func)) {
return match;
}
}
return letter + '*' + paren;
});
// Clean up any remaining LaTeX backslashes
expr = expr.replace(/\\/g, '');
// Fix sqrt
expr = expr.replace(/sqrt\*/g, "sqrt");
// Fix exp patterns - handle cases where exp got concatenated
// zexp -> z*exp, 2exp -> 2*exp, etc.
expr = expr.replace(/([a-z0-9])exp/gi, '$1*exp');
// Convert exp to e^
expr = expr.replace(/exp\*/g, "e^");
expr = expr.replace(/exp\(/g, "e^(");
// Add explicit multiplication between variable/number and e
expr = expr.replace(/([a-z0-9])e\^/gi, '$1*e^');
// Fix cos*, sin*, tan*, etc (remove the *)
expr = expr.replace(/(cos|sin|tan|log|ln)\*/g, '$1');
// Add multiplication between number and letter/function
expr = expr.replace(/([0-9])([a-z])/gi, '$1*$2');
// Add multiplication between closing paren and letter/number
expr = expr.replace(/\)([a-z0-9(])/gi, ')*$1');
return expr;
};