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.

0.74mean page fidelity (0–1), counting pages that fail to compile as 0
0.92median page fidelity
329 / 400outputs compile with no manual fixes
0.94median fidelity of the pages that compile

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

Page imageinput Vision-languagemodel + LoRA LaTeX codeoutput Compilertectonic Rendered pagePDF compared with the input page
  • Model. Qwen2.5-VL-3B fine-tuned with LoRA. It writes the whole document, from \documentclass to \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.

Original page from arXiv 2608.22291
Input page
Page compiled from the LaTeX written by ReTeX
ReTeX output, compiled

fid_v3 = 0.991 · One column, custom geometry. Theorem environments, aligned equations and an inline sum are reproduced almost exactly.

Loss by family:content 0.007graphics 0.000geometry 0.002placement 0.000style 0.000

Source: G. Andrews, M. Ghosh Dastidar, “The Partition Pairing Theorems I”, arXiv:2608.22291, CC BY 4.0. The page was re-rendered from the paper's LaTeX source.

Show the LaTeX written by the model
Loading…

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 \newcommand lines 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.