TL;DR
Turning a messy whiteboard sketch into a professional LaTeX diagram usually involves a tedious hour of wrestling with TikZ coordinates and syntax. Sketch2TikZ changes the game by using Reinforcement Learning to teach AI models how to "see" a hand-drawn sketch and write the executable code to recreate it perfectly. By using a clever "curriculum" of rewards, the system achieves near-perfect code compilation and visual accuracy, even outperforming some of the world's most famous proprietary AI models.
What Problem Does This Paper Address?
Scientific diagrams are the lifeblood of technical communication, but authoring them in TikZ—the gold standard for LaTeX graphics—is notoriously difficult. It’s a steep learning curve where a single misplaced semicolon can lead to a compilation nightmare.
While we’ve seen AI models attempt to bridge this gap before, they usually struggle with "noisy" inputs. A hand-drawn sketch is inherently messy: lines overlap, handwriting is shaky, and spatial relationships are imprecise. Traditional AI training (Supervised Fine-Tuning) tries to get the model to predict the next "token" of code correctly. However, in the world of programming, "almost correct" code is often just "broken" code. The authors identify a critical gap: we need models that don't just mimic code, but understand the global validity of the diagram—ensuring it compiles, the text is accurate, and the geometry matches the original intent.
Methods
The authors propose a framework called Sketch2TikZ. At its heart is a Vision-Language Model (VLM) that combines a visual encoder (SigLIP) with a lightweight "reasoning" engine (either TinyLlama-1.1B or DeepSeek-Coder-1.3B).
The real innovation, however, lies in how they train this model using Group Relative Policy Optimization (GRPO). Instead of just telling the model "this is the right code," they let the model generate several candidates and then grade them using a Sequential Multi-Reward Strategy.
They use four distinct "teachers" (reward functions):
- Code-Structure ($\mathcal{R}_{cs}$): A binary check—does the code actually compile into a PDF?
- OCR Reward ($\mathcal{R}_{ocr}$): Does the text in the rendered diagram match the text in the sketch, and is it in the right place?
- CleanDIFT ($\mathcal{R}_{cd}$): A geometric check that ensures the shapes and lines are spatially aligned with the sketch.
- Self-Similarity ($\mathcal{R}_{ss}$): A high-level "vibe check" using vision foundation models to ensure the topological structure is preserved.
Crucially, they use Curriculum-Based Training. They don't overwhelm the model with all rewards at once. They start by teaching it how to write valid, compilable code (Structure), and then progressively introduce finer rewards for visual and textual accuracy.
Datasets
The study utilizes two primary benchmarks:
- SkeTikZ: A curated dataset of 3,231 pairs of hand-drawn sketches and their corresponding TikZ code, covering flowcharts, neural architectures, and block diagrams.
- FlowChart-3B: A specialized dataset used for "zero-shot" evaluation. This tests how well the model generalizes to new, unseen flowchart styles without any specific training on them.
Key Insights and Ideas
- The "Compilation Cliff": In natural language, a small mistake makes a sentence slightly awkward. In TikZ, a small mistake makes the code useless. The authors found that optimizing for "compilation success" first provides a stable foundation for the model to then learn visual nuances.
- Reward Interference is Real: If you try to optimize for "looks like the sketch" and "compiles perfectly" at the same time from day one, the gradients (the signals telling the model how to improve) can conflict. The curriculum approach—learning to walk (compile) before you run (perfect alignment)—is the secret to their stability.
- Small Models, Big Results: By using specialized RL, their 1.3B parameter models (which can run on modest hardware) managed to beat or match the performance of massive proprietary models like GPT-4o-mini.
Key Results
The quantitative results are impressive:
- Near-Perfect Compilation: Their best model achieved a 99.88% Compilation Success Rate (CSR) on the SkeTikZ dataset.
- Visual Fidelity: The model significantly improved Image Similarity scores over previous open-source baselines (like DeTikZify).
- Outperforming the Giants: In zero-shot tests on the FlowChart-3B dataset, Sketch2TikZ outperformed GPT-4o-mini in both compilation success (94.5% vs 87.1%) and visual similarity.
- Ablation Proof: Their tests showed that without the curriculum-based scheduling, the model was much more likely to "over-optimize" for one reward while failing at others.
Limitations and Future Work
While Sketch2TikZ is a major step forward, the authors note a few areas for growth:
- Scaling: The current framework uses relatively small models (1.1B - 1.3B). Scaling this to larger "frontier" models could unlock even more complex diagramming capabilities.
- Compositional Reasoning: Extremely dense or nested diagrams still pose a challenge for the model's spatial reasoning.
- Generalization: While it handles flowcharts and block diagrams well, moving into more artistic or highly specialized scientific domains (like circuit diagrams or chemical structures) remains a future frontier.
Summary updated