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. 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.11.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.11.0
File Size Uploaded Provenance Actions
mathra-1.11.0.tar.gz 1.8 MB With provenance Details for mathra-1.11.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for mathra 1.11.0
File Actions
mathra-1.11.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for mathra-1.11.0-cp39-abi3-win_amd64.whl
mathra-1.11.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.11.0-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
mathra-1.11.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for mathra-1.11.0-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
mathra-1.11.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for mathra-1.11.0-cp39-abi3-macosx_11_0_arm64.whl
mathra-1.11.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details for mathra-1.11.0-cp39-abi3-macosx_10_12_x86_64.whl

Total release size: 17.2 MB

Release files / mathra-1.11.0.tar.gz

Download URL mathra-1.11.0.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
042fe578893093e4e6b75fb4171ee51275c318279cdfb06683a1f7c39b9743d4
BLAKE2b-256 checksum
How to use checksums
a7fc2d89cc2addcefbb61db7f0a64fdff94a2dd84a1845cb692b67bed55fc33f
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 8, 2026.

Transparency log

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

Download URL mathra-1.11.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
a7ea6392409297504f64e141eb9202750ef298ac5f907f44fc4719eeab7a862c
BLAKE2b-256 checksum
How to use checksums
1ed67caf9755089b44c943e2cbeaf29a0947bc06f0680fe1ea37dd43667df472
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 8, 2026.

Transparency log

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

Download URL mathra-1.11.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
6ab55b18304970eee575e2f942c1774c2ba767f4a456b614cf5c2e6ba15ee92d
BLAKE2b-256 checksum
How to use checksums
403f0e44478d9e5634dbae12f0569649f518a2b3b31bc0286bb94a1040b1709a
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 8, 2026.

Transparency log

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

Download URL mathra-1.11.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
9480528033008cfcf4724293a2a8869384d31df36870bf1e3947c6fdff81ab76
BLAKE2b-256 checksum
How to use checksums
50ba733a46ead9c46680ad3f5e095087f2059eb2485ce527c1bf4214b4d0f8de
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 8, 2026.

Transparency log

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

Download URL mathra-1.11.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
da84321afb580d8301b0dcf315bf8de7412ba6a35ae8ce1f7b9f4f26bc518518
BLAKE2b-256 checksum
How to use checksums
6dcbd55a711d0777f4e8c26d33f3fae49b1928b16d486d2116af1e7a706763c8
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 8, 2026.

Transparency log

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

Download URL mathra-1.11.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
502e444dbfe1fc9d3143e17ec2e9461f6e6200f891ce79dba48f77ee86956f27
BLAKE2b-256 checksum
How to use checksums
a4490b07ddd60c7613ca5c99e91162992ea3f758db4981d65fdc9ed17458b7ae
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 8, 2026.

Transparency log

Security for mathra 1.11.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.11.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 8, 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