I've looked at this and this and understand there is no grammar of TeX as a whole. I am wondering if there is a grammar just for equations. They are relatively easy to identify in an amsmath file that doesn't mess with \catcode, and I would like to be able to process them automatically. Thanks!

  • 4
    The answer is still no :P Jun 23, 2015 at 13:57

2 Answers 2


See this paper (I think I learned of it from this answer):


While the quality of the results of TeX's mathematical formula layout algorithm is convincing, its original description is hard to understand since it is presented as an imperative program with complex control flow and destructive manipulations of the data structures representing formulae. In this paper, we present a re-implementation of TeX's formula layout algorithm in the functional language SML, thereby providing a more readable description of the algorithm, extracted from the monolithical TeX system.

Review (not sure where this is from or who wrote this, but I think it may be a feature of that journal?):

The TeX typesetting system basically consists of two parts: a macro language and certain basic typesetting capabilities, such as placing boxes next to each other, either vertically or horizontally. However, the real documentation for the typesetting capabilities is the TeXbook and the TeX source code.

This paper is truly welcome. The basic typesetting functions used by TeX should become more clear and should be reachable from a decent declarative language, thereby allowing documents to be typeset or generated in a declarative manner.

As ideas are better and better understood, they are expressed in a more and more declarative manner. The work done in this paper shows how this can be done for a very useful algorithm written in a very unreadable form. This sort of work should be done for other algorithms that are commonly used.

Work of the kind done in this paper allows one to perceive the need for more general means for laying out two-dimensional diagrams, with aesthetic or other constraints defining the actual placement of atomic components to form more complex diagrams.

In the paper, after explaining why they find the TeX program source code hard to read, they extract and reimplement the relevant part:

The main task of the formula layout algorithm consists in translating formula terms (a kind of abstract syntax for formulae) into box terms which describe the sizes and relative positions of the formula constituents in the final layout.

And this is what is implemented. After reading the paper, you can find their full code (2312 lines across 58 .sml files) at the location described in their paper, and also mirrored elsewhere. Note that (as the paper says) it does not implement everything in TeX: even square roots are not implemented.

Otherwise you can look at more recent (and still maintained) programs that implement just the math part of TeX, like MathJax (source), KaTeX (source), iosMath, or its port AndroidMath.


After posting, I discovered the MathJAX JavaScript library for (among other things) rendering TeX equations in MathML. I haven't yet had a chance to investigate in detail, but it handled with aplomb some aligns I had lying around. The code to parse TeX equations is as close to a grammar as anything I've seen.

If you install a local copy, test/sample-dynamic-2.html will render an equation you paste into a text box.

  • 1
    Note that Mathjax is not latex. It is a Javascript lib that can parse a subset of latex. The only thing that can fully parse latex is latex it self
    – daleif
    Jun 29, 2015 at 16:27
  • 1
    @daleif Yes but that's what the question is about: it's about whether it's possible to parse just a restricted subset of (La)TeX math. Sep 22, 2018 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.