Retyping a long equation from a PDF, a lecture slide or a textbook photo into LaTeX is slow and error-prone. pix2tex, also known as LaTeX-OCR, is a free, open-source AI model that looks at an image of a maths formula and returns the LaTeX code for it, running on your own computer rather than a cloud service.
What pix2tex is
pix2tex is a Python package published under the MIT licence. Its README describes the goal as "a learning based system that takes an image of a math formula and returns corresponding LaTeX code". Under the hood it uses a Vision Transformer (ViT) encoder with a ResNet backbone and a Transformer decoder. The latest release on PyPI at the time of writing is version 0.1.4, uploaded on 18 January 2025, so treat it as a mature but quietly maintained tool rather than a fast-moving one.
The project reports a BLEU score of 0.88, a normalised edit distance of 0.10 and token accuracy of 0.60 on its own test data. Those are the developer's figures, not ours, and token accuracy of 0.60 is a good reminder that you'll need to check its output.
What you need
Python 3.7 or newer, which the README lists as the minimum.
PyTorch. If it isn't already installed, the README points you to PyTorch's official install instructions first.
A little disk space for the model checkpoints, which pix2tex downloads automatically the first time it runs.
If you'd rather keep AI tools in Linux on a Windows PC, our guide to setting up WSL2 with GPU support for local AI covers the groundwork.
Step 1: Install pix2tex with the GUI
Open a terminal (ideally inside a Python virtual environment) and run:
pip install "pix2tex[gui]"
The [gui] extra pulls in the dependencies for the screenshot tool described below.
Step 2: Snip an equation with the GUI
Run:
latexocr
This opens a small window. Take a screenshot of the equation you want, and the predicted LaTeX is rendered with MathJax so you can compare it with the original, then copied to your clipboard ready to paste into your LaTeX editor.
On Linux the GUI uses gnome-screenshot where available. The README says users of wlroots-based Wayland compositors can use grim and slurp, and KDE Plasma users spectacle; you may need to set the SCREENSHOT_TOOL environment variable to grim or spectacle if gnome-screenshot is also installed.
If the model is unsure, clicking Retry may give a different answer each time. The temperature setting controls this: a low temperature produces the same result every time.
Step 3: Use the command line for saved images
If you already have image files, run:
pix2tex
The README says the command-line tool can read existing images from disk as well as images in your clipboard.
Step 4: Call it from Python or an API
For scripts, the README's Python example is short:
from PIL import Image
from pix2tex.cli import LatexOCR
img = Image.open('path/to/image.png')
model = LatexOCR()
print(model(img))
There's also an API with a Streamlit demo. Install it with pip install -U "pix2tex[api]" and run python -m pix2tex.api.run; the demo connects to the API on port 8502. A Docker image (lukasblecher/pix2tex:api) is available too.
Tips for better results
Don't zoom in too far. The README says the model works best with smaller images. pix2tex runs a second neural network that predicts the best resolution and resizes your image to resemble its training data, but it may still struggle with very large images.
If it's wrong, try another size. The developer suggests redoing the prediction at a different resolution when the answer is wrong.
Always check the output. The README explicitly says to double-check results carefully. Paste the LaTeX into a previewer and compare it symbol by symbol, especially subscripts and brackets.
Equations, not whole pages. pix2tex is designed for images of individual formulas. For whole PDFs with text and maths, see our comparison of Docling vs Marker.
Can you train it on your own data?
Yes. The README covers installing pix2tex[train], building a dataset from images and their LaTeX labels, editing a config file and running python -m pix2tex.train. Generating training images needs extra tools including XeLaTeX, ImageMagick with Ghostscript, and Node.js for KaTeX. That's well beyond what most people need, but it's useful if you work with unusual notation.