Mathematical expression recognition is the task of converting images or pen strokes of mathematical formulas into a structured, machine-readable representation, typically LaTeX, MathML or a symbol layout tree. In Document AI, it extends optical character recognition to content where meaning depends on two-dimensional layout, and it is also known as formula recognition or math OCR.
Ordinary OCR reads text as a one-dimensional sequence of characters. A formula, by contrast, encodes relations through position, size and nesting: a superscript is an exponent, a horizontal bar is a fraction and a radical sign covers its argument. Recognizing mathematics therefore requires both symbol recognition and structural analysis.
How formula recognition works
Mathematical expression recognition traditionally divides into three subproblems: symbol segmentation, symbol classification and structural analysis. Segmentation isolates candidate symbols, classification assigns each a label such as a digit, Greek letter or operator, and structural analysis determines the spatial relations between symbols.
Structural analysis is the hardest part. The system must decide whether a small character to the upper right is a superscript or an adjacent term, whether a dot is a decimal point or a multiplication sign, and which symbols fall under a fraction bar or summation limits. Classic systems used two-dimensional grammars and parsing algorithms to resolve these relations.
The table below shows the main spatial relations a recognizer must detect and how each appears in LaTeX output.
| Relation | Visual cue | LaTeX form |
|---|---|---|
| Superscript | Smaller symbol raised to the right | x^{2} |
| Subscript | Smaller symbol lowered to the right | a_{i} |
| Fraction | Symbols above and below a bar | \frac{a}{b} |
| Radical | Symbols inside a root sign | \sqrt{x} |
| Limits | Symbols above and below an operator | \sum_{i=1}^{n} |
| Matrix | Grid of aligned cells | \begin{pmatrix} … \end{pmatrix} |
Online and offline recognition
Formula recognition comes in two settings that differ in input and difficulty. Online recognition receives pen strokes with timing from a tablet or stylus, while offline recognition receives only a static image from a scan, photograph or PDF rendering.
| Aspect | Online recognition | Offline recognition |
|---|---|---|
| Input | Pen trajectories with timing | Raster image |
| Extra information | Stroke order and direction | Pixel intensity only |
| Typical source | Tablets, digital whiteboards | Scanned papers, textbooks, PDFs |
| Main challenge | Varied writing styles | Noise, blur, touching symbols |
| Common benchmark | CROHME handwritten sets | IM2LATEX-100K rendered formulas |
Printed formulas are considerably easier than handwritten ones because fonts are consistent and symbols rarely touch. Handwritten expressions remain a demanding problem, since writers vary symbol shapes, spacing and baseline alignment, and visually similar symbols such as the digit one, the letter l and a vertical bar are frequently confused.
Deep learning approaches
Modern systems treat mathematical expression recognition as an image-to-sequence problem. A convolutional or vision transformer encoder turns the formula image into feature vectors, and an attention-based decoder generates LaTeX tokens one at a time, learning segmentation and structure implicitly.
Several architectural families have emerged, each with different strengths.
| Approach | How it works | Strength |
|---|---|---|
| Encoder-decoder with attention | Generates LaTeX tokens from image features | Simple, end-to-end training |
| Tree-structured decoders | Predict a symbol layout tree directly | Valid structure by design |
| Graph-based parsers | Classify symbols and relations as a graph | Interpretable relations |
| Page-level transformers | Convert whole pages with formulas to markup | No separate formula detection |
| Vision-language models | General multimodal models prompted for LaTeX | Broad coverage without task training |
Page-level models such as Nougat convert full academic pages, including inline and display formulas, into Markdown with embedded LaTeX. This avoids a separate formula detection step but makes errors harder to localize, and such models can occasionally hallucinate plausible but incorrect notation, especially on low-quality scans.
Evaluating recognition quality
Evaluation is less straightforward than for plain text because different LaTeX strings can render the same formula. For example, extra braces or a different spacing command change the string without changing the mathematics, so naive string comparison underestimates quality.
| Metric | What it measures | Limitation |
|---|---|---|
| Expression recognition rate | Share of formulas recognized exactly | Strict, ignores near misses |
| Token edit distance | Edits needed between LaTeX sequences | Penalizes equivalent notation |
| BLEU score | N-gram overlap of token sequences | Weak link to correctness |
| Image-based match | Similarity of re-rendered output | Ignores semantic differences |
| Symbol layout tree match | Agreement of symbols and relations | Requires tree conversion |
On handwritten benchmarks, exact expression recognition rates for strong systems are commonly reported in the range of roughly 50 to 65 percent, depending on the dataset year and evaluation protocol. Printed formula recognition reaches much higher accuracy, so reported figures must always be read with their dataset and metric.
Applications and limitations in document processing
Within Document AI pipelines, formula recognition is essential wherever documents contain technical content. Key applications include digitizing scientific literature, making textbooks accessible to screen readers through MathML, indexing formulas for mathematical search, grading handwritten homework and converting legacy engineering documents into editable formats.
- Formula detection must precede recognition in most pipelines, and missed inline formulas silently corrupt the extracted text.
- Long or multi-line expressions, such as aligned derivations, degrade accuracy because errors accumulate in sequence decoders.
- Notation ambiguity cannot always be resolved from the image alone, so context or human review is needed for critical content.
- Domain shift between rendered training formulas and real scans remains a major source of errors.
For high-stakes uses, the recommended practice is to render the recognized LaTeX back to an image, compare it visually with the source and route low-confidence formulas to a human verification queue.
In plain words
Ordinary text reads left to right. Math does not. A small number up high means a power. A line with numbers above and below means division. Mathematical expression recognition is software that reads formulas the way a mathematician does.
It looks at a picture of a formula and works out each symbol. Then it works out how the symbols relate. The result is code that computers can edit, search and read aloud. This turns a flat picture into usable math.
A real-world example
A university library is digitizing 12,000 scanned physics theses from the 1970s to the 1990s. Standard OCR extracts the prose well but turns every equation into meaningless character strings. The library adds a pipeline that first detects formula regions, then runs a neural formula recognizer that outputs LaTeX for each region.
Each recognized formula is re-rendered and compared with the original crop, and about 8 percent of formulas fall below a confidence threshold and go to graduate assistants for correction. The finished collection supports searching by equation, so a researcher can find every thesis that uses a particular form of the Schrödinger equation. Screen reader users can also navigate the formulas through MathML, which was impossible with the original scans.