lukas-blecher /LaTeX-OCR
pix2tex: Using a ViT to convert images of equations into LaTeX code.
★ 16,621⑂ 1,301👁 85⑃ 79● 299▤ 7
+0
日增长
16,621
stars
1,301
forks
85
watchers
79
prs
299
issues
7
releases
README
pix2tex - LaTeX OCR
The goal of this project is to create a learning based system that takes an image of a math formula and returns corresponding LaTeX code.
!header
Using the model
To run the model you need Python 3.7+
If you don't have PyTorch installed. Follow their instructions here.
Install the package pix2tex:
pip install "pix2tex[gui]"
Model checkpoints will be downloaded automatically.
There are three ways to get a prediction from an image.
1. You can use the command line tool by calling pix2tex. Here you can parse already existing images from the disk and images in your clipboard.
2. Thanks to @katie-lim, you can use a nice user interface as a quick way to get the model prediction. Just call the GUI with latexocr. From here you can take a screenshot and the predicted latex code is rendered using MathJax and copied to your clipboard.
Under linux, it is possible to use the GUI with gnome-screenshot (which comes with multiple monitor support). For other Wayland compositers, grim and slurp will be used for wlroots-based Wayland compositers and spectacle for KDE Plasma. Note that gnome-screenshot is not compatible with wlroots or Qt based compositers. Since gnome-screenshot will be preferred when available, you may have to set the environment variable SCREENSHOT_TOOL to grim or spectacle in these cases (other available values are gnome-screenshot and pil).
!demo
If the model is unsure about the what's in the image it might output a different prediction every time you click "Retry". With the temperature parameter you can control this behavior (low temperature will produce the same result).
3. You can use an API. This has additional dependencies. Install via pip install -U "pix2tex[api]" and run
bash
python -m pix2tex.api.run
to start a Streamlit demo that connects to the API at port 8502. There is also a docker image available for the API: https://hub.docker.com/r/lukasblecher/pix2tex
docker pull lukasblecher/pix2tex:api
docker run --rm -p 8502:8502 lukasblecher/pix2tex:api
To also run the streamlit demo run
docker run --rm -it -p 8501:8501 --entrypoint python lukasblecher/pix2tex:api pix2tex/api/run.py
and navigate to http://localhost:8501/
4. Use from within Python
python
from PIL import Image
from pix2tex.cli import LatexOCR
img = Image.open('path/to/image.png')
model = LatexOCR()
print(model(img))
The model works best with images of smaller resolution. That's why I added a preprocessing step where another neural network predicts the optimal resolution of the input image. This model will automatically resize the custom image to best resemble the training data and thus increase performance of images found in the wild. Still it's not perfect and might not be able to handle huge images optimally, so don't zoom in all the way before taking a picture.
Always double check the result carefully. You can try to reTop issues
#2Convert to onnx model10 comments · 2021-03-03#248[M1 Mac] Preparing metadata (pyproject.toml) did not run successfully.9 comments · 2023-03-15#216Unicode character supported ?9 comments · 2022-11-22#405Can not open GUI with latexcor8 comments · 2024-11-27#257latexocr error8 comments · 2023-04-15#389Conflinct between libraries PyQt6 and PySide6, when lunching the GUI7 comments · 2024-07-30#330latexocr: DLL load failed while importing QtCore7 comments · 2023-10-20#318Could not find the Qt platform7 comments · 2023-09-19
Discussions
#434Futur integration0 comments · 2025-10-04#432Working with Aerospace on MacOS0 comments · 2025-09-30#429In the browser...0 comments · 2025-08-31#426Diagram representation of the LaTeX-OCR codebase1 comments · 2025-06-18#412Error when running on macOS (M1, Sequoia)1 comments · 2025-01-15#409why this happened?0 comments · 2024-12-26#408弄了一个在线网站可以免费用1 comments · 2024-12-13#388Thank you for providing the tool0 comments · 2024-07-25
Latest release
0.0.31 0.0.31 Pre-release 2023-04-13What's Changed Big change (maybe breaking in your old environment) is moving away from PyQt5 to PyQt6. This will help with installation issues for M1 and M2 based systems. Update GUI to PyQt6 (Fix for M1 and M2 OS X) by @JoepdeJong in https://github.com/lukas-blecher/LaTeX-OCR/pull/236 feat: add utf-8 support for reading README file by @llxlr in https://github.com/lukas-blecher/LaTeX-OCR/pull/201 New Contributors @llxlr made their first contribution in https://github.com/lukas-blecher/LaTeX-OCR/pull/201 @JoepdeJong made their first contribution in https://github.com/lukas-blec