LaTeX Parsing and Mathematical Notation
Parse and generate LaTeX notation for mathematical expressions. Full bidirectional support: LaTeX → Expression and Expression → LaTeX. Automatic type inference for matrix symbols (\mathbf{A}), operator symbols (\hat{p}), and implicit multiplication.
Code Examples
Basic LaTeX Parsing
Parse standard mathematical expressions
use mathhook::parser::{Parser, ParserConfig};
let parser = Parser::new(ParserConfig::default());
// Basic arithmetic
let expr = parser.parse(r"2 + 3 \cdot 4")?; // 2 + 3*4
// 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}")?;
// Exponents
let expr = parser.parse(r"e^{-x^2}")?; // Gaussian
Greek Letters and Constants
Parse Greek symbols and mathematical constants
use mathhook::parser::Parser;
let parser = Parser::new(ParserConfig::default());
// Greek symbols (lowercase)
let expr = parser.parse(r"\alpha + \beta + \gamma")?;
// Greek symbols (uppercase functions)
let expr = parser.parse(r"\Gamma(n)")?; // Gamma function
// Mathematical constants
let expr = parser.parse(r"\pi r^2")?; // π*r²
let expr = parser.parse(r"e^{i\pi} + 1")?; // Euler's identity
let expr = parser.parse(r"\phi = \frac{1+\sqrt{5}}{2}")?; // Golden ratio
Matrix and Operator Symbols
Automatic type inference from LaTeX notation
use mathhook::parser::Parser;
let parser = Parser::new(ParserConfig::default());
// Matrix symbols (bold, noncommutative)
let expr = parser.parse(r"\mathbf{A} \mathbf{B}")?;
// Creates: symbol!(A; matrix) * symbol!(B; matrix)
// Operator symbols (quantum mechanics)
let expr = parser.parse(r"\hat{p} \hat{x}")?;
// Creates: symbol!(p; operator) * symbol!(x; operator)
// Mixed scalar and matrix
let expr = parser.parse(r"x \mathbf{A}")?;
// Creates: symbol!(x) * symbol!(A; matrix)
Generating LaTeX Output
Convert expressions back to LaTeX
use mathhook::prelude::*;
use mathhook::formatter::latex::LaTeXFormatter;
let x = symbol!(x);
// Simple expression
let expr = expr!(x^2 / 2);
let latex = expr.to_latex(None)?;
// Returns: "\frac{x^{2}}{2}"
// Matrix expression
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);
let expr = expr!(A * B);
let latex = expr.to_latex(None)?;
// Returns: "\mathbf{A}\mathbf{B}"
// Complex expression
let expr = expr!(sin(x) + cos(x^2));
let latex = expr.to_latex(None)?;
// Returns: "\sin\left(x\right) + \cos\left(x^{2}\right)"
Implicit Multiplication
Automatic insertion of multiplication operators
use mathhook::parser::Parser;
let parser = Parser::new(ParserConfig::default());
// Number-variable: 2x → 2*x
let expr = parser.parse("2x")?;
// Parentheses: (a)(b) → a*b
let expr = parser.parse("(a)(b)")?;
// Functions: sin(x)cos(y) → sin(x)*cos(y)
let expr = parser.parse(r"\sin(x)\cos(y)")?;
// Mixed: 2πr → 2*π*r
let expr = parser.parse(r"2\pi r")?;
Calculus Notation
Parse derivatives, integrals, limits
use mathhook::parser::Parser;
let parser = Parser::new(ParserConfig::default());
// Derivative notation
let expr = parser.parse(r"\frac{d}{dx} x^2")?;
// Integral notation
let expr = parser.parse(r"\int x^2 \, dx")?;
// Definite integral
let expr = parser.parse(r"\int_0^1 x^2 \, dx")?;
// Limit notation
let expr = parser.parse(r"\lim_{x \to 0} \frac{\sin(x)}{x}")?;
// Summation
let expr = parser.parse(r"\sum_{i=1}^{n} i^2")?;