npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@scicave/math-latex-parser

v3.0.3

Published

A mathematical (La)TeX parser.

Readme

math-latex-parser

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.

Install

npm i @scicave/math-latex-parser

Usage

Node.js / Bundlers

const { 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:

AST

Browser

<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>

Features

  • 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 2x or (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|
  • Ellipsis: Support for ..., \dots, \cdots, etc. inside sets, matrices, and series.

Operator Precedence

| 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 |


Parser Options

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.)
  }
});

API: The Node Class

Every parsed node is an instance of the Node class.

Properties

  • 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.

Methods

node.check(props, checkArgs = false)

Checks if the node matches the provided properties.

node.check({ type: "operator", name: "+" });

node.contains(props, checkArgs = false)

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: "+" });

node.checkType(type)

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 Error

Node Types

The 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.

Contribute

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

License

MIT