Skip to content

Guide · Open source

How to use LaTeX-OCR (pix2tex): install, run and convert formula images

Updated · written with AI from the project's own documentation and checked against our catalogue data

LaTeX-OCR, published as the pix2tex package, takes an image of a math formula and returns the matching LaTeX code so you can paste it into a document. This guide shows you how to install it, run it from the command line, the GUI, the API or Python, and what to check in the output. It is written for readers who are comfortable installing Python packages but are not machine learning specialists.

pix2texlukas-blecher/LaTeX-OCR · ★ 16.6k · MIT

Quick start

  1. 1Install Python and PyTorch. Make sure you have Python 3.7 or newer. Install PyTorch by following the instructions on the PyTorch get-started page.
  2. 2Install pix2tex with GUI support. Run pip install "pix2tex[gui]" in your terminal.
  3. 3Prepare a formula image. Take a photo or screenshot of the formula, cropped closely and not zoomed in too far.
  4. 4Run the prediction. Run the pix2tex command for the command line, or latexocr for the GUI. Model checkpoints download automatically.
  5. 5Check the LaTeX output. Compare the returned LaTeX with the original formula, since the README says to always double check results.
  6. 6Retry if needed. If the answer is wrong, try again, or use a different resolution for the image.

What you need before installing pix2tex

pix2tex requires Python 3.7 or newer. It also needs PyTorch, which you install separately by following the PyTorch instructions for your system. The package is built on a neural network that combines a Vision Transformer encoder with a Transformer decoder.

  • Python 3.7+
  • PyTorch, installed separately using the official PyTorch get-started guide
  • Model checkpoints are downloaded automatically when you first run it

Install pix2tex with GUI support

Install the package with the GUI extra using pip. The README lists this as the install command. Other extras are available: the API dependencies are installed with pip install -U "pix2tex[api]", and training dependencies with pip install "pix2tex[train]".

pip install "pix2tex[gui]"

Get a prediction from the command line

Call the pix2tex command to parse images already on disk, or images in your clipboard. This is the simplest way to convert a formula without writing any code.

  • Run the pix2tex command and follow its prompts for an image file or clipboard input
  • Copy the LaTeX output into your document
pix2tex

Use the desktop GUI to convert a screenshot

The GUI is a quick way to get a prediction. Start it with the latexocr command, take a screenshot of the formula, and the predicted LaTeX is rendered with MathJax and copied to your clipboard. If the output is wrong, click Retry; the result can change between tries when the model is unsure.

  • On Linux, the GUI uses gnome-screenshot when available, with grim and slurp for wlroots-based Wayland compositors and spectacle for KDE Plasma
  • gnome-screenshot is not compatible with wlroots or Qt based compositers, so you may need to set the SCREENSHOT_TOOL environment variable to grim or spectacle
  • Other values for SCREENSHOT_TOOL are gnome-screenshot and pil
latexocr

Use the Streamlit demo or the Docker API image

The API option needs extra dependencies. After installing the api extra, you can start a Streamlit demo that connects to the API on port 8502. The project also publishes a Docker image for the API.

  • The Streamlit demo connects to the API at port 8502
  • A Docker image is available on Docker Hub under lukasblecher/pix2tex
python -m pix2tex.api.run

Call pix2tex from Python

If you want to use the model inside your own code, open the image with Pillow, create a LatexOCR model and call it with the image. The model returns the LaTeX string, which you can print or save.

  • Open the image with PIL.Image.open
  • Create the model with LatexOCR()
  • Pass the image to the model and print the result
from PIL import Image from pix2tex.cli import LatexOCR img = Image.open('path/to/image.png') model = LatexOCR() print(model(img))

Get better results from your images

The model works best with images of smaller resolution. The README says a preprocessing step resizes your image to better match the training data, but it is not perfect and may not handle huge images well. Do not zoom in all the way before taking a picture. If the answer is wrong, try the prediction again with a different resolution.

Always double check the result carefully. The README says the output must be checked, and it reports a token accuracy of 0.60.

  • Avoid very large or heavily zoomed images
  • Crop the image to the formula alone
  • Compare the LaTeX output with the original formula before you use it

What people use it for

  • Turn a handwritten or printed formula photo into editable LaTeX for a homework write-up.
  • Copy a formula from a PDF screenshot into a LaTeX document.
  • Call a formula-to-LaTeX model from a Python script to process a folder of formula images.
  • Run a quick local tool on your own computer, without sending the image to a web service.

Frequently asked questions

Is LaTeX-OCR free to use?

pix2tex is released under the MIT licence, according to the project page. Check the licence file in the repository for the full terms before you use it in a commercial product.

What do I need to install LaTeX-OCR?

You need Python 3.7 or newer and PyTorch, which you install separately. The package itself installs with pip install "pix2tex[gui]".

How accurate is pix2tex?

The README reports a BLEU score of 0.88, a normed edit distance of 0.10 and a token accuracy of 0.60. The author says to always double check results, so treat the output as a draft.

Why does pix2tex give a different answer each time I click Retry?

When the model is unsure what is in the image, it can output a different prediction each time. The temperature parameter controls this, and a low temperature produces the same result.

Can pix2tex convert a whole PDF or document?

No. The project targets images of single math formulas, and the README does not describe whole-document conversion.

What are the alternatives to LaTeX-OCR?

Comparable projects named in this context include Mathpix, Nougat and im2markup. Mathpix is a commercial service, while the others are open-source projects, so compare them on your own formula images.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Use pix2tex if you want a free, local tool to turn single formula images into LaTeX and you are comfortable installing Python and PyTorch. Pick another tool if you need guaranteed-correct output, must convert whole documents, or work with very large images, since the README warns about all three.

Install options & downloads