| Crates.io | mathlex |
| lib.rs | mathlex |
| version | 0.1.1 |
| created_at | 2026-01-19 17:42:06.707086+00 |
| updated_at | 2026-01-20 11:54:11.667861+00 |
| description | Mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic AST |
| homepage | https://github.com/ChrisGVE/mathlex |
| repository | https://github.com/ChrisGVE/mathlex |
| max_upload_size | |
| id | 2055017 |
| size | 810,539 |
A mathematical expression parser for LaTeX and plain text notation, producing a language-agnostic Abstract Syntax Tree (AST).
\frac{1}{2}, \int_0^1, \sum_{i=1}^n)2*x + 3, sin(x))Add to your Cargo.toml:
[dependencies]
mathlex = "0.1.0"
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/ChrisGVE/mathlex.git", from: "0.1.0")
]
Or in Xcode: File → Add Package Dependencies → Enter https://github.com/ChrisGVE/mathlex.git
use mathlex::{parse, Expression};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let expr = parse("2*x + sin(y)")?;
// Find all variables
let vars = expr.find_variables();
println!("Variables: {:?}", vars); // {"x", "y"}
// Convert back to string
println!("{}", expr); // "2 * x + sin(y)"
Ok(())
}
use mathlex::{parse_latex, Expression};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let expr = parse_latex(r"\frac{1}{2} + \sqrt{x}")?;
// Convert to LaTeX string
println!("{}", expr.to_latex()); // "\frac{1}{2} + \sqrt{x}"
Ok(())
}
import MathLex
do {
// Parse plain text
let expr = try MathExpression.parse("2*x + sin(y)")
print(expr.variables) // ["x", "y"]
print(expr.description) // "2 * x + sin(y)"
// Parse LaTeX
let latex = try MathExpression.parseLatex(#"\frac{1}{2}"#)
print(latex.latex) // "\frac{1}{2}"
} catch {
print("Parse error: \(error)")
}
42, -173.14, 2.5e-3\frac{1}{2}x, y, theta\alpha, \beta, \Gamma\pi, \infty, e, i+, -, *, /, ^, %-x, x!sin, cos, tan, log, ln, exp, sqrt, abs\frac{d}{dx}, \frac{\partial}{\partial x}\int, \int_a^b\lim_{x \to a}\sum_{i=1}^{n}\prod_{i=1}^{n}\begin{pmatrix} a \\ b \end{pmatrix}\begin{bmatrix} a & b \\ c & d \end{bmatrix}x = yx < y, \leq, \geq, \neqmathlex is a pure parsing library. It converts text to AST and back - nothing more.
This design allows different libraries to interpret the AST according to their capabilities:
Derivative nodesFunction nodes numerically[dependencies]
mathlex = { version = "0.1.0", features = ["serde"] }
serde - Enable serialization/deserialization of AST typesffi - Enable Swift FFI bindings (for building XCFramework)MIT License - see LICENSE for details.