Maths written any way, into anything.

Type maths the way you'd write it — 3x^2 + sin x, \frac{d}{dx} e^{2x}, Math.pow(x, 2), x² ≤ √y. MathZetta reads it into one standard postfix form, and from there gives you LaTeX, MathML, the expression read aloud, JavaScript, Python, SQL, a shader, the exact answer, or a formula that runs over a table. No dependencies, a few kilobytes per output, works in the browser and in Node.

Ways to use it

A script tag

One file from a CDN, no build. Everything is 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>

Modules: only what you use

The core is the parser; each output is its own entry point, so a page that only needs LaTeX only loads LaTeX.

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

Entries: /latex /mathml /text /speech /javascript /python /sql /excel /glsl /evaluate /calculate /formulas /all /plain — all of them, live. From a CDN without npm: import https://cdn.jsdelivr.net/npm/mathzetta@1/build/mathzetta.esm.min.js, or pick exactly the outputs you need on the CDN page (one small file per output, the core shared).

Node

npm install mathzetta
node -e "import('mathzetta/sql').then(m =>
  console.log(m.toSQL('price * qty > 10')))"
// or require('mathzetta') for the CommonJS build

Answers inside text: {{…}}

Anything in double braces is calculated — exactly where it can be — before the rest is parsed. Load mathzetta/calculate (or the script tag) and it's on.

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'

Show it: Inline Math

A sibling library draws the same trees as HTML and CSS — fractions, integrals, piecewise — with every part addressable by the same node ids the spoken form uses, so a narration can light up what it's describing.

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

Open the Inline Math demo

Formulas over tables

mathzetta/formulas turns H / AB > 0.3 into a function of a row, or a SQL expression. tabview is a whole table page built on it — rows in the browser, in a tabc server, or in a sqldn (SQLite) server.

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;

Open the tabview demo

Image filters

mathzetta/glsl writes shaders; qblur turns a formula like if gray > 0.5 then (1, 0, 0) or gaussian(3) into a shareable filter with parameters, masks and chains, and runs it in WebGL.

Open the filter editor

Algebra: MathZetta CAS

Exact simplify, factor, solve, differentiate and integrate, with steps you can read. What it knows is rules over the same postfix, which you can read and change. 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

Try it on Inline Math's CAS page · the rules behind it

Rules as formulas

Two games use MathZetta so every rule — swing chance, fatigue, ticket demand — is a formula the player can replace and see rendered. If your app has knobs, they can be formulas.

evaluate('max(0, 1 - fatigue/100) * skill',
         { fatigue: 50, skill: 0.8 });   // 0.4

Built on it

Every one of these reads its maths with MathZetta; each card says how. They are the family of sites in the sidebar.

The format

Standard postfix: operands and operators in reverse Polish order; fixed-arity operators are bare (x sin, a b ^), variadic ones follow a count (a b c 3 max), unary minus is neg, d/dx is der, eu is e u ^. Every token is listed in docs/postfix.md; the pipeline (clean → tokenize → parse → tree → render) in docs/how-it-works.md. Node ids are postfix indexes, which is what lets a spoken phrase, a MathML element and a drawn part all mean the same thing. Learn it by doing: the postfix quiz turns an expression into postfix one move at a time, with an operator stack and hints.

What it understands

Every operation, with live examples in every format: the operations reference. The playground shows every stage of the pipeline for one input; the test runner is the ~6,000 checks the library is held to, editable in place.