Crate mathlex

Crate mathlex 

Source
Expand description

§mathlex

A mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic Abstract Syntax Tree (AST).

§Overview

mathlex is a pure parsing library that converts mathematical expressions in LaTeX or plain text format into a well-defined AST. The library does NOT perform any evaluation or mathematical operations - interpretation of the AST is entirely the responsibility of consuming libraries.

§Features

  • LaTeX Parsing: Parse mathematical LaTeX notation
  • Plain Text Parsing: Parse standard mathematical expressions
  • Rich AST: Comprehensive AST for algebra, calculus, linear algebra
  • Utilities: Variable extraction, substitution, string conversion

§Quick Start

use mathlex::{parse, parse_latex, Expression, BinaryOp};

// Parse plain text
let expr = parse("2*x + sin(y)").unwrap();

// Parse LaTeX
let expr = parse_latex(r"\frac{1}{2}").unwrap();

// Verify the parsed structure
if let Expression::Binary { op: BinaryOp::Div, .. } = expr {
    println!("Parsed as division");
}

§Configuration

For advanced parsing options, use ParserConfig:

use mathlex::{parse_with_config, ParserConfig};

let config = ParserConfig {
    implicit_multiplication: true,
};

// Parse with custom configuration (config reserved for future use)
let expr = parse_with_config("2*x + 3", &config).unwrap();

§Design Philosophy

mathlex follows the principle of single responsibility:

  • Parse text into AST
  • Convert AST back to text (plain or LaTeX)
  • Query AST (find variables, functions)
  • Transform AST (substitution)

What mathlex does NOT do:

  • Evaluate expressions
  • Simplify expressions
  • Solve equations
  • Perform any mathematical computation

This design allows mathlex to serve as a shared foundation for both symbolic computation systems (like thales) and numerical libraries (like NumericSwift) without creating dependencies between them.

Re-exports§

pub use ast::BinaryOp;
pub use ast::Direction;
pub use ast::Expression;
pub use ast::InequalityOp;
pub use ast::IntegralBounds;
pub use ast::MathConstant;
pub use ast::UnaryOp;
pub use error::ParseError;
pub use error::ParseErrorKind;
pub use error::ParseResult;
pub use error::Position;
pub use error::Span;
pub use latex::ToLatex;
pub use parser::parse;
pub use parser::parse_latex;

Modules§

ast
Abstract Syntax Tree (AST) Types
display
Display Trait Implementations for AST (Plain Text)
error
Error types for mathlex parsing operations.
latex
LaTeX Conversion for AST
parser
Parser module for mathlex.
util
AST Utility Functions

Structs§

ParserConfig
Configuration options for the mathematical expression parser.

Constants§

VERSION
Placeholder for library version

Functions§

parse_with_config
Parses a plain text mathematical expression with custom configuration.