Expand description
LaTeX-Rust: a pure Rust LaTeX math renderer.
Parse a math string to a typed AST, lay it out with a TeX-faithful box
model whose dimensions are zenith-float Dim values (no hardware f32 /
f64 in layout arithmetic), and render to SVG, PNG (features = ["png"]),
or egui shapes (features = ["egui"]).
Crate layout follows the build-sheet architecture: parser,
layout, font, render (svg / png / egui).
Unsupported constructs return Error — never a fake render.
§Examples
use latex_rust::{parse, layout, tokenize, lookup, MathFont, MathStyle, Dim, MathBox};
let ast = parse(r"\frac{1}{2}").expect("parse");
assert_eq!(ast.gold(), r#"(frac (atom Ord "1") (atom Ord "2"))"#);
let tokens = tokenize(r"\frac{1}{2}").expect("tokens");
assert!(!tokens.is_empty());
assert_eq!(lookup(r"\alpha").unwrap().glyph, "α");
let font = MathFont::stix_two_math().expect("STIX Two Math");
assert_eq!(font.units_per_em(), 1000);
let boxed = layout(&ast, &font, MathStyle::Text).expect("layout");
assert!(!boxed.width.is_zero());
let svg = latex_rust::latex_to_svg(r"x", &font, &latex_rust::SvgOptions::new()).expect("svg");
assert!(svg.contains("<path"));
let packed = MathBox::hpack(vec![
MathBox::rule(Dim::one(), Dim::zero(), Dim::zero()),
MathBox::rule(Dim::ratio(1, 2), Dim::zero(), Dim::zero()),
]);
assert_eq!(packed.width, Dim::ratio(3, 2));Re-exports§
pub use font::GlyphMetrics;pub use font::MathFont;pub use font::STIX_TWO_MATH_NAME;pub use font::STIX_TWO_MATH_OTF;pub use font::STIX_TWO_MATH_SHA256;pub use layout::layout;pub use layout::layout_with_numbering;pub use layout::BoxContent;pub use layout::MathBox;pub use layout::MathParams;pub use layout::MathStyle;pub use layout::NumberFormat;pub use layout::NumberStyle;pub use layout::NumberingConfig;pub use layout::NumberingState;pub use parser::format_tokens;pub use parser::parse;pub use parser::parse_with_colors;pub use parser::preprocess;pub use parser::tokenize;pub use parser::AccentKind;pub use parser::AtomKind;pub use parser::ColSpec;pub use parser::DelimSize;pub use parser::Delimiter;pub use parser::EnvRow;pub use parser::EqNumber;pub use parser::IntegralKind;pub use parser::MathNode;pub use parser::MatrixStyle;pub use parser::PhantomKind;pub use parser::SpaceKind;pub use parser::TextStyle;pub use parser::Token;pub use render::egui::latex_to_shapes;eguipub use render::egui::paint_egui;eguipub use render::egui::shapes;eguipub use render::egui::render_egui;pub use render::egui::EguiOptions;pub use render::png::latex_to_png;pub use render::png::render_png;pub use render::png::PngBackground;pub use render::png::PngOptions;pub use render::svg::latex_to_svg;pub use render::svg::render_svg;pub use render::svg::SvgOptions;
Modules§
- font
- OpenType MATH metrics and the embedded STIX Two Math face.
OpenType math font metrics. Integer font units →
Dim. - layout
- AST → TeX-faithful
MathBox. TeX-faithful math box model. Dimensions areDimfrom zenith-float 1.0. - parser
- LaTeX math →
MathNodeAST. LaTeX math →MathNodeAST. - render
- Box model → SVG, PNG, or egui primitives.
Render backends: SVG, PNG (
tiny-skia, featurepng), and egui (features = ["egui"]).
Structs§
- Color
- 8-bit sRGB color. Values are integers; conversion from unit intervals uses
Dim. - Color
Table - Named-color registry, including
\definecoloroverrides. - Dim
- TeX-style dimension: width, height, depth, italic correction, mu.
- Symbol
Entry - One row of the shipped symbol table.
Enums§
- Error
- Crate-level error.
- Font
Error - Font loader or metric lookup failure.
- Parse
Error - Tokenizer / parser failure.
- Symbol
Kind - How a catalog entry is used on a math keyboard / in the parser.
Constants§
- DIM_
PREC - Working precision for layout
Dimvalues, in bits.
Functions§
- category_
count - Number of entries in a category.
- glyph_
char - Single-character catalog glyph for
query, if the row is a lone code point. - lookup
- Look up by raw table LaTeX (
\alpha,\frac{}{}) or command name (alpha). - named_
color - Named color from the standard table (
red,RoyalBlue, …). - parse_
color_ spec - Parse
{model}{spec}as in\definecolor/\color[model]{spec}. - styled_
char - Map
chunder a math font style. Unmapped characters are returned unchanged. - symbol_
atom_ kind - Atom class for a control-sequence name (
times,leq,langle) or a single-character operator (+,=). - symbols
- All shipped symbols, in table order.