LaTeX-Rust
Please upgrade to latex-rust 2.1.0. Versions 1.0.0 through 1.0.4 abort the process with a stack overflow when given deeply nested input, which matters if you render LaTeX from untrusted sources. 1.0.5 fixes that for 1.x with no API changes (
cargo update -p latex-rust). 2.0.0 has the same fixes plus breaking API changes; see the 2.0.0 release notes. 1.0.5 and 2.0.1 also fix the egui backend, which failed with "glyph tessellation" on some glyphs (for example\partial_t u). 2.1.0 keeps those fixes and corrects TeX and OpenType MATH layout, so some formulas draw differently from 2.0.1.
Repository: https://github.com/jscarr64/LaTeX-Rust
Pure Rust LaTeX math renderer. No JavaScript. No webview. No runtime dependencies.
It parses LaTeX math into a typed AST, lays the AST out with a TeX-faithful box
model whose dimensions are exact rationals (Dim; no hardware f32/f64 in the
layout path), and renders to SVG, PNG (features = ["png"]), or egui shapes
(features = ["egui"]).
Features
- Parses LaTeX math to a typed AST
- TeX-faithful layout engine (Appendix G exact)
- SVG output — self-contained, scalable, no font embedding required
- PNG output (
features = ["png"]) via tiny-skia - egui integration (
features = ["egui"]) for native desktop apps - Full symbol coverage — Greek, AMS, arrows, operators, font styles
- Complete accent and decoration support
- Multiline environments — align, gather, multline, cases, array
- Color support — named, RGB, HTML, CMYK, gray,
\definecolor - 100% pure Rust — no C, no Python, no shell, no subprocesses
- MIT OR Apache-2.0
src/parser/ LaTeX → AST
src/layout/ AST → box model
src/font/ STIX Two Math metrics / TrueType loader
src/render/svg box model → SVG
src/render/png box model → PNG (`tiny-skia`, feature `png`)
src/render/egui box model → egui Shapes (feature `egui`)
golds/ gold tests (repository only)
benches/ parse / layout / render timings
This crate does not typeset documents. Text-mode LaTeX, TikZ, chemistry, and PDF
are out of scope and return Err — never a fake render.
Inventory: capabilities. Patches: CONTRIBUTING.md.
Quick start
[]
= "2.1"
use ;
let ast = parse.expect;
let font = stix_two_math.expect;
let boxed = layout.expect;
assert!;
let svg = latex_to_svg.expect;
assert!;
Layout math is exact rationals in this crate. It does not depend on a numeric library.
MSRV is 1.76 (matches optional egui 0.28).
Usage
SVG
use ;
let font = stix_two_math.unwrap;
let svg = latex_to_svg.unwrap;
PNG
Enable with features = ["png"]. tiny-skia rasterizes the same MathBox tree
as the SVG backend. SIMD is selected at runtime:
- On x86_64 with AVX2 — tiny-skia runs at peak performance automatically
- On Apple Silicon — tiny-skia uses the ARM NEON path, also fast
- No user configuration needed
- No compile flags required
- Works correctly on every supported architecture
Without the feature, latex_to_png / render_png return Err(Unsupported).
#
#
egui
Enable with features = ["egui"]. Glyphs become egui::Mesh; rules and color
boxes become Shape::Rect. No SVG intermediate. Callers should keep the
returned Vec<Shape> if the same expression is painted every frame (cache hit).
#
#
Supported LaTeX
Math mode only. Command and environment coverage is data/symbols.tsv.
Look up a command with latex_rust::lookup. Missing glyphs and unknown commands
are Err.
Color: named_color / parse_color_spec. Channel values use Dim. spot and
unknown names return Err. SVG, PNG, and egui all take Color::to_rgba8().
Performance
Times below are median-of-batch averages from cargo bench --features png,egui
on this machine (release, in-crate Dim layout). Re-run the bench on your
hardware.
| Operation | Target | Measured |
|---|---|---|
Parse \frac{1}{2} |
< 50µs | 1.8µs |
| Parse full display equation | < 200µs | 3.3µs |
Layout \frac{1}{2} |
< 100µs | 13µs |
| Layout full display equation | < 500µs | 47µs |
SVG \frac{1}{2} |
< 500µs | 232µs |
| SVG full display equation | < 1ms | 951µs |
| PNG 144 DPI | < 5ms | 142µs |
| PNG 300 DPI | < 15ms | 350µs |
| egui inline (cache miss) | < 2ms | 216µs |
| egui inline (cache hit) | < 0.1ms | 29µs |
KaTeX cold start in a browser is typically 200–400ms (JS parse + layout +
font). This crate’s first MathFont::stix_two_math() plus an SVG of a short
expression is on the order of the layout+SVG rows above after the face is loaded
(embedded STIX Two Math, no network). There is no JS runtime to warm up.
Fonts
The crate embeds STIX Two Math 2.13 (SIL OFL 1.1) and loads metrics through
ttf-parser using integer font units converted to Dim. Glyph outlines become
SVG <path> elements (and PNG/egui meshes when those features are on).
Notes
2.1.0 is the current release. baselogic contributed the TeX and OpenType MATH
layout work in pull request 13. His commits are in the history under his name. They cover delimiter sizing with
a physical \delimitershortfall, fraction width without null-delimiter padding,
radical variant slack, large-operator limits, ssty script alternates,
SpaceAfterScript, row math italic correction, AMSMath column spacing,
display-integral side scripts, and the exact-rational and egui index overflow
checks.
Three rules were corrected on top of that work. A wide hat or tilde uses the
smallest MATH variant that covers the nucleus. AMSMath rows use LaTeX's 12 pt
\baselineskip, plus \jot in aligned. A radical degree is placed by the
bottom of its ink.
License
MIT OR Apache-2.0. STIX Two Math remains under the SIL Open Font License 1.1.
See NOTICE.