DeTikZify: Synthesizing Graphics Programs for Scientific Figures and Sketches with TikZ

Jonas Belouadi, Simone Paolo Ponzetto, Steffen Eger

Introduction

Creating high-quality scientific figures can be a time-consuming and challenging process. While sketching an idea on a whiteboard or paper is highly intuitive, translating that sketch into a compilable, high-quality graphic requires deep domain knowledge.

To bridge this gap, researchers have introduced DeTikZify, a novel multimodal language model that automatically converts hand-drawn sketches and rasterized scientific figures into editable, semantic TikZ code. By combining a vision-language architecture with a Monte Carlo Tree Search (MCTS) algorithm that allows the model to visually "check its work," DeTikZify vastly outperforms commercial giants like GPT-4V and Claude 3, promising to make the creation of high-quality scientific diagrams significantly easier.

The Challenge of Scientific Figures

Typesetting scientific documents in LaTeX is standard practice across many STEM fields, but creating the accompanying figures using markup languages like TikZ remains notoriously difficult due to a steep learning curve.

Furthermore, researchers often need to recreate existing figures that are stuck in "dumb" raster formats (like PNGs) or low-level vector formats (like SVG). These formats lack semantic, editable structure—they are just collections of pixels or raw coordinates. The authors tackle this gap by building an AI system capable of "reading" a rough sketch or an existing figure and writing the corresponding high-level, semantic TikZ code to reproduce it. Generating high-level TikZ code is far more useful than standard image vectorization because TikZ captures the semantics of a scientific figure (e.g., nodes, loops, mathematical relationships), making it genuinely editable and reusable by humans.

DeTikZify Architecture and Methods

The authors built DeTikZify by pairing a state-of-the-art vision encoder (SigLIP) with code-savvy large language models (like CodeLLaMA and DeepSeek-Coder).

DeTikZify Architecture

To train the model, the researchers utilized a two-step approach:

  1. Pretraining: They pretrained the vision-language connector on figure metadata to help the model understand the visual components of scientific diagrams.
  2. Fine-tuning with Synthetic Sketches: They fine-tuned the LLM on a massive corpus of TikZ code. To teach the model to understand rough human drawings, they dynamically swapped out 50% of the training figures with synthetic sketches generated by a fine-tuned Instruct-Pix2Pix model.

MCTS Inference: The Secret Sauce

Language models are notoriously bad at writing perfectly compiling LaTeX on the first try; standard left-to-right generation often results in hallucinated syntax errors. To solve this, the authors introduce a Monte Carlo Tree Search (MCTS) algorithm tailored specifically for code generation.

MCTS Algorithm

The MCTS algorithm iteratively generates code, compiles it using a background LaTeX engine, and scores the output. The reward signal is based on either compiler diagnostics (e.g., did the code crash?) or SelfSim—a perceptual similarity score where the model uses its own vision encoder to compare its rendered output against the original input image. By wrapping the generation process in this MCTS loop with a visual reward, the model can literally "look" at its mistakes and iteratively refine the code until it matches the target image.

New Datasets

The authors introduce three new datasets to make this research possible:

  • DaTikZv2: The largest dataset of its kind, containing over 360,000 human-created, compilable TikZ graphics scraped from arXiv, TeX StackExchange, and other repositories.
  • SketchFig: A highly curated dataset of 549 real, hand-drawn sketches paired with their final scientific figures, mined from TeX StackExchange.
  • MetaFig: A massive meta-dataset of over 734,000 scientific figures paired with rich text metadata (captions, OCR tokens) used for pretraining the vision-language connector.

Key Results and Insights

DeTikZify demonstrates that a specialized, smaller model can significantly outperform generalized proprietary models when equipped with the right training data and inference algorithms.

  • Massive Outperformance: On a normalized Average Similarity (AVG) metric, the best model (DeTikZify-DS7b) scored 0.965 for both reference figures and sketches. In stark contrast, GPT-4V scored 0.612 on figures and a dismal 0.150 on sketches. Claude 3 performed even worse (0.148 and 0.189).
  • The MCTS Boost: Giving the model a 10-minute computational budget to run MCTS refinement improved perceptual similarity (SelfSim) by 2.2 to 5.6 percentage points and structural similarity (DreamSim) by up to 8.7 points compared to standard single-pass generation.
  • Efficiency: DeTikZify models generated compilable code on the first or second try 82–91% of the time, whereas GPT-4V and Claude 3 only managed 50–62%.
  • Human Evaluation: In blind A/B testing (Best-Worst Scaling), human experts strongly preferred DeTikZify's MCTS-refined outputs over GPT-4V's attempts, noting fewer fidelity errors and better structural interpretation.

Limitations and Future Work

The authors acknowledge that comparing open-source models against proprietary APIs (GPT-4V/Claude) is inherently tricky because the commercial models are black boxes, frequently updated, and might have data leakage. Additionally, due to the high compute cost of LLMs, the authors relied on intuition rather than exhaustive hyperparameter searches. Finally, the model sometimes struggles with rendering highly legible text from rough sketches.

For future work, the authors plan to expand this approach to other graphics languages like MetaPost, PSTricks, or Asymptote. They also want to explore more rigorous reward functions (like point-cloud metrics) and experiment with Direct Preference Optimization (DPO) to further align the model's outputs with human preferences.

Summary updated

BibTeX
@inproceedings{belouadi2024detikzify,
  author = {Jonas Belouadi and Simone Paolo Ponzetto and Steffen Eger},
  title = {{DeTikZify: Synthesizing Graphics Programs for Scientific Figures and Sketches with TikZ}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=bcVLFQCOjc},
}
Figure