A formula parses into a real syntax tree, driven by xparse-style argument specifications for every command in a structured knowledge base; you get an editable Document tree to query and mutate, and a profile-based transform engine that normalizes formulas into canonical forms tuned for specific downstream uses.
Programs that process LaTeX formulas — cleaning OCR training data, comparing formulas for equivalence, preparing pretraining corpora, building math editors — keep reinventing the same fragile layer of regular expressions and one-off heuristics that guess what \frac, \over, or \quantity mean. The hard part was never the string manipulation; it is knowing the argument structure of every command well enough to work on formulas as syntax trees instead of text. That is why TeXForm parses against a command knowledge base rather than matching patterns: a regex cannot tell that \frac takes two arguments while \sqrt takes an optional one followed by a mandatory one, because argument structure is not something the surface syntax reveals. Parsing explains the full mental model.
Three pillars#
TeXForm is built on three capabilities, each layered on the one before it:
- Knowledge-driven parser. Formulas parse into real syntax trees, driven by argument specifications for every command in the built-in packages:
base,ams,physics,braket,bboldx,boldsymbol,color, andtextmacros. By default, a parser omitsphysicsbecause it redefines\bra,\ket, and friends thatbraketalso owns, so you turn it on explicitly when your formulas use physics-package notation. Unknown commands and unparseable fragments survive as explicit nodes under your control, never a crash. Parsing covers the package set and how to change it. - Editable document tree. Parsing produces a
Document— a DOM-style tree you can query, mutate, and serialize back to LaTeX. Edits are validated and return structured errors; no panic ever reaches the caller. The serializer guarantees text idempotency. - Profile-based transform engine. Normalization has no single correct answer, so TeXForm never imposes one true form. You pick a profile that names your downstream use, and a curated rule pipeline produces the canonical form for that case — from polished authoring output to aggressive corpus normalization.
Where to start#
For corpus cleaning and formula comparison:
- Quick start — normalize your first formula in Python, JavaScript, or Rust.
- Normalize a corpus — reuse an engine with error handling and statistics.
For editors, linters, and converters:
- What TeXForm is — understand the product boundary.
- Pipeline — follow a formula through its output channels.
- Edit a document — query and mutate the tree, then serialize it.