LaTeX Notation

Parse and generate beautiful LaTeX notation for mathematical expressions. MathHook provides full bidirectional support: Parse LaTeX → Expression, Expression → LaTeX. Includes automatic type inference, implicit multiplication, and comprehensive coverage of 150+ LaTeX commands.

Code Examples

Basic LaTeX Parsing

Parse common LaTeX expressions

use mathhook::parser::{Parser, ParserConfig};

let parser = Parser::new(ParserConfig::default());

// Fractions
let expr = parser.parse(r"\frac{x^2 + 1}{x - 1}")?;

// Functions
let expr = parser.parse(r"\sin(x) + \cos(y)")?;

// Square roots
let expr = parser.parse(r"\sqrt{x^2 + y^2}")?;

Expression to LaTeX

Format expressions as LaTeX

use mathhook::prelude::*;
use mathhook::formatter::latex::LaTeXFormatter;

let x = symbol!(x);
let expr = expr!(x^2 / 2);
let latex = expr.to_latex(None)?;
// Returns: \frac{x^{2}}{2}

Noncommutative Type Inference

Automatic symbol type inference from LaTeX notation

use mathhook::parser::latex::parse_latex;

// Matrix symbols (noncommutative): \mathbf{A}
let expr = parse_latex(r"\mathbf{A}\mathbf{X} = \mathbf{B}")?;
// Creates matrix symbols A, X, B where A*X ≠ X*A

// Operator symbols (noncommutative): \hat{p}
let expr = parse_latex(r"[\hat{x}, \hat{p}] = i\hbar")?;
// Creates operator symbols (quantum mechanics commutator)

Calculus Notation

Parse calculus operations in LaTeX

use mathhook::parser::latex::parse_latex;

// Indefinite integral
let expr = parse_latex(r"\int x^2 \, dx")?;

// Definite integral
let expr = parse_latex(r"\int_0^{\infty} e^{-x} \, dx")?;

// Summations
let expr = parse_latex(r"\sum_{i=1}^{n} i^2")?;

// Limits
let expr = parse_latex(r"\lim_{x \to 0} \frac{\sin(x)}{x}")?;

🔗 Related Topics