pub fn render(latex: &str) -> RenderedBlockExpand description
Parse a LaTeX math string and render it as a 2D character grid.
This is the primary entry point for the library.
let block = term_maths::render(r"x^2 + y^2 = z^2");
assert_eq!(format!("{}", block), "x² + y² = z²");Examples found in repository?
examples/latex_roundtrip.rs (line 22)
7fn main() {
8 let examples = [
9 r"\frac{a}{b}",
10 r"x^2 + y^2 = z^2",
11 r"\sum_{i=1}^{n} x_i",
12 r"\sqrt{b^2 - 4ac}",
13 r"\begin{pmatrix} a & b \\ c & d \end{pmatrix}",
14 r"\mathbb{R}^n",
15 ];
16
17 for latex in &examples {
18 println!("Original: {}", latex);
19 let roundtrip = to_latex(latex);
20 println!("Round-trip: {}", roundtrip.trim());
21 println!("Rendered:");
22 println!("{}", render(latex));
23 println!();
24 }
25}More examples
examples/render_demo.rs (line 19)
3fn main() {
4 let examples = [
5 (r"\frac{a}{b}", "Simple fraction"),
6 (r"\frac{1}{1+\frac{1}{x}}", "Nested fraction"),
7 (r"x^2", "Superscript"),
8 (r"a_n", "Subscript"),
9 (r"x_i^2", "Super + subscript"),
10 (r"a + b = c", "Sequence"),
11 (r"e^{i\pi} + 1 = 0", "Euler's identity"),
12 (r"\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}", "Quadratic formula"),
13 ];
14
15 for (latex, label) in &examples {
16 println!("--- {} ---", label);
17 println!("LaTeX: {}", latex);
18 println!();
19 println!("{}", render(latex));
20 println!();
21 }
22}examples/matrix_demo.rs (line 38)
3fn main() {
4 let examples = [
5 (
6 r"\begin{pmatrix} a & b \\ c & d \end{pmatrix}",
7 "2x2 pmatrix",
8 ),
9 (
10 r"\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}",
11 "2x2 identity bmatrix",
12 ),
13 (
14 r"\begin{vmatrix} a & b \\ c & d \end{vmatrix}",
15 "2x2 determinant",
16 ),
17 (
18 r"\begin{pmatrix} \frac{1}{2} & 0 \\ 0 & \frac{3}{4} \end{pmatrix}",
19 "Matrix with fractions",
20 ),
21 (
22 r"\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}",
23 "3x3 bmatrix",
24 ),
25 // Math font tests
26 (r"\mathbb{R}", "Blackboard bold R"),
27 (r"\mathbb{Z}", "Blackboard bold Z"),
28 (r"\mathcal{L}", "Calligraphic L"),
29 (r"\mathbf{x}", "Bold x"),
30 (r"\mathfrak{g}", "Fraktur g"),
31 (r"\mathbb{R}^n", "R^n"),
32 ];
33
34 for (latex, label) in &examples {
35 println!("--- {} ---", label);
36 println!("LaTeX: {}", latex);
37 println!();
38 println!("{}", render(latex));
39 println!();
40 }
41}examples/dsp_equations.rs (line 31)
3fn main() {
4 let equations = [
5 // DFT summation
6 (
7 r"X[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-j \frac{2\pi}{N} kn}",
8 "DFT Summation",
9 ),
10 // Convolution integral
11 (
12 r"(f * g)(t) = \int_{-\infty}^{\infty} f(\tau) g(t - \tau) \, d\tau",
13 "Convolution Integral",
14 ),
15 // Transfer function
16 (
17 r"H(z) = \frac{b_0 + b_1 z^{-1} + b_2 z^{-2}}{1 + a_1 z^{-1} + a_2 z^{-2}}",
18 "Transfer Function",
19 ),
20 // Hann window
21 (
22 r"w(n) = 0.5 \left(1 - \cos\left(\frac{2\pi n}{N - 1}\right)\right)",
23 "Hann Window",
24 ),
25 ];
26
27 for (latex, label) in &equations {
28 println!("=== {} ===", label);
29 println!("LaTeX: {}", latex);
30 println!();
31 println!("{}", render(latex));
32 println!();
33 }
34
35 // Also test individual components
36 println!("=== Standalone tests ===\n");
37
38 println!("--- Sum with limits ---");
39 println!("{}\n", render(r"\sum_{n=0}^{N-1}"));
40
41 println!("--- Integral with limits ---");
42 println!("{}\n", render(r"\int_{0}^{1}"));
43
44 println!("--- Product with limits ---");
45 println!("{}\n", render(r"\prod_{i=1}^{n}"));
46
47 println!("--- Delimited fraction ---");
48 println!("{}\n", render(r"\left(\frac{a}{b}\right)"));
49
50 println!("--- Overline ---");
51 println!("{}\n", render(r"\overline{x + y}"));
52
53 println!("--- Hat ---");
54 println!("{}\n", render(r"\hat{x}"));
55
56 println!("--- Sqrt of fraction ---");
57 println!("{}\n", render(r"\sqrt{\frac{a}{b}}"));
58}