ReTeX
From the image of a document page to LaTeX that compiles back into the same page. Personal research project, work in progress.
Work in progress · paper in preparation · model not released yet
ReTeX reads the image of a document page and writes LaTeX code that compiles back into that page: the same text, equations, tables and figures, in the same place, with the same fonts and margins. Document conversion becomes an image-to-code problem, and the LaTeX compiler is the judge.
This is a personal research project. The results below come from the current model, evaluated on 400 real arXiv pages from papers submitted between July and September 2026, after every paper the model was trained on.
Why LaTeX
Most document converters output plain text, Markdown or a JSON tree, and lose what makes a page a page: equations stop being math, tables lose their structure and the layout disappears. LaTeX keeps all of it, and it can be compiled. Since the same page can be written in many different ways, ReTeX is judged on what its code produces, not on the code itself.
How it works
- Model. Qwen2.5-VL-3B fine-tuned with LoRA. It writes the whole document, from
\documentclassto\end{document}, in one pass. - Training data. Synthetic pages from a programmatic generator that samples layouts, fonts and content and keeps only what compiles, plus about 59,000 real arXiv pages paired with LaTeX that reproduces them exactly.
- Figures. Photos and plots cannot be written as code, so the model only decides where each figure goes and how big it is.
How it is measured
Both the original page and the model’s output are compiled, and fid_v3, a metric built for this project, compares them: it lists every visible word, image and line on each page, aligns them, and splits the error into content, graphics, geometry, placement and style. A perfect copy scores 1 and a page that does not compile scores 0.
The benchmark holds 400 pages from 400 arXiv papers submitted between July and September 2026, so the model has never seen any of them.
Results
| Page layout | Pages | Compile | Mean fidelity |
|---|---|---|---|
| One column, default margins | 111 | 91 | 0.75 |
| One column, custom geometry | 169 | 141 | 0.77 |
| One column with figures or tables | 47 | 36 | 0.60 |
| Two columns | 73 | 61 | 0.72 |
| All pages | 400 | 329 | 0.74 |
When the output compiles, it is usually very close to the original. Most of the remaining gap comes from pages that do not compile and from figures and floats that land in the wrong place.
Examples
Real outputs of the model on pages from the benchmark, chosen to show what works and what does not. ● good, ● partial, ● failure. All pages come from papers under a CC BY 4.0 licence.
What we learned along the way
- On real pages, the main failure was never finishing the preamble. Real arXiv preambles are long and full of custom macros, and the model learned to keep writing
\newcommandlines until it ran out of tokens. Rewriting the training preambles into a canonical form that renders the same page removed almost all of these loops. - Measure the render, and measure it carefully. An earlier metric gave a blank page 0.47. Replacing it changed which experiments looked like progress.
Next steps
- Placement, especially for figures and two-column pages: the text is usually right, but it does not always land where it should.
- Reinforcement learning with the compiler in the loop, using the fidelity of the compiled page as the reward.
- Multi-page documents.
Demo
A public demo will come with the paper. Until then, the examples above are real, unedited outputs of the model.















