Generating realistic handwritten mathematical expressions is a notoriously difficult task in computer vision. Unlike standard text, math is inherently two-dimensional, featuring complex nested structures like fractions, exponents, and subscripts.
In a new paper, researchers from the South China University of Technology and Huawei Technologies introduce DiffMath, a latent diffusion framework that tackles these challenges end-to-end, producing high-fidelity handwritten math without requiring expensive symbol-level bounding box annotations.
TL;DR
DiffMath is an end-to-end latent diffusion transformer that generates realistic handwritten mathematical expressions from LaTeX strings. By leveraging a novel tree-based structural representation (RelAST) and a structure-aware latent space, it achieves state-of-the-art performance and serves as a powerful data engine for training math OCR models.
What Problem Does This Paper Address?
Handwritten Mathematical Expression Generation (HMEG) faces two major hurdles:
- Complex 2D Topology: Mathematical formulas aren't linear. A single LaTeX string might represent a deeply nested fraction or a multi-level superscript, requiring the model to understand long-range structural dependencies.
- Supervision Costs: Most existing high-performing methods rely on a two-stage process: first generating individual symbols and then predicting their spatial layout. This requires "dense supervision"—meaning every symbol in the training data must be labeled with a bounding box. This is incredibly expensive to annotate and limits the ability to scale models to diverse, real-world math.
Methods
The authors propose a three-part framework to move beyond the need for positional supervision:
1. Relational Abstract Syntax Tree (RelAST)
Standard LaTeX can be ambiguous, and raw MathML trees contain redundant "container" nodes that don't correspond to actual ink. The authors designed RelAST, which distills math structures into compact sequences of triplets: [Symbol, Relation, Depth].
- Symbol (S): The identity of the character (e.g., 'x', '+', '2').
- Relation (R): The spatial relationship to the previous symbol (e.g.,
RIGHT,ABOVE,SUB). - Depth (D): The nesting level in the hierarchy.
This "prescriptive" prior allows a transformer to process the 2D graph of a formula as a structured 1D sequence.
2. MathVAE: A Structure-Aware Latent Space
Instead of generating pixels directly, DiffMath operates in a compressed latent space. The authors trained a Variational Autoencoder (MathVAE) with a twist: they added "perceptual regularization." By forcing the VAE's encoder to also perform a recognition task (predicting the symbols and their relations), they ensured the latent space captures the underlying math semantics and topology, not just the visual appearance of the ink.
3. MathDiT: The Diffusion Transformer
The core of the system is MathDiT, a transformer-based denoiser. It takes noisy latent representations and reconstructs the "clean" formula, conditioned on the RelAST tokens. To prevent the model from omitting or repeating symbols in long formulas, they introduced a global symbol-count prior injected via Adaptive Layer Normalization (AdaLN).
Datasets
The model was primarily trained and evaluated on:
- MathWriting: A large-scale dataset containing 220k authentic human-written samples.
- CROHME (2014, 2016, 2019): Standard benchmarks for mathematical expression recognition, used here to evaluate the effectiveness of DiffMath-generated synthetic data for downstream OCR tasks.
Key Insights and Ideas
- Prescriptive vs. Descriptive Priors: The shift from descriptive MathML to prescriptive RelAST is crucial. By stripping away non-visible nodes and explicitly encoding relations, the model focuses its "attention budget" on the elements that actually appear in the final image.
- Organizing the Latent Manifold: The researchers found that a standard VAE latent space is too "messy" for complex math. By adding symbol- and relation-aware losses during VAE training, they created a more organized manifold that makes the diffusion model's job significantly easier.
- Complexity-Sensitive Modulation: The global symbol count prior is modulated by time. Its influence is weak during early denoising stages (when the model is establishing the general layout) and becomes stronger during later refinement stages to ensure every symbol is accounted for.
Key Results
DiffMath outperformed both specialized HMEG models and general-purpose text-to-image (T2I) models:
- Accuracy: On the MathWriting dataset, DiffMath achieved an Expression Recognition Rate (ExpRate) of 70.70%, significantly higher than the previous SOTA, FormulaGAN (65.86%), and the unstructured diffusion model DiffInk (58.35%).
- Realism: It achieved the lowest FID score (5.43), indicating its generated samples are statistically closest to real human handwriting.
- OCR Boost: Using DiffMath to generate 150k synthetic samples for training improved the accuracy of a state-of-the-art OCR model (Uni-MuMER) by 2.84% on the MathWriting test set.
Limitations and Future Work
While DiffMath is a major step forward, the authors note some remaining challenges:
- Rare Symbols: The model still struggles with very rare mathematical symbols that appear infrequently in the training set.
- Dense/Deep Nesting: In extremely complex formulas (e.g., matrices within fractions), the model can occasionally misplace small components or confuse similar-looking symbols.
- Future Directions: The authors suggest exploring more advanced topological modeling and scaling the framework to even larger, more diverse datasets to improve robustness for edge-case mathematical notations.
Summary updated