LaTeX math renderer API
Render LaTeX formulas (fractions, roots, sums, integrals, matrices, \ce chemistry) to SVG, PNG or MathML for web pages, e-mails, PDFs and quizzes.
Send a LaTeX formula in tex and choose the output: svg (default, self-contained: glyphs are vector paths, no font needed), png (transparent or background, scale 2 by default for sharp text), mathml (native MathML that current browsers display) or json (MathML, an HTML snippet, the SVG, a PNG as base64, the size and the vertical_align offset to line the image up with surrounding text). display=false gives inline style (smaller fractions, limits beside ∑).
The LaTeX is parsed by temml (MIT), which covers the usual math of KaTeX: fractions, binomials, roots, scripts, sums and integrals with limits, \left…\right delimiters, matrices, cases, aligned, accents, \mathbb / \mathcal / \mathbf, \text{} (Vietnamese works), colours and mhchem \ce{} for chemistry. The MathML is then laid out by our own TeX-like box engine and drawn with the STIX Two Math font (SIL OFL); no MathJax or browser runs on the server. Set size (font size in px, default 24), color and padding.
The SVG / PNG layout is an approximation of TeX: spacing and stretchy delimiters (which are scaled, not assembled from pieces) can differ slightly from KaTeX or MathJax, and line breaks (\\ outside environments) are ignored. Errors in the LaTeX return 400 with the parser message.
Parameters
| Name | In | Type | Required | Default | |
|---|---|---|---|---|---|
tex | query|body | string | yes | LaTeX formula, without $ delimiters (max 5000 characters). | |
format | query|body | string | no | svg | svg, png, mathml or json. |
display | query|body | boolean | no | true | false for inline style. |
size | query|body | number | no | 24 | Font size in px (8–200). |
color | query|body | string | no | #000000 | Formula colour. |
background | query|body | string | no | Background colour (transparent by default). | |
padding | query|body | number | no | Margin in px (default: a quarter of size). | |
scale | query|body | number | no | 2 | PNG pixel ratio (1–8). |
Example
curl "https://apibox.io.vn/v1/math-render?tex=E%20%3D%20mc%5E2&format=json" \
-H "Authorization: Bearer YOUR_API_KEY"const res = await fetch('https://apibox.io.vn/v1/math-render?tex=E%20%3D%20mc%5E2&format=json', {
method: 'GET',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
},
});
const data = await res.json();
console.log(data);import requests
res = requests.get(
"https://apibox.io.vn/v1/math-render?tex=E%20%3D%20mc%5E2&format=json",
headers={"Authorization": "Bearer YOUR_API_KEY"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/math-render?tex=E%20%3D%20mc%5E2&format=json');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY'],
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Response
{
"tex": "E = mc^2",
"display": true,
"mathml": "<math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\" class=\"tml-display\" style=\"display:block math;\"><mrow><mi>E</mi><mo>=</mo><mi>m</mi><msup><mi>c</mi><mn class=\"tml-sml-pad\">2</mn></msup></mrow></math>",
"html": "<div style=\"text-align:center\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\" …</math></div>",
"svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"99\" height=\"34\" viewBox=\"0 0 99 34\" role=\"img\">…</svg>",
"width": 99,
"height": 34,
"vertical_align": "-6.29px",
"png_base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAABECAYAAADa40qO…",
"png_scale": 2,
"engine": "temml (LaTeX to MathML) + own box layout with STIX Two Math outlines"
}
Use cases
- Formulas in exam papers, quizzes and e-learning content
- Math in e-mails and chat messages as PNG
- Formulas inside generated PDFs and slide decks
- Previewing LaTeX typed by students or teachers
FAQ
Which LaTeX commands are supported?
Those of temml, similar to KaTeX: standard math, environments (matrix, pmatrix, cases, aligned, array), \text, \operatorname, \color, \boxed, \cancel and \ce for chemistry. Unknown commands return a 400 error.
How do I align the image with text?
Use format=json: vertical_align (for example -6.29px) is the distance from the baseline to the bottom of the image; set it as CSS vertical-align on the <img>.
Is the SVG accessible?
The SVG is a drawing. For screen readers, use the MathML output (format=mathml or the html field of json), which browsers read as math.
Related APIs
QR Code Generator
Generate QR codes as PNG or SVG with one GET request: URLs, text, WiFi logins, vCards, e-mail, SMS, phone and geo links. Custom colours, size and ECC.
|||Barcode Generator
Create Code 128 barcodes for labels, SKUs and shipping with a single request. SVG with human-readable text or PNG, custom height, width and colours.
▭Placeholder Image
Get placeholder images of any size for mockups and tests: /v1/placeholder/600x400.png. SVG with custom text and colours, or a solid-colour PNG.