About this project

The `math-to-speech` crate is a Rust library designed to convert LaTeX mathematical expressions into natural spoken English text. Unlike general-purpose document text-to-speech systems that read rendered glyphs literally (often ignoring mathematical semantics), this tool parses the underlying structure of the formula to generate phrases as a human would speak them aloud. For example, `x^2` becomes "x squared", `\frac{\pi}{2}` becomes "pi over 2", and `\sqrt{2}` becomes "the square root of 2". ### Key Features * **Semantic Parsing**: Utilizes `mitex-parser` to analyze the LaTeX AST, ensuring that the output reflects mathematical meaning rather than just character sequences. * **Context-Aware Bracket Handling**: Distinguishes between function application and grouping. `x(t)` is spoken as "x of t", while `[a, b]` is spoken as "a, b". It also differentiates continuous-time notation (`x(t)`) from discrete-index notation (`x[n]` → "x at index n"). * **Strict Error Handling**: Returns an `Err` for unrecognized commands or unsupported constructs instead of guessing. This allows callers to implement fallback strategies (e.g., reading raw LaTeX or using a placeholder) without risking misinterpretation of the formula's meaning. * **Consistent LaTeX Subset**: Targets the same LaTeX subset as the `math-render` crate, allowing documents to be both visually rendered and audibly spoken from the same source without divergence in supported syntax. ### Supported Constructs The current version supports: * Fractions (`\frac`) * Square roots (`\sqrt`) * Subscripts and superscripts (including special cases like `^2` → "squared", `^3` → "cubed") * Sums, products, and integrals (as prefix phrases) * Greek letters and common symbols/relations (e.g., `\alpha \leq \beta` → "alpha less than or equal to beta") * Named functions (`\sin`, `\cos`, `\log`, etc.) when used with parenthesized arguments * Text and mathrm environments (`\text{...}`, `\mathrm{...}`) ### Limitations The project is in an early stage. Currently unsupported: * Matrices * Aligned or multi-line equations * Cases and piecewise definitions * Bare function calls without parentheses (e.g., `\sin x` is spoken as "sine x" rather than "sine of x") ### Usage ```rust use math_to_speech::speak; fn main() { match speak(r"\frac{\pi}{2}") { Ok(phrase) => println!("{}", phrase), // Output: pi over 2 Err(e) => eprintln!("Error: {}", e), } } ```