Skip to main content

rust_latex_parser/
lib.rs

1//! # rust-latex-parser
2//!
3//! A LaTeX equation parser that produces an abstract syntax tree.
4//!
5//! Feed it a string of LaTeX math markup, get back an [`EqNode`] tree. No
6//! rendering opinions, no font dependencies, no runtime allocation tricks —
7//! just parsing.
8//!
9//! The tree is yours to walk however you want: render to SVG, convert to
10//! MathML, draw on a Skia canvas, dump to a terminal. The crate doesn't care.
11//!
12//! # Quick start
13//!
14//! ```
15//! use rust_latex_parser::{parse_equation, EqNode};
16//!
17//! let tree = parse_equation("\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}");
18//! assert!(matches!(tree, EqNode::Frac(_, _)));
19//! ```
20//!
21//! # Bareword shortcuts
22//!
23//! You don't always need backslashes. The parser recognizes common names as
24//! barewords, so `pi` works the same as `\pi`, `sqrt(x)` works like
25//! `\sqrt{x}`, and `int_0^1` works like `\int_0^1`.
26//!
27//! Parentheses after bareword operators act as invisible grouping:
28//! `sqrt(x+1)` parses the full `x+1` as the argument.
29//!
30//! # Supported syntax
31//!
32//! | Category | Examples |
33//! |----------|---------|
34//! | Superscripts / subscripts | `x^2`, `x_{i+1}`, `x^2_3` |
35//! | Fractions | `a/b`, `\frac{a}{b}` |
36//! | Square roots | `sqrt(x)`, `\sqrt{x}` |
37//! | Greek letters | `pi`, `\alpha`, `\Omega` |
38//! | Big operators | `\sum_{i=0}^{n}`, `int_0^1`, `\prod` |
39//! | Limit operators | `lim_{x \to 0}`, `sin`, `log_2` |
40//! | Accents | `\hat{x}`, `\bar{x}`, `\vec{v}` |
41//! | Matrices | `\begin{pmatrix} a & b \\\\ c & d \end{pmatrix}` |
42//! | Cases | `\begin{cases} x & x>0 \\\\ 0 & x=0 \end{cases}` |
43//! | Delimiters | `\left( ... \right)` |
44//! | Math fonts | `\mathbb{R}`, `\mathcal{F}`, `\mathbf{v}` |
45//! | Binomials | `\binom{n}{k}` |
46//! | Braces | `\overbrace{a+b}^{n}`, `\underbrace{...}_{text}` |
47//! | Stacked | `\overset{def}{=}`, `\underset{lim}{=}` |
48//! | 130+ symbols | `\pm`, `\leq`, `\in`, `\rightarrow`, `\infty`, ... |
49//!
50//! # Error handling
51//!
52//! The parser never fails. Malformed input produces a best-effort tree:
53//! unmatched braces get ignored, unknown commands become literal text nodes,
54//! and so on. This is intentional — it keeps live-preview editors responsive
55//! while the user is still typing.
56
57pub mod ast;
58pub mod parser;
59
60pub use ast::{AccentKind, EqMetrics, EqNode, MathFontKind, MatrixKind};
61pub use parser::{latex_to_unicode, parse_equation};