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

@latex-math/core

v0.1.3

Published

A robust, lightweight TypeScript library that parses LaTeX mathematical expressions into a strictly typed Abstract Syntax Tree (AST), evaluates mathematical values, simplifies expressions, and supports symbolic calculus.

Readme

@latex-math/core

A robust, lightweight TypeScript library that parses LaTeX mathematical expressions into a strictly typed Abstract Syntax Tree (AST), evaluates mathematical values, simplifies expressions, and supports symbolic calculus.

Zero dependencies for the core runtime. Built without unsafe dynamic code execution (eval or Function()).

npm version License: MIT


Features

  • 📐 Typed Mathematical AST: Parses mathematical LaTeX into a well-defined Expression AST union (fractions, roots, powers, matrices, sums, integrals, and more).
  • ⚡ Safe & Deterministic: Hand-rolled recursive descent parser and tokenizer. Never relies on eval() or new Function().
  • 🧠 Evaluation Engine: Evaluates arithmetic, trigonometric functions, logarithms, binomial coefficients, summations, matrices, and complex numbers with variable scoping.
  • 🔄 TeX Macro Normalization: Automatically resolves and normalizes unbraced TeX macros (such as \sqrt 1+2 $\to$ \sqrt{1} + 2, \frac12 $\to$ \frac{1}{2}).
  • 📈 Calculus Support: Symbolic differentiation (\frac{d}{dx}) and numerical integration with Simpson's rule.
  • 🖨️ Printer & Simplifier: Print AST back into clean LaTeX string representation, and perform algebraic simplifications.

Installation

npm install @latex-math/core
yarn add @latex-math/core
# or
pnpm add @latex-math/core

Quick Start

1. Parsing to AST

import { parseLatex } from '@latex-math/core';

// Parse a LaTeX expression into a typed AST
const ast = parseLatex('\\frac{2x + 4}{2}');
console.log(ast);

2. Evaluating Expressions

import { parseLatex, evaluateLatex } from '@latex-math/core';

// Evaluate numeric expressions
const result = evaluateLatex('\\sqrt{16} + 2^3');
console.log(result); // 12

// Evaluate with variables
const ast = parseLatex('3x^2 - 4x + 1');
const evaluated = evaluateLatex(ast, {
  variables: { x: 5 }
});
console.log(evaluated); // 56

3. Symbolic Differentiation & Calculus

import { parseLatex, differentiate, printLatex } from '@latex-math/core';

// Differentiate an expression symbolically with respect to 'x'
const ast = parseLatex('x^3 + 4x^2 - 7x + 2');
const derivativeAst = differentiate(ast, 'x');

// Print back to LaTeX
console.log(printLatex(derivativeAst)); // 3x^2 + 8x - 7

4. Expression Simplification

import { parseLatex, simplifyExpression, printLatex } from '@latex-math/core';

const expr = parseLatex('0 + 1 \\cdot x + 4 - 2');
const simplified = simplifyExpression(expr);

console.log(printLatex(simplified)); // x + 2

5. LaTeX Argument Normalization

Converts ambiguous or unbraced LaTeX syntax into explicit, braced TeX:

import { normalizeLatex } from '@latex-math/core';

normalizeLatex('\\sqrt 1+2'); // "\\sqrt{1} + 2"
normalizeLatex('\\frac12');   // "\\frac{1}{2}"
normalizeLatex('\\binom42');  // "\\binom{4}{2}"

Supported LaTeX Syntax

| Category | Examples | |---|---| | Arithmetic | +, -, \cdot, \times, /, \div, implicit multiplication (2x, 3\pi) | | Fractions | \frac{a}{b}, \frac12 | | Powers & Roots | x^2, x^{2+y}, \sqrt{x}, \sqrt[3]{8} | | Trigonometry | \sin, \cos, \tan, \arcsin, \arccos, \arctan, \csc, \sec, \cot | | Logarithms | \ln(x), \log(x), \log_{10}(x) | | Calculus | \frac{d}{dx} f(x), \int_{0}^{1} x^2 \, dx | | Summations | \sum_{n=1}^{10} n^2 | | Matrices | \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \begin{bmatrix} ... \end{bmatrix} | | Combinatorics | \binom{n}{k}, n! | | Special Constants | \pi, e, i (imaginary unit) |


API Reference

parseLatex(input: string): Expression

Parses a LaTeX math string into a typed AST. Automatically runs macro normalization.

evaluateLatex(expr: Expression | string, scope?: EvaluationScope): number | Matrix | Complex | boolean

Evaluates an AST or LaTeX string. Accepts optional variable scopes:

interface EvaluationScope {
  variables?: Record<string, number | Complex | Matrix>;
  functions?: Record<string, (...args: number[]) => number>;
}

differentiate(expr: Expression, variable?: string): Expression

Calculates the symbolic derivative of an expression with respect to the specified variable (defaults to 'x').

simplifyExpression(expr: Expression): Expression

Simplifies an expression using algebraic identity rules (constant folding, zero multiplication, identity element removal).

printLatex(expr: Expression): string

Serializes an Expression AST back into a standard LaTeX string.

normalizeLatex(input: string): string

Normalizes unbraced TeX macros (\sqrt, \frac, \binom) by wrapping implicit single tokens in braces.


License

MIT © Sora1123