Skip to main content

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 quickjs Python 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 quickjs package (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

  1. Build timeesbuild bundles MathJax v4 (mathjax-full@4.0.0-beta.7) plus all 26 dynamic font files from mathjax-modern-font into a single IIFE JavaScript file.
  2. RuntimeMathJaxRenderer.__init__() creates a QuickJS context (4 MB stack / 128 MB heap) and evaluates the bundle once.
  3. Render — Each render() call invokes globalThis.render(latex) inside that context. MathJax's liteAdaptor provides a virtual DOM; the SVG output is extracted and returned as a pure <svg> string.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quickjax-0.1.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quickjax-0.1.0-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

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

Hashes for quickjax-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ffc2065ab357f34091c743c659501184c0bce17d4827b26e3b193fe3ceb8be76
MD5 64a491a09ed479f2d05c5a716b56835e
BLAKE2b-256 d4c8845d366f401d50ac57d378de9892e033efa1a396a9d5db04268ac3d69085

See more details on using hashes here.

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

Hashes for quickjax-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cdcde6770598e8dda805cffd033328ec0610c250fc9515b1451a53a9f641cca1
MD5 63e3e9fa7fac2fe6ebd8339cf9b6a46a
BLAKE2b-256 c2bb37927ee314fd7f6d2cc05d0f029fb28e411b6b9ee569474ed9a15f5175d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page