Install LaTeX-OCR recognition tool on M1

date
Feb 25, 2023
slug
install-latex-ocr-on-apple-silicon-m1
status
Published
summary
LaTeX-OCR is a well-known, open-source, and free OCR recognition tool that is widely used as an alternative to Mathpix. However, due to compatibility issues with cross-platform modules such as pytorch and qt, installing this tool on an arm64 host requires certain operations.
tags
Engineering
AI
Data Analysis
Python
Productivity
type
Post
LaTeX-OCR is a well-known, open-source, and free OCR recognition tool that is widely used as an alternative to Mathpix.
However, due to compatibility issues with cross-platform modules such as pytorch and qt, installing this tool on an arm64 host requires certain operations.

Solution

  • Install compiled qt library through brew
  • Install pytorch through conda

Steps in details

  1. pip install "pix2tex[gui]" the module
  1. brew install pyqt@5 the GUI dependency
  1. sudo cp -r /opt/homebrew/Cellar/pyqt@5/5.15.7_2/lib/python3.9/site-packages/* /Users/rey/miniconda3/lib/python3.9/site-packages/ * since you cannot find a compatible version on the official forge of pip or conda
  1. pip install pynput screeninfo required by the package
  1. conda install pytorch torchvision conda will try to install a better version of pytorch on arm 64

Usage

  1. Start python -m pix2tex latexocr
  1. Screenshot the LaTeX image to be recognized to the clipboard
  1. Wait for the terminal to display the recognized text, and the LaTeX text has been automatically copied to the clipboard.

Recognition effect

For the chained probability formula in the Stanford CS324 Large Language Model lecture notes:
notion image
Recognized as LaTeX code:
{\\mathbf{p}}(\\mathbf{x}*{1,\\mathrm{L}})=\\mathbf{p}(\\mathbf{x}*{1})\\mathbf{p}(\\mathbf{x}*{2}\\mid\\mathbf{x}*{1})\\mathbf{p}(\\mathbf{x}*{3}\\mid\\mathbf{x}*{1},\\mathbf{x}*{2})\\cdots\\mathbf{p}(\\mathbf{x}*{\\mathrm{L}}\\mid\\mathbf{x}*{1,\\mathrm{L}-1})=\\prod*{\\mathrm{i=1}}^{\\mathrm{L}}\\mathbf{p}(\\mathbf{x}*{\\mathrm{i}}\\mid\\mathbf{x}*{1,\\mathrm{i,t-1}).
Rendered equivalent to:
notion image
You can see that there are some small errors, but it can save a lot of time.

Reference

[2] Official installation tutorial: https://github.com/lukas-blecher/LaTeX-OCR


© Rongxin 2021 - 2024