Write math any way. Convert it to anything.
Type math however you like — 3x^2 + sin x, \frac{d}{dx} e^{2x}, Math.pow(x, 2), or x² ≤ √y.
MathZetta parses it into a single standard postfix format, then converts it to LaTeX, MathML, spoken English,
JavaScript, Python, SQL, Excel, GLSL shaders, exact answers, or live table formulas. Zero dependencies, just a few kilobytes per output, and it runs in both the browser and Node.
Used for variables inside {{…}} blocks (so x^2 = {{x^2}} becomes x² = 4 when x = 2) and for the evaluate and calculate rows. Everywhere else, variables stay symbolic.
How to use it
The API
Parse once, output anywhere. parse turns text into postfix, and every to… function accepts raw text,
postfix, or a syntax tree — so you can parse once and pass the result to as many formats as you need.
const p = parse('x^2 + 1');
// p is ['x', '2', '^', '1', '+']
toLaTeX(p); // 'x^2 + 1'
toSpeech(p); // 'x squared plus 1'
toJavaScript(p); // 'Math.pow(x, 2) + 1'
evaluate(p, {x: 3}); // 10
Pass options as the last argument (for example, toSQL(p, { dialect: 'sqlite' }) or toPython(p, { numpy: true })).
Invalid input or unsupported outputs (like a derivative in SQL) throw a clear Error, and
parseSteps(text) shows every step of the parser.
Script tag
Drop in one CDN script with no build step. Everything is available on window.MathZetta.
<script src="https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.min.js"></script>
<script>
const { parse, toLaTeX, toSpeech } = MathZetta;
toSpeech('(x+1)^2'); // "the quantity x plus 1, squared"
</script>
Import only what you need
The core package is just the parser. Each output format is its own entry point, so you only bundle what you actually use.
import { parse } from 'mathzetta';
import { toLaTeX } from 'mathzetta/latex';
import { calculate } from 'mathzetta/calculate';
parse('2x sin x'); // ['2','x','*','x','sin','*']
toLaTeX('x^2/2'); // '\\frac{x^2}{2}'
calculate('sqrt(8)').text; // '2*sqrt(2)'
Entry points: /latex /mathml /text /speech /javascript /python /sql /excel /glsl /evaluate /calculate /formulas /all /plain — see them live. Using a CDN without npm? Import https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js, or customize your bundle on the CDN builder page.
Node
npm install mathzetta
node -e "import('mathzetta/sql').then(m =>
console.log(m.toSQL('price * qty > 10')))"
// or require('mathzetta') for CommonJS
Works with ES modules and CommonJS in the browser and Node 18+, with full TypeScript types and zero runtime dependencies.
Features
- Accepts almost any notation. Plain text, LaTeX, Unicode, JavaScript, Python (math, NumPy, SymPy), SQL, GLSL, and spreadsheet formulas all parse into one standard postfix format.
- Outputs to every major format. LaTeX, MathML, plain text, spoken English, JavaScript, Python, SQL (PostgreSQL and SQLite), Excel/Sheets formulas, and GLSL.
- Full code round-tripping. MathZetta can parse back every code format it generates, verified automatically by the test suite.
- Exact and numeric math.
calculatekeeps fractions and roots exact (√8 becomes 2√2), whileevaluatecomputes fast floating-point values. - Safe for untrusted input. Generated JavaScript is checked against a strict whitelist before running, so user-typed formulas can't execute arbitrary code.
- Shared node IDs. Every node's ID matches its postfix index, so spoken words, MathML elements, and rendered HTML parts all stay linked.
- Lightweight and thoroughly tested. Just a few kilobytes per output, backed by over 6,000 tests you can run right in your browser.
Inline calculations: {{…}}
Wrap an expression in double braces to calculate it automatically — keeping exact fractions and roots where possible. Just import mathzetta/calculate (or use the full script tag).
toText('3 + 4 = {{3 + 4}}') // '3 + 4 = 7'
toLaTeX('\\sqrt{8} = {{sqrt(8)}}') // '\\sqrt{8} = 2\\sqrt{2}'
toText('\\pi \\approx {{~4 pi}}') // 'π ≈ 3.1416'
Display math: Inline Math
A companion library that renders MathZetta trees as plain HTML and CSS — fractions, integrals, piecewise functions, and more. Every part shares the same node IDs as the speech output, so you can highlight, click, or animate individual pieces.
import { render } from '@mathzetta/inlinemath';
const v = render(el, String.raw`\int_0^1 x^2 dx`);
v.highlight('x^2'); v.set(String.raw`\frac{1}{3}`, { animate: true });
Table formulas
mathzetta/formulas compiles expressions like H / AB > 0.3 into fast row functions or SQL. tabview uses it to power searchable data tables in the browser, on a tabc server, or on SQLite.
const f = strToFunction(['H', 'AB'], 'H / AB');
createRowEvaluator(f.jsFunction)([30, 100]); // 0.3
strToSQL(['H', 'AB'], 'H / AB > 0.3', new Map(),
{ dialect: 'sqlite' }).sqlExpression;
Image filters
mathzetta/glsl compiles math into GPU shaders. qblur builds on it to turn formulas like if gray > 0.5 then (1, 0, 0) or gaussian(3) into shareable WebGL image filters with sliders and masks.
Computer algebra: MathZetta CAS
Simplify, factor, solve, differentiate, and integrate with readable step-by-step explanations — driven by YAML rules you can inspect and customize. npm install @mathzetta/cas
import { parse } from 'mathzetta';
import { createCAS } from '@mathzetta/cas';
const cas = createCAS({ read: (text) => parse(text) });
cas.diff('x^2 sin(x)', 'x').text // x^2·cos(x) + 2x·sin(x)
cas.solve('x^2 - 5x + 6 = 0', 'x').text // x = 2 or x = 3
cas.integrate('x e^x', 'x').text // x·e^x - e^x
Narrated math videos
MathZetta CAS generates each step, Inline Math renders it, speech segments sync every spoken word to the matching part of the equation, and TTS narrates it — turning a single expression like the derivative of x^2 sin(x) into a full video.
Open the video maker (requires local server)
Editable game & app formulas
Two games use MathZetta so every rule — swing chance, fatigue, ticket demand — is a formula players can replace and see rendered as math. If your app has knobs, they can be formulas too.
evaluate('max(0, 1 - fatigue/100) * skill',
{ fatigue: 50, skill: 0.8 }); // 0.4
Built with MathZetta
Live tools and sites powered by MathZetta. You can also jump to any of them from the sidebar.
The postfix format
MathZetta uses standard reverse Polish notation (postfix): fixed-arity operators are bare (x sin, a b ^), variadic functions include an argument count (a b c 3 max), unary minus is neg, derivatives use der, and eu is e u ^.
See the full token list in docs/postfix.md and the parsing pipeline in docs/how-it-works.md.
Because every node ID is just its postfix index, speech segments, MathML elements, and rendered HTML parts all stay automatically in sync.
Want to learn how it works hands-on? Try the interactive postfix quiz.
What it supports
The goal: read any math you'd meet through Calculus II — algebra, functions, trig, exponentials, logarithms, limits, derivatives, integrals, sequences, and series — no matter how it's written, and draw it, say it, and work it out.
Browse every operator with live examples in the operations reference, inspect every parsing stage in the playground, or run the 6,000+ checks yourself in the browser test runner.
When math can be read two ways
Some input has two reasonable readings; MathZetta always takes the same one. Click one to see it read above. All of them, with how to write the other reading, are in When math can be read two ways (each is a test).