Skip to main content

Crate rust_latex_parser

Crate rust_latex_parser 

Source
Expand description

§rust-latex-parser

A LaTeX equation parser that produces an abstract syntax tree.

Feed it a string of LaTeX math markup, get back an EqNode tree. No rendering opinions, no font dependencies, no runtime allocation tricks — just parsing.

The tree is yours to walk however you want: render to SVG, convert to MathML, draw on a Skia canvas, dump to a terminal. The crate doesn’t care.

§Quick start

use rust_latex_parser::{parse_equation, EqNode};

let tree = parse_equation("\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}");
assert!(matches!(tree, EqNode::Frac(_, _)));

§Bareword shortcuts

You don’t always need backslashes. The parser recognizes common names as barewords, so pi works the same as \pi, sqrt(x) works like \sqrt{x}, and int_0^1 works like \int_0^1.

Parentheses after bareword operators act as invisible grouping: sqrt(x+1) parses the full x+1 as the argument.

§Supported syntax

CategoryExamples
Superscripts / subscriptsx^2, x_{i+1}, x^2_3
Fractionsa/b, \frac{a}{b}
Square rootssqrt(x), \sqrt{x}
Greek letterspi, \alpha, \Omega
Big operators\sum_{i=0}^{n}, int_0^1, \prod
Limit operatorslim_{x \to 0}, sin, log_2
Accents\hat{x}, \bar{x}, \vec{v}
Matrices\begin{pmatrix} a & b \\\\ c & d \end{pmatrix}
Cases\begin{cases} x & x>0 \\\\ 0 & x=0 \end{cases}
Delimiters\left( ... \right)
Math fonts\mathbb{R}, \mathcal{F}, \mathbf{v}
Binomials\binom{n}{k}
Braces\overbrace{a+b}^{n}, \underbrace{...}_{text}
Stacked\overset{def}{=}, \underset{lim}{=}
130+ symbols\pm, \leq, \in, \rightarrow, \infty, …

§Error handling

The parser never fails. Malformed input produces a best-effort tree: unmatched braces get ignored, unknown commands become literal text nodes, and so on. This is intentional — it keeps live-preview editors responsive while the user is still typing.

Re-exports§

pub use ast::AccentKind;
pub use ast::EqMetrics;
pub use ast::EqNode;
pub use ast::MathFontKind;
pub use ast::MatrixKind;
pub use parser::latex_to_unicode;
pub use parser::parse_equation;

Modules§

ast
AST types for the equation layout tree.
parser
The parser. Turns LaTeX math strings into EqNode trees.