Skip to main content

mathra

mathra

Math LaTeX to accessible SVG, without a TeX install, chemistry (chemfig structures, reaction schemes, mechanisms) and commutative diagrams (tikz-cd, xymatrix) included. Every SVG carries the phrase a screen reader says, in 16 languages, and draws with the STIX Two Math font embedded, so it looks the same everywhere. Try it in the browser.

pip install mathra

Wheels for Linux, macOS and Windows, for Python 3.9 and later; nothing else to install.

Usage

import mathra

svg = mathra.tex_to_svg(r"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}")
open("quadratic.svg", "w", encoding="utf-8").write(svg)

r = mathra.render(r"\frac{3}{4}", locale="fr")
r["speech"]        # 'trois quarts'
r["warnings"]      # [] — broken LaTeX is drawn anyway and reported here (E-REC-*)

mathra.tex_to_svg(r"\sum_{n=1}^{\infty} \frac{1}{n^2}", display=False)   # inline style
mathra.tex_to_svg(r"\vertA\vert", strict=True)                           # ValueError, like TeX
mathra.render("sqrt(x)/2", input="asciimath")["speech"]                   # also input="mathml"
mathra.render(r"\chemfig{H_3C-*6(=-=-=-)}")["speech"]                     # 'structure of C7 H8, with 1 aromatic ring'

png = mathra.tex_to_png(r"e^{i\pi} + 1 = 0", background="white")           # PNG bytes, 300 dpi
open("euler.png", "wb").write(png)
tex_to_svg(tex, locale="en", *, display=True, strict=False, input="latex", braille=None, animate=False, steps=False, max_width=None, fallback_fonts=None, labels=None, citations=None) The SVG as a string.
render(tex, locale="en", *, display=True, strict=False, input="latex", braille=None, animate=False, steps=False, max_width=None, fallback_fonts=None, labels=None, citations=None) A dict: svg, speech, text, braille, braille_code, mathml, tree, applied, warnings, boxes. mathml is the equation as MathML Core (display decides display="block" or "inline"), with intent where it adds meaning, and a <semantics> with the source for what MathML cannot draw (spec/mathml.md).
tex_to_png(tex, locale="en", *, display=True, strict=False, input="latex", braille=None, font=None, dpi=300, background=None, color="#111", max_width=None, fallback_fonts=None, labels=None, citations=None) The PNG as bytes.
tex_to_tree(tex, locale="en", *, display=True, strict=False, input="latex", braille=None, animate=False, steps=False, max_width=None, fallback_fonts=None, labels=None, citations=None) The tree of meaning as JSON.
locales() [{"code": "en", "name": "English"}, …]: en, pt-BR, es, fr, de, it, ru, zh, ja, ko, hi, ar, id, tr, nl, pl.

input is "latex", "mathml" or "asciimath"; MathML passed as LaTeX, starting with <math, is recognized on its own. braille is "cmu" or "nemeth"; by default the locale's (CMU in pt-BR and es, Nemeth elsewhere). tex_to_png is for where SVG is not accepted (e-mail, some editors and LMSs): dpi is over a 12pt body, so an em is dpi * 12 / 72 pixels (50 at 300 dpi), each side rounded up to a whole pixel; background (transparent by default) and color, the ink, are CSS colors, and colors set in the equation keep their own. A PNG is only the drawing, with no speech or source: give it the speech as its alt text. labels and citations give the numbers of the document's cross-references, which an equation converted alone cannot know: with labels={"eq:a": 3} and citations={"knuth": 1}, \eqref{eq:a} shows (3), \ref{eq:a} shows 3 and \cite{knuth} shows [1]; an unknown key is (??), ?? or [?], as on LaTeX's first run, with a warning (a ValueError with strict=True). Errors are ValueError. Types are included (py.typed).

What it draws

Every image below is an SVG written by mathra, on a white card so it reads well on dark pages.

Quadratic formula
Quadratic formula
Gaussian integral
Gaussian integral
m × n matrix with dots
Matrix with dots
Piecewise definition
Piecewise definition
Chemical equation (mhchem)
Chemistry (\ce)
Quantity with units (siunitx)
Units (\SI)
Maxwell's equations
Maxwell's equations
Caffeine: fused rings (chemfig)
Caffeine (\chemfig)
Pullback with bent arrows (tikz-cd)
Commutative diagram (tikz-cd)
Nitration of benzene: structures, mhchem and conditions on the arrow
Reaction scheme (\schemestart)

Chemistry and diagrams

chemfig works inside any equation: structures (rings, wedges, Lewis marks), reaction schemes (\schemestart … \arrow{->[above][below]} … \schemestop) and mechanism arrows (\chemmove). The speech gives each structure's molecular formula, counted from the drawing. Commutative diagrams come in amscd, tikz-cd and xymatrix. Every construction, drawn live, is in the gallery and the reference.

What the SVG carries

Speech in <title> linked with aria-labelledby, the source equation and the tree of meaning in <metadata>, color from the page (currentColor), size in em, a selectable text layer and ids unique to each equation. The same input always gives the same bytes, in Python and in every other package.

With animate=True the equation writes itself in the browser, with CSS alone: each glyph fades in, each rule grows and each line is drawn, in the order the layout set them, in about three seconds (nothing moves with prefers-reduced-motion); steps=True numbers each element by that order (data-step) with no animation. Both are off by default, and without them the SVG is unchanged byte for byte.

With max_width=30 (in em) an equation wider than that is broken into lines: after a relation (=, ≤, →…), and only when relations alone are not enough, after + and − too, only at its top level (never inside a fraction, a root, \left…\right, scripts or a table). Each line is a <g data-line> of the SVG; speech, text and braille do not change.

Other math fonts

STIX Two Math is embedded and is the default. Any OpenType math font (an .otf or .ttf with a MATH table, such as Libertinus Math or Fira Math) can be loaded from a path or its bytes:

font = mathra.load_font("LibertinusMath-Regular.otf")  # Font(id=0, name='Libertinus Math')
mathra.tex_to_svg(r"e^{i\pi} + 1 = 0", font=font)

A font without a MATH table raises ValueError. The SVG carries the font's outlines, so check that its license allows that.

The fonts of mathfonts, a catalog of math fonts checked with mathra, load by their name. Nothing is downloaded until you ask; a font is downloaded once, kept in ~/.cache/mathra/fonts ($XDG_CACHE_HOME or $MATHRA_CACHE when set, cache= for another folder, cache=False for none) and refused if it does not match the catalog's sha256:

mathra.catalog_fonts()  # [{'slug': 'euler-math', 'name': 'Euler Math', 'license': 'OFL-1.1', ...}, ...]
font = mathra.load_font_from_catalog("euler-math")
mathra.tex_to_svg(r"e^{i\pi} + 1 = 0", font=font)

All the fonts of the catalog are under licenses that let the SVG carry their outlines (OFL, GUST, Apache-2.0, MIT).

Emoji and other characters the math font lacks

Emoji are drawn from Noto Emoji (the monochrome one), embedded in the package: mathra.tex_to_svg("E = mc^2 😀") draws the face, with no warning, in the text color. For other characters the math font lacks, such as Chinese in \text{…}, pass fallback fonts: any .otf or .ttf with outlines, tried in order before Noto Emoji. A math font from load_font can be one too.

cjk = mathra.load_fallback_font("NotoSansSC-Regular.otf")  # FallbackFont(id=0, name='Noto Sans SC')
mathra.tex_to_svg(r"\text{面积} = \pi r^2", fallback_fonts=[cjk])

A character no font has is drawn as a box, with the warning E-REC-8, or raises ValueError with strict=True. Noto Emoji adds about 540 KB to the wheel.

Also for JavaScript, Rust and the terminal

The same core is an npm package (@mathlabs/mathra), a Rust crate and a command-line tool: github.com/matheusmota-dev-br/mathra. Rules, languages and the full list of supported commands are there too.

License

The code is MIT or Apache-2.0; the embedded STIX Two Math and Noto Emoji fonts are under the SIL Open Font License 1.1.

Metadata

Release files for mathra 1.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mathra 1.12.0
File Size Uploaded Provenance Actions
mathra-1.12.0.tar.gz 1.8 MB With provenance Details for mathra-1.12.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mathra 1.12.0
File Actions
mathra-1.12.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for mathra-1.12.0-cp39-abi3-win_amd64.whl
mathra-1.12.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for mathra-1.12.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
mathra-1.12.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for mathra-1.12.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
mathra-1.12.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for mathra-1.12.0-cp39-abi3-macosx_11_0_arm64.whl
mathra-1.12.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for mathra-1.12.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 17.4 MB

Release files / mathra-1.12.0.tar.gz

Download URL mathra-1.12.0.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
bea9eea1dd642bef5a483def3e2c5f98b01c8042f10434a9998f67e3e709f9dd
BLAKE2b-256 checksum
How to use checksums
7a87af1e53de4337d875a7186a35694cbd6af29f6358a9f37be41a23c4f950f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / mathra-1.12.0-cp39-abi3-win_amd64.whl

Download URL mathra-1.12.0-cp39-abi3-win_amd64.whl
Size 3.0 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e12b95b45d3eade5d52990f15023fd06179f8b0bf8fa396fa9e4416a697e0d37
BLAKE2b-256 checksum
How to use checksums
cc21838a69c92e686d49912d969e20321c34c912e73df007cb3ed10aeb1361a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / mathra-1.12.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL mathra-1.12.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 3.2 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
126b142ca5062229cc32f0e3b5e12133d3fa72ace108a3a6d97cd978b7731b2d
BLAKE2b-256 checksum
How to use checksums
0ea106a3422f3c78e04dd7f4d0c0cebfe40fe157b272faad7ead7fe332e48118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / mathra-1.12.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL mathra-1.12.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 3.2 MB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
3eed72593fb45f33f3b0c6d25268cba7e827c4690eebc173beba3935c6588bcc
BLAKE2b-256 checksum
How to use checksums
47a512d7c3d54e423d523334388ede5cb58ca00da691e9a6f571b4ccd667d523
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / mathra-1.12.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL mathra-1.12.0-cp39-abi3-macosx_11_0_arm64.whl
Size 3.0 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1bee586b3fde7aad42905ec530b1bfcbc3d78d96ee8a9be6cb8e989991e63637
BLAKE2b-256 checksum
How to use checksums
4bd3a431d64c3b5d674a0381f448e6c1a1555b9276d4bf6de2dc4e34ad2a1924
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / mathra-1.12.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL mathra-1.12.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 3.1 MB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
7c04990cc51e1ee73813eb510770403212d1107d2d30475cfb9630464f2a2cf7
BLAKE2b-256 checksum
How to use checksums
218654fd7a2fc608a55c1127e20d0a5ffbb9c8080ebcc24ed10f660adc4ca7f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for mathra 1.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for mathra-1.12.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page