Expression Formatting
Format mathematical expressions for display in multiple notations. Supports LaTeX, Unicode, Wolfram, and custom formatters for different output targets.
Code Examples
Basic Formatting
Format expressions in different notations
use mathhook::prelude::*;
use mathhook::formatter::{LatexFormatter, UnicodeFormatter, WolframFormatter};
let x = symbol!(x);
let expr = expr!(x^2 + 2*x + 1);
// LaTeX
let latex = LatexFormatter::new().format(&expr);
println!("{}", latex); // x^{2} + 2 \cdot x + 1
// Unicode (pretty-print)
let unicode = UnicodeFormatter::new().format(&expr);
println!("{}", unicode); // x² + 2·x + 1
// Wolfram
let wolfram = WolframFormatter::new().format(&expr);
println!("{}", wolfram); // x^2 + 2*x + 1
Type-Aware Formatting
Noncommutative symbols formatted correctly
use mathhook::prelude::*;
use mathhook::formatter::latex::LatexFormatter;
// Matrix symbols (bold)
let A = symbol!(A; matrix);
let B = symbol!(B; matrix);
let matrix_expr = expr!(A * B);
let formatter = LatexFormatter::new();
println!("{}", formatter.format(&matrix_expr));
// Output: \mathbf{A}\mathbf{B}
// Operator symbols (hat)
let p = symbol!(p; operator);
let x = symbol!(x; operator);
let op_expr = expr!(p * x);
println!("{}", formatter.format(&op_expr));
// Output: \hat{p}\hat{x}
Customized LaTeX Output
Configure formatter behavior
use mathhook::prelude::*;
use mathhook::formatter::latex::LatexFormatter;
// Configure formatter
let formatter = LatexFormatter::new()
.with_precision(6) // Float precision
.with_explicit_multiplication(true) // Show all * as \cdot
.with_compact_fractions(false); // Use \frac always
let expr = expr!(2*x / 3);
println!("{}", formatter.format(&expr));
// Output: \frac{2 \cdot x}{3}
Educational Step Formatting
Format step-by-step explanations
use mathhook::prelude::*;
use mathhook::formatter::latex::LatexFormatter;
let x = symbol!(x);
let expr = expr!(x^2 + 2*x + 1);
// Generate step-by-step LaTeX
let formatter = LatexFormatter::new();
println!("Step 1: Start with {}", formatter.format(&expr));
let factored = expr.factor(); // (x+1)^2
println!("Step 2: Factor as {}", formatter.format(&factored));