A powerful (La)TeX mathematical expression parser. Unlike simple string-based parsers, it generates a full Abstract Syntax Tree (AST) while respecting mathematical operator precedence and associativity.
npm i @scicave/math-latex-parserconst { parse } = require('@scicave/math-latex-parser');
// Basic arithmetic
const ast = parse('1 + 2 * 3^2');
// Complex LaTeX
const tex = String.raw`\int_{0}^{\pi} \sin(x) dx + \frac{1}{2}`;
const ast2 = parse(tex);For an expression like 12+3^6x \frac 1 {5+3}, the parser generates a structured tree:
<script src="https://cdn.jsdelivr.net/npm/@scicave/math-latex-parser/lib/bundle.min.js"></script>
<script>
const ast = mathLatexParser.parse("x^2 + y^2 = r^2");
</script>- Arithmetic Operations: Full support for addition, subtraction, multiplication, division, powers, and factorials.
- Unary Prefix Operators: Correct handling of
+,-,\pm,\mp,\neg, and\lnot. - LaTeX Commands: Supports
\frac,\sqrt,\sum,\prod,\int,\operatorname, and more. - Automatic Multiplication: Intelligently handles implicit multiplication like
2xor(a+b)(c+d). - Extended Math Structures: Support for:
- Matrices:
\begin{matrix} 1 & 2 \\ 3 & 4 \end{matrix} - Sets:
\{ 1, 2, 3 \} - Tuples:
(1, 2, 3) - Intervals:
[a, b),(0, \infty) - Absolute Value:
|x|or\left| x \right|
- Matrices:
- Ellipsis: Support for
...,\dots,\cdots, etc. inside sets, matrices, and series.
| Operator | Type | Precedence | Associativity |
|---|---|---|---|
^ |
Infix | 7 | Left-to-Right |
! |
Postfix | 6 | N/A |
| Implicit Mult | Automult | 5 | Left-to-Right |
+, -, \pm, \mp, \neg, \lnot |
Prefix | 4 | N/A |
*, /, \cdot |
Infix | 3 | Left-to-Right |
+, - |
Infix | 2 | Left-to-Right |
=, \neq, \approx, \le, \in, etc. |
Infix | 1 | Left-to-Right |
The parse(tex, options) function accepts an optional configuration object.
parse("...", {
autoMult: true, // Allow implicit multiplication like 2x
keepParentheses: false, // If true, wraps parenthesized expressions in a "parentheses" node
functions: ["f", "g"], // Custom function names to prioritize
builtinFunctions: ["..."], // Override or extend (using "...") default TeX functions
builtinLetters: ["..."], // Override or extend (using "...") default TeX Greek letters
extra: {
memberExpressions: true, // Allow e.g., p.x
sets: true,
matrices: true,
tuples: true,
intervals: true,
ellipsis: true // Can be object for granular control (sets, matrices, etc.)
}
});Every parsed node is an instance of the Node class.
node.type: The type of the node (e.g.,"number","id","operator","function").node.args: An array of child nodes (if any).node.name: For identifiers, functions, and operators.node.value: For numbers.
Checks if the node matches the provided properties.
node.check({ type: "operator", name: "+" });Recursively checks if the node or any of its descendants match the properties. This is the recommended way to search the tree.
// Check if an expression contains any variable named "x"
ast.contains({ type: "id", name: "x" });
// Check if there is any addition anywhere in the tree
ast.contains({ type: "operator", name: "+" });Syntactic sugar for node.type === type, but with safety. It will throw an error if the type string provided is not one of the valid Node types.
node.checkType("operator"); // true or false
node.checkType("invalid_type"); // Throws ErrorThe available types are accessible via Node.types:
Node.types.NUMBER("number")Node.types.ID("id")Node.types.OPERATOR("operator")Node.types.FUNCTION("function")Node.types.FRAC("frac")- ... and more.
We love open-source! Feel free to suggest AST improvements or performance enhancements.
npm install
npm run build:watch # Watch and build
npm run test:watch # Run tests on changes- Support for parsing comments (e.g.,
%). - IDs decoration (e.g.,
\vec{F},\dot{a}). - Improved ellipsis handling variants:
1 + ... + 4,1 + \cdot\cdot\cdot + 4,1 + \cdots + 4.
MIT
