ConceptsHow TeXForm compares

How TeXForm compares

TeXForm next to LaTeX parsers, normalization scripts, renderers, and CAS tools: a feature matrix for the tools that overlap with it, and when to use which.

Any program that reads LaTeX math as more than a string needs to know what each command takes. \frac12 has two arguments, \binom{n}{k} has two, and \dv{f}{x} has two only when the physics package is in play. Several kinds of tool carry some of that knowledge, and TeXForm overlaps with each of them in a different way.

The landscape#

ToolKindWhat you getOverlap with TeXForm
pylatexenc, unified-latex, TexSoupLaTeX parserA tree of a LaTeX document, and LaTeX back outParsing, editing, serialization
Compute EngineMath engineMathJSON expressions, and LaTeX back outParsing and serialization, as mathematics
SymPy, Math-VerifyMath engineSymPy expressions for checking answersComparing formulas, as mathematics
im2markup normalizerNormalization scriptA token string for recognition labelsNormalization
MATH strip_string, Minerva normalizerNormalization scriptA comparison key for grading answersNormalization
KaTeX, MathJax, TemmlRendererHTML and MathMLKnowing what commands mean
texvcjsValidatorLaTeX checked against a fixed command listKnowing what commands mean

The parsers and Compute Engine are libraries that read LaTeX into a structure and write LaTeX back, which is what TeXForm does too, so the feature comparison below puts them side by side. Normalization scripts, renderers, and math engines solve narrower jobs; the sections after the matrix cover how TeXForm fits next to them.

Feature comparison#

TeXFormpylatexencunified-latexTexSoupCompute Engine
Parsing
Math command signaturesYesKnowledge base, by packagePartlyLaTeX corePartlyCore and selected packagesNoTakes following groupsPartlyMath dictionary
amsmath commands, such as \binomYesNoGroups left as siblingsNoGroups left as siblingsPartlyBy positionPartlyAs mathematics
Package macros, such as \dvYesOpt-in physicsNoGroups left as siblingsNoGroups left as siblingsPartlyBy positionNoError node
Unbraced arguments, \frac12YesYesYesNoText 12Yes
Infix commands, a \over bYesInfix nodeNoPlain commandNoPlain commandNoPlain commandYesAs division
Your own command signaturesYesYesMacro specsYesMacro specsNoNo public APIYesDictionary entries
Errors
Reports unknown commandsYesRejected or markedNoNoNoYesError node
Reports malformed input, \frac{a}{bYesDiagnosticsPartlyWith tolerant parsing offNoYesExceptionYesError node
Output and editing
Writes LaTeX backYesCanonical spacingYesSource textYesLight reformattingYesSource textPartlyNotation may change
Editable treeYesEdits checked against signaturesPartlyWrite-back in 3.0 betaYesunist treeYesInsert, replace, deletePartlyImmutable expressions
Normalization
Folds equivalent spellingsYesFour profilesNoNoNoPartlyMathematical canonical form
Expands package macros to core LaTeXYesNoPartlyDefinitions you supplyNoNo
Scope
Whole LaTeX documentsNoFormulas onlyYesYesYesNoFormulas only
LanguagesRust, Python, JavaScript, CLIPythonJavaScriptPythonJavaScript

Some cells need a qualifier:

  • TeXForm loads a default package set; physics is opt-in because it gives \ket and related commands different meanings than braket does. Strict parsing rejects unknown commands, and the default lenient mode keeps them as nodes marked unknown. Package macros are expanded by the Faithful profile and stronger ones; Authoring keeps them. See Transforms for the profiles.
  • pylatexenc 2.x writes back each node's source text, so changing a node does not change the output; the 3.0 beta adds a recomposer that rebuilds LaTeX from the nodes.
  • unified-latex ships signatures for the LaTeX core and selected packages, including mathtools but not amsmath or physics. Its unified-latex-util-macros package expands macros once you give it their definitions.
  • TexSoup attaches every bracket and brace group that follows a command, apart from a small hard-coded table, so \binom{n}{k} and \dv{f}{x} get two arguments while \frac12 gets none.
  • Compute Engine reads formulas as mathematics: a \over b comes back as \frac{a}{b}, and an aligned environment as a list. Its canonical forms may change between versions.

The number of arguments each parser attached on the inputs above, with groups left as siblings in parentheses:

Inputpylatexencunified-latexTexSoupTeXForm
\dv{f}{x}0 (2 groups)0 (2 groups)22
\frac12220 (text 12)2
\binom{n}{k}0 (2 groups)0 (2 groups)22

When a parser is the right choice. For whole LaTeX documents, with sections, environments, and preambles, use pylatexenc, unified-latex, or TexSoup; TeXForm parses formulas only. unified-latex also fits naturally if your pipeline already lives in the unified.js ecosystem. For formulas, TeXForm gives you the argument structure without writing signatures, and an editable Document whose edits are validated against the same signatures.

Normalization scripts#

Many recognition datasets and metrics normalize labels with a copy of the im2markup preprocessing script, which parses with a vendored KaTeX 0.6.0 and re-serializes the tree as space-separated tokens. Answer-grading code strips and rewrites strings until two answers can be compared: strip_string from the MATH benchmark, the normalization in lm-evaluation-harness adapted from the Minerva paper, and evaluation scripts derived from them.

TeXForm does the same jobs from the parse tree instead of string edits. The Corpus profile produces complete canonical forms intended as training labels, and Equiv folds equivalent spellings into one string for comparison and deduplication. Every rule declares the forms it eliminates, and the engine checks that they are gone. Malformed formulas fail with diagnostics instead of producing a different string, so you can sort a batch into normalized, repairable, and discarded formulas; Clean OCR/MER output shows the workflow. Serialization in either style is text-idempotent: parsing a normalized formula and serializing it again returns the same string.

Inputim2markup normalizerTeXForm Corpus
a \over b\frac { a } { b }\frac { a } { b }
{\bf x} + \mathbf{y}{ \bf x } + \mathbf { y }\mathbf { x } + \mathbf { y }
a \le b \leq ca \le b \leq ca \leq b \leq c

For \dv{f}{x}, the im2markup normalizer keeps \dv { f } { x }, while Corpus expands it into a fraction of differentials. TeXForm's spaced token output matches what tokenized training labels usually expect; if you want compact LaTeX instead, see Configure output style.

When the script is the right choice. If you must reproduce numbers reported on an existing benchmark, use that benchmark's own normalizer, because changing it changes the scores. TeXForm is the choice for new labels and new pipelines.

Renderers and math engines#

Renderers such as KaTeX, MathJax, and Temml know commands well enough to display them, and emit HTML or MathML, not LaTeX. Support for package macros varies: MathJax renders physics macros once its physics extension is loaded, which it is not by default; Temml bundles physics support; with KaTeX you define the macros yourself in its macros option. TeXForm's Faithful profile expands convenience macros into core LaTeX while keeping the rendered formula the same, so \dv{f}{x} becomes \frac { \mathrm { d } f } { \mathrm { d } x } and works on a stock renderer; see Use TeXForm with MathJax or KaTeX. If you control the renderer and only need display, loading the extension is the simpler path.

texvcjs, the checker behind math on Wikipedia, accepts LaTeX from a fixed table of functions and rejects anything else. It suits a site that needs one safe input language; TeXForm's knowledge base is curated per package and you choose which packages to load.

Math engines such as SymPy and Math-Verify read formulas as mathematics: \frac12 becomes the number one half. That is the right tool for checking an answer, and the wrong one for keeping notation. TeXForm does not decide that x + x equals 2x; for mathematical equivalence, normalize with TeXForm first and hand the result to SymPy or Math-Verify. Compare formulas shows the two layers together.

When to use something else#

  • Displaying math: KaTeX, MathJax, or Temml. TeXForm emits LaTeX and never renders.
  • Symbolic math and answer checking: SymPy, Math-Verify, or Compute Engine.
  • Whole documents: LaTeXML, pandoc, or plasTeX convert full LaTeX documents to HTML, XML, or other formats.
  • Formatting .tex source: tex-fmt or latexindent.
  • Converting to Typst or MathML: MiTeX or tex2typst for Typst, Temml or texmath for MathML.

What TeXForm does not cover#

  • Commands outside the knowledge base. The knowledge base is curated per package; browse what it covers in the LaTeX reference. Strict parsing rejects anything else, and lenient parsing keeps it as an opaque node. You can register your own commands; see Inject custom commands and Handle parse errors.
  • Rewrites nobody has written yet. Normalization covers the rules in the rule reference; a command the knowledge base parses is not necessarily rewritten by any profile.
  • Mathematical equivalence, rendering, and documents. These are out of scope by design; What TeXForm is explains why.

Tools compared#

Project pages: pylatexenc, unified-latex, TexSoup, im2markup, MATH, lm-evaluation-harness, SymPy, Math-Verify, Compute Engine, texvcjs, KaTeX, MathJax, Temml, LaTeXML, pandoc, plasTeX, tex-fmt, latexindent, MiTeX, tex2typst, texmath. If a tool has changed since, corrections are welcome: open an issue.

Last updated