Zero-dependency MathJax v4 SVG renderer for Python, powered by QuickJS
Project description
QuickJax
Zero-dependency MathJax v4 SVG renderer for Python, powered by QuickJS.
QuickJax lets you convert LaTeX math expressions to self-contained SVG strings entirely within your Python process — no Node.js, no subprocess calls, no network requests.
Features
- Pure in-process rendering — MathJax v4 runs inside an embedded QuickJS engine via the
quickjsPython library. - Self-contained SVG output — Every rendered SVG carries its own font glyphs (
fontCache: "local"); no external CSS or fonts required. - 23 TeX extensions included out of the box: AMS, physics, mhchem, mathtools, braket, cancel, color, and more.
- Display & inline modes supported.
- Python 3.8+ compatible.
Installation
pip install quickjax
The only runtime dependency is the
quickjspackage (a C extension embedding the QuickJS engine, about 2 MB in size). No Node.js installation is needed.
Quick Start
from quickjax import render
# Display mode (default)
svg = render(r"E = mc^2")
# Inline mode
svg = render(r"\alpha + \beta", display=False)
# Write to file
with open("equation.svg", "w") as f:
f.write(svg)
Using the Renderer Class
For batch rendering, instantiate MathJaxRenderer once to amortize the JS context creation (~0.3 s):
from quickjax import MathJaxRenderer
renderer = MathJaxRenderer()
expressions = [
r"\int_0^\infty e^{-x}\,dx = 1",
r"\nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}",
r"\mathbb{R}^n",
]
for i, expr in enumerate(expressions):
svg = renderer.render(expr)
with open(f"eq_{i}.svg", "w") as f:
f.write(svg)
API Reference
render(latex, *, display=True) -> str
Module-level convenience function. Creates a shared MathJaxRenderer lazily on first call.
| Parameter | Type | Default | Description |
|---|---|---|---|
latex |
str |
— | LaTeX expression (without $ delimiters) |
display |
bool |
True |
True for display mode, False for inline |
Returns: A self-contained <svg>…</svg> string.
Raises: MathJaxRenderError if the expression cannot be rendered.
class MathJaxRenderer
| Method | Description |
|---|---|
__init__() |
Load the MathJax JS bundle into a QuickJS context. |
render(latex, *, display=True) -> str |
Render LaTeX to SVG. Same parameters as the module-level function. |
Supports use as a context manager (with MathJaxRenderer() as r: …).
class MathJaxRenderError
Subclass of Exception. Raised when MathJax cannot parse or render the given LaTeX input.
Supported TeX Extensions
ams · newcommand · boldsymbol · braket · cancel · color · enclose · extpfeil · html · mhchem · noerrors · noundefined · physics · mathtools · amscd · action · bbox · unicode · verb · textmacros · textcomp · cases
Development
Prerequisites
- Python 3.8+
- Node.js (build-time only, for bundling MathJax)
esbuild(installed automatically via npm)
Building the JS Bundle
bash build_bundle.sh
This runs npm install in renderer_src/ and produces quickjax/js/mathjax_bundle.js (~4.1 MB minified).
Running Tests
pip install pytest
pytest tests/ -v
How It Works
- Build time —
esbuildbundles MathJax v4 (mathjax-full@4.0.0-beta.7) plus all 26 dynamic font files frommathjax-modern-fontinto a single IIFE JavaScript file. - Runtime —
MathJaxRenderer.__init__()creates a QuickJS context (4 MB stack / 128 MB heap) and evaluates the bundle once. - Render — Each
render()call invokesglobalThis.render(latex)inside that context. MathJax'sliteAdaptorprovides a virtual DOM; the SVG output is extracted and returned as a pure<svg>string.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quickjax-0.1.0.tar.gz.
File metadata
- Download URL: quickjax-0.1.0.tar.gz
- Upload date:
- Size: 1.3 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffc2065ab357f34091c743c659501184c0bce17d4827b26e3b193fe3ceb8be76
|
|
| MD5 |
64a491a09ed479f2d05c5a716b56835e
|
|
| BLAKE2b-256 |
d4c8845d366f401d50ac57d378de9892e033efa1a396a9d5db04268ac3d69085
|
File details
Details for the file quickjax-0.1.0-py3-none-any.whl.
File metadata
- Download URL: quickjax-0.1.0-py3-none-any.whl
- Upload date:
- Size: 1.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdcde6770598e8dda805cffd033328ec0610c250fc9515b1451a53a9f641cca1
|
|
| MD5 |
63e3e9fa7fac2fe6ebd8339cf9b6a46a
|
|
| BLAKE2b-256 |
c2bb37927ee314fd7f6d2cc05d0f029fb28e411b6b9ee569474ed9a15f5175d7
|