LaTeX to JavaScript Math Syntax Converter
Convert LaTeX math equations to JavaScript math syntax compatible with math.js. Supports fractions, roots, trig functions, powers, and nested expressions.
About
LaTeX is the standard typesetting language for mathematical notation, but it carries zero computational meaning. Pasting a LaTeX fraction into a JavaScript runtime produces a syntax error, not a number. This converter parses raw LaTeX input through a recursive-descent algorithm and emits valid JavaScript math syntax targeting the math.js evaluation engine. It handles \frac, \sqrt, \sin, \cos, \tan, \log, \ln, nth roots via \sqrt[n]{x}, and nested exponentiation via caret notation. The parser respects operator precedence and implicit multiplication rules common in mathematical writing.
Manual transcription of formulas is error-prone. A single misplaced parenthesis in a nested fraction silently corrupts every downstream calculation. This tool eliminates that risk by enforcing balanced brace matching and producing structurally correct output. Note: the converter assumes well-formed LaTeX. Ambiguous constructs like bare superscripts without bases will produce a parse error rather than a guess. Variable names are passed through verbatim. Greek letters such as Ο are mapped to their math.js constant equivalents.
Formulas
The converter implements a three-stage pipeline common in compiler design: Lexical Analysis, Parsing, and Code Generation.
Operator precedence follows standard mathematical convention, implemented via recursive descent:
Level 2: Γ Γ· (explicit and implicit)
Level 3: unary β +
Level 4 (highest): ^ (right-associative)
Fraction conversion rule:
Nth root conversion rule:
Power conversion rule:
Implicit multiplication is inserted when a number is directly followed by a variable or opening parenthesis, or when a closing parenthesis is followed by an opening parenthesis or variable. For example, 2x becomes 2 Γ x.
Reference Data
| LaTeX Input | JavaScript Output | Description |
|---|---|---|
\frac{a}{b} | (a) / (b) | Simple fraction |
\frac{x + 1}{y - 2} | (x + 1) / (y - 2) | Fraction with expressions |
\sqrt{x} | sqrt(x) | Square root |
\sqrt[3]{x} | nthRoot(x, 3) | Cube root (nth root) |
\sqrt[n]{x + 1} | nthRoot(x + 1, n) | Nth root with variable index |
x^{2} | pow(x, 2) | Exponentiation |
x^{n+1} | pow(x, n + 1) | Exponent with expression |
\sin{x} | sin(x) | Sine function |
\cos{\theta} | cos(theta) | Cosine with Greek letter |
\tan{x} | tan(x) | Tangent function |
\log{x} | log10(x) | Base-10 logarithm |
\ln{x} | log(x) | Natural logarithm |
\pi | pi | Pi constant |
e^{x} | pow(e, x) | Euler's number exponent |
2x | 2 * x | Implicit multiplication |
\frac{\sin{x}}{\cos{x}} | (sin(x)) / (cos(x)) | Nested functions in fraction |
\sqrt{x^{2} + y^{2}} | sqrt(pow(x, 2) + pow(y, 2)) | Pythagorean expression |
-x^{2} | -pow(x, 2) | Unary minus with power |
\frac{-b + \sqrt{b^{2} - 4ac}}{2a} | (-b + sqrt(pow(b, 2) - 4 * a * c)) / (2 * a) | Quadratic formula |
(a + b)(c + d) | (a + b) * (c + d) | Implicit mult. with parens |
\sin^{2}{x} | pow(sin(x), 2) | Squared trig function |
\left(x\right) | (x) | Left/right delimiters |
3.14 | 3.14 | Decimal number passthrough |
\frac{1}{\sqrt{2}} | (1) / (sqrt(2)) | Fraction over square root |
x_{1} | x_1 | Subscripted variable |
\alpha + \beta | alpha + beta | Greek letter variables |