term_maths/lib.rs
1//! # term-maths
2//!
3//! Character-grid mathematical notation renderer for terminals.
4//!
5//! Accepts LaTeX math input and renders it as 2D Unicode character art suitable
6//! for display in a terminal. Targets JuliaMono as the recommended font.
7//!
8//! ## Quick Start
9//!
10//! ```rust
11//! let block = term_maths::render(r"\frac{a}{b}");
12//! println!("{}", block);
13//! // a
14//! // ───
15//! // b
16//! ```
17//!
18//! ## Output Backends
19//!
20//! - **Plain text** — always available via [`render()`] and [`Display`](std::fmt::Display)
21//! - **crossterm** — direct terminal output (feature `crossterm`)
22//! - **ratatui** — TUI widget (feature `ratatui`)
23//! - **LaTeX round-trip** — serialise back to LaTeX via [`to_latex()`]
24
25pub mod latex_renderer;
26pub mod layout;
27pub mod mathfont;
28pub mod rendered_block;
29pub mod renderer;
30
31#[cfg(feature = "crossterm")]
32pub mod crossterm_renderer;
33
34#[cfg(feature = "ratatui")]
35pub mod ratatui_widget;
36
37#[cfg(feature = "python")]
38pub mod python;
39
40pub use latex_renderer::LatexRenderer;
41pub use rendered_block::RenderedBlock;
42pub use renderer::{MathRenderer, TerminalRenderer};
43
44#[cfg(feature = "crossterm")]
45pub use crossterm_renderer::CrosstermRenderer;
46
47#[cfg(feature = "ratatui")]
48pub use ratatui_widget::MathWidget;
49
50use rust_latex_parser::parse_equation;
51
52/// Parse a LaTeX math string and render it as a 2D character grid.
53///
54/// This is the primary entry point for the library.
55///
56/// ```rust
57/// let block = term_maths::render(r"x^2 + y^2 = z^2");
58/// assert_eq!(format!("{}", block), "x² + y² = z²");
59/// ```
60pub fn render(latex: &str) -> RenderedBlock {
61 let ast = parse_equation(latex);
62 layout::layout(&ast)
63}
64
65/// Parse a LaTeX math string and serialise it back to LaTeX (round-trip).
66///
67/// Useful for normalising LaTeX input or for the LaTeX output backend.
68pub fn to_latex(latex: &str) -> String {
69 let ast = parse_equation(latex);
70 let renderer = LatexRenderer;
71 renderer.render(&ast)
72}