### Setup and Installation for LaTeX OCR
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_test.ipynb
This Python code snippet sets up the environment for the LaTeX OCR project. It installs necessary libraries like Pillow, pix2tex, and opencv-python-headless. It also includes logic to handle potential Pillow version issues and initializes the LaTeXOCR model.
```python
#@title Setup
%reload_ext autoreload
%autoreload
import PIL
!pip install Pillow -U -qq
if int(PIL.__version__[0]) < 9:
print('Mandatory restart: Execute this cell again!')
import os
os.kill(os.getpid(), 9)
!pip install pix2tex -qq
!pip install opencv-python-headless==4.1.2.30 -U -qq
def upload_files():
from google.colab import files
from io import BytesIO
uploaded = files.upload()
return [(name, BytesIO(b)) for name, b in uploaded.items()]
from pix2tex import cli as pix2tex
from PIL import Image
model = pix2tex.LatexOCR()
from IPython.display import HTML, Math
display(HTML(""))
table = r'\begin{array} {l|l} %s \end{array}'
```
--------------------------------
### Run pix2tex API with Docker
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/installation.md
Pulls the pix2tex API Docker image and runs it, exposing the API on port 8502. This allows the API to be used from a Docker container.
```bash
docker pull lukasblecher/pix2tex:api
docker run -p 8502:8502 lukasblecher/pix2tex:api
```
--------------------------------
### Run pix2tex Streamlit Demo with Docker
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/installation.md
Runs the Streamlit demo for pix2tex from a Docker container, exposing the demo on port 8501. Users can then access the demo at http://localhost:8501/.
```bash
docker run -it -p 8501:8501 --entrypoint python lukasblecher/pix2tex:api pix2tex/api/run.py
```
--------------------------------
### Install pix2tex Python Package
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/installation.md
Installs the pix2tex Python package using pip. The `[gui]` tag installs GUI dependencies. Model checkpoints are downloaded automatically on first run. Other dependency tags include `[train]`, `[api]`, and `[all]`.
```bash
pip install pix2tex[gui]
```
--------------------------------
### KaTeX Rendering Options Example (JavaScript)
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/pix2tex/dataset/preprocessing/third_party/katex/README.md
This JavaScript example illustrates how to pass rendering options to `katex.render`. It demonstrates setting `displayMode` to `true` for block math and `throwOnError` to `false` to customize error handling. Other options like `errorColor` can also be provided.
```javascript
katex.render("c = \pm\sqrt{a^2 + b^2}", element, { displayMode: true });
```
--------------------------------
### Install pix2tex for Training
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Installs the pix2tex library with training dependencies. This is a prerequisite for all subsequent training steps.
```python
!pip install pix2tex[train] -qq
```
--------------------------------
### Install Wandb (Optional)
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Installs the Weights & Biases (wandb) library for experiment tracking. This is an optional step and can be skipped if wandb is not desired or if a W&B account is not available. It also includes a commented-out command for logging into wandb.
```python
# If using wandb
!pip install -q wandb
# you can cancel this if you don't wan't to use it or don't have a W&B acc.
#!wandb login
```
--------------------------------
### Setup Project Directory
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Creates a 'LaTeX-OCR' directory and changes the current working directory to it. This organizes project files and ensures subsequent commands operate within the correct context.
```python
import os
!mkdir -p LaTeX-OCR
os.chdir('LaTeX-OCR')
```
--------------------------------
### Install Additional Dependencies
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Installs necessary libraries such as gpustat for GPU monitoring, opencv-python-headless for image processing, and gdown for downloading files from Google Drive. These are essential for data handling and GPU utilization.
```python
!pip install gpustat -q
!pip install opencv-python-headless==4.1.2.30 -U -q
!pip install --upgrade --no-cache-dir gdown -q
```
--------------------------------
### Run pix2tex API with Docker
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/README.md
Starts the pix2tex API service using a Docker container. This command maps the container's port 8502 to the host machine's port 8502, making the API accessible externally. Ensure Docker is installed and running.
```bash
docker pull lukasblecher/pix2tex:api
docker run --rm -p 8502:8502 lukasblecher/pix2tex:api
```
--------------------------------
### Install pix2tex Packages
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Installs the pix2tex package with different optional dependencies for CLI, GUI, API, or training capabilities. Use 'pip install pix2tex' for basic installation.
```bash
# Basic installation for CLI/Python usage
pip install pix2tex
# With GUI support (PyQt6)
pip install "pix2tex[gui]"
# With API support (FastAPI + Streamlit)
pip install "pix2tex[api]"
# With training dependencies
pip install "pix2tex[train]"
# Install all extras
pip install "pix2tex[all]"
```
--------------------------------
### Install pix2tex with GUI
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/README.md
Installs the pix2tex package with support for its graphical user interface. This command requires Python 3.7 or higher and assumes PyTorch is already installed.
```bash
pip install "pix2tex[gui]"
```
--------------------------------
### Training Configuration - YAML
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Example YAML configuration file for training. It specifies dataset paths, model architecture parameters, training hyperparameters, and logging settings.
```yaml
# Example config.yaml
name: my_latex_ocr
data: train_dataset.pkl
valdata: val_dataset.pkl
tokenizer: dataset/tokenizer.json
# Model architecture
encoder_structure: hybrid
num_tokens: 8000
max_width: 1024
max_height: 512
min_width: 32
min_height: 32
channels: 1
max_seq_len: 1024
# Training parameters
epochs: 50
batchsize: 16
testbatchsize: 8
micro_batchsize: 8
lr: 0.001
optimizer: AdamW
betas: [0.9, 0.999]
scheduler: StepLR
lr_step: 30
gamma: 0.1
seed: 42
# Logging
sample_freq: 1000
save_freq: 5
valbatches: 100
test_samples: 5
wandb: true
```
--------------------------------
### Start Model Training
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Initiates the training process for the LaTeX OCR model using the generated 'colab.yaml' configuration file. This command executes the training script provided by the pix2tex library.
```python
!python -m pix2tex.train --config colab.yaml
```
--------------------------------
### Evaluate pix2tex from Command Line
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
This snippet demonstrates how to evaluate the pix2tex model from the command line using specific configuration and checkpoint files. It requires Python and the pix2tex library to be installed.
```bash
python -m pix2tex.eval \
--config config.yaml \
--checkpoint checkpoints/weights.pth \
--data test_dataset.pkl \
--batchsize 10 \
--temperature 0.333 \
--num-batches 100
```
--------------------------------
### Download Pretrained Weights
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Downloads the 'weights.pth' file from a GitHub release using curl. These weights are intended for fine-tuning the OCR model, providing a starting point for the training process.
```python
# download the weights we want to fine tune
!curl -L -o weights.pth https://github.com/lukas-blecher/LaTeX-OCR/releases/download/v0.0.1/weights.pth
```
--------------------------------
### Get All ArXiv IDs
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Extracts all arXiv identifiers present within a given string of text.
```APIDOC
## Function: get_all_arxiv_ids
### Description
Retrieves all arXiv identifiers found within a given text string.
### Method
`pix2tex.dataset.arxiv.get_all_arxiv_ids`
### Parameters
#### Arguments
- **text** (str) - The input string to search for arXiv IDs.
### Request Example
```python
from pix2tex.dataset.arxiv import get_all_arxiv_ids
text_with_ids = "This paper is available at https://arxiv.org/abs/1234.5678 and also https://arxiv.org/abs/9876.5432."
arxiv_ids = get_all_arxiv_ids(text_with_ids)
print(arxiv_ids) # Output: ['1234.5678', '9876.5432']
```
### Response
#### Success Response
- **List[str]**: A list containing all extracted arXiv IDs found in the text.
#### Response Example
```json
{
"status": "success",
"arxiv_ids": ["1234.5678", "9876.5432"]
}
```
```
--------------------------------
### Check GPU Status
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Uses the gpustat command to display information about the available GPUs. This helps in verifying GPU availability and status before starting resource-intensive training.
```python
# check what GPU we have
!gpustat
```
--------------------------------
### Recursive Search for Math
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Recursively searches for mathematical expressions starting from a set of seed URLs or identifiers.
```APIDOC
## Function: recursive_search
### Description
Performs a recursive search for mathematical expressions starting from a list of seed URLs or identifiers. It explores linked pages up to a specified depth, parsing content using a provided parser function.
### Method
`pix2tex.dataset.scraping.recursive_search`
### Parameters
#### Arguments
- **parser** (Callable) - A function that takes a page's content and returns a tuple of (list of math expressions, list of identifiers for next level).
- **seeds** (List[str]) - The initial list of URLs or identifiers to start the search from.
- **depth** (int, optional) - The maximum depth of recursion for exploring links. Defaults to 2.
- **skip** (List[str], optional) - A list of already visited identifiers to avoid redundant processing. Defaults to [].
- **unit** (str, optional) - Description for the progress bar unit (e.g., 'links'). Defaults to 'links'.
- **base_url** (str, optional) - The base URL to prepend to relative identifiers. Defaults to None.
- **kwargs** - Additional keyword arguments to pass to the parser function.
### Request Example
```python
# This is a conceptual example as the 'parser' function needs to be defined.
# from pix2tex.dataset.scraping import recursive_search
# def dummy_parser(url):
# # Dummy parser logic
# math_found = ["$E=mc^2$"]
# next_links = [url + "/page2"]
# return math_found, next_links
# seeds = ["http://example.com/start"]
# found_math, visited_ids = recursive_search(dummy_parser, seeds, depth=3)
# print(f"Found math: {found_math}")
# print(f"Visited IDs: {visited_ids}")
```
### Response
#### Success Response
- **Tuple[List[str], List[str]]**: A tuple containing:
- A list of all mathematical expressions found during the recursive search.
- A list of all unique identifiers (e.g., URLs) visited during the search.
#### Response Example
```json
{
"status": "success",
"found_math": ["$E=mc^2$", "\\int x dx"],
"visited_ids": ["http://example.com/start", "http://example.com/start/page2"]
}
```
```
--------------------------------
### REST API - /predict/
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Endpoint for uploading an image file to get LaTeX OCR predictions.
```APIDOC
## REST API - /predict/
### Description
This endpoint allows you to upload an image file via HTTP POST request and receive the predicted LaTeX code as a string.
### Method
`POST`
### Endpoint
`/predict/`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **file** (multipart/form-data) - Required - The image file to process.
### Request Example
```bash
curl -X POST "http://localhost:8502/predict/" -F "file=@equation.png"
```
### Response
#### Success Response (200 OK)
- **string**: The predicted LaTeX code.
#### Response Example
```
\frac{1}{2} \sum_{i=1}^{n} x_i^2
```
#### Error Response (e.g., 400 Bad Request)
- **detail**: Error message describing the issue (e.g., 'Invalid file format').
```
--------------------------------
### REST API - /bytes/
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Endpoint for sending image data as bytes to get LaTeX OCR predictions.
```APIDOC
## REST API - /bytes/
### Description
This endpoint accepts raw image bytes via HTTP POST request and returns the predicted LaTeX code. It bypasses the need for multipart/form-data encoding.
### Method
`POST`
### Endpoint
`/bytes/`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **file** (bytes) - Required - The raw image data.
### Request Example
```bash
curl -X POST --data-binary @equation.png "http://localhost:8502/bytes/"
```
### Response
#### Success Response (200 OK)
- **string**: The predicted LaTeX code.
#### Response Example
```
\frac{1}{2} \sum_{i=1}^{n} x_i^2
```
#### Error Response (e.g., 400 Bad Request)
- **detail**: Error message describing the issue (e.g., 'Could not decode image').
```
--------------------------------
### LatexOCR Initialization
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Initializes the LatexOCR model for image-to-LaTeX prediction. Allows for custom model parameters.
```APIDOC
## Class: LatexOCR
### Description
Initializes a LatexOCR model for image-to-LaTeX prediction.
### Method
`__init__`
### Parameters
#### Arguments
- **arguments** (Union[Namespace, Munch], optional) - Special model parameters. Defaults to None.
### Request Example
```python
from pix2tex.cli import LatexOCR
# Initialize with default arguments
ocr = LatexOCR()
# Initialize with custom arguments (example)
# from argparse import Namespace
# args = Namespace(argument1='value1')
# ocr = LatexOCR(arguments=args)
```
### Response
#### Success Response
- **LatexOCR instance**: An initialized LatexOCR object.
#### Response Example
```json
{
"status": "success",
"message": "LatexOCR model initialized successfully."
}
```
```
--------------------------------
### GUI Application: Launching and Features
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Describes how to launch the PyQt6-based graphical user interface for pix2tex and lists its key features. The GUI supports screenshot capture, MathJax rendering, drag-and-drop, and clipboard pasting.
```python
from pix2tex.gui import main, App
from munch import Munch
# Launch GUI with default settings
arguments = Munch({
'config': 'settings/config.yaml',
'checkpoint': 'checkpoints/weights.pth',
'no_cuda': False,
'no_resize': False,
'temperature': 0.333
})
main(arguments)
# Or run from command line
# latexocr
# pix2tex --gui
# GUI Features:
# - Alt+S (Option+S on macOS): Take screenshot
# - Ctrl+V: Paste image from clipboard
# - Drag and drop: Load image files
# - Format options: Raw, LaTeX-$, LaTeX-$$, Sympy
# - Temperature slider for sampling control
# - Retry button for different predictions
```
--------------------------------
### Recursive Wikipedia Search
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Recursively searches Wikipedia for mathematical content starting from specified article URLs.
```APIDOC
## Function: recursive_wiki
### Description
Recursively searches Wikipedia articles for mathematical content. It starts from a given set of article URLs and explores linked articles up to a specified depth, prioritizing pages with mathematical expressions.
### Method
`pix2tex.dataset.scraping.recursive_wiki`
### Parameters
#### Arguments
- **seeds** (List[str]) - A list of starting Wikipedia article URLs.
- **depth** (int, optional) - The maximum depth of recursion for exploring linked articles. Defaults to 4.
- **skip** (List[str], optional) - A list of already visited article URLs to avoid redundant processing. Defaults to [].
- **base_url** (str, optional) - The base URL for Wikipedia articles. Defaults to 'https://en.wikipedia.org/wiki/'.
### Request Example
```python
# This function likely uses recursive_search internally with a specific parser.
# from pix2tex.dataset.scraping import recursive_wiki
# seed_articles = [
# "https://en.wikipedia.org/wiki/Calculus",
# "https://en.wikipedia.org/wiki/Linear_algebra"
# ]
# found_math, visited_urls = recursive_wiki(seed_articles, depth=2)
# print(f"Found math on Wikipedia: {found_math}")
```
### Response
#### Success Response
- **Tuple[List[str], List[str]]**: A tuple containing:
- A list of mathematical expressions found on Wikipedia pages.
- A list of visited Wikipedia article URLs.
#### Response Example
```json
{
"status": "success",
"found_math": ["\\int_a^b f(x) dx = F(b) - F(a)"],
"visited_urls": ["https://en.wikipedia.org/wiki/Calculus", ...]
}
```
```
--------------------------------
### Run Streamlit Frontend with Docker
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Launches the Streamlit-based frontend for the pix2tex API using Docker. This provides a web interface for interacting with the OCR model.
```bash
docker run --rm -it -p 8501:8501 --entrypoint python lukasblecher/pix2tex:api pix2tex/api/run.py
# Navigate to http://localhost:8501/
```
--------------------------------
### Training Execution - Command Line
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Commands for running the training script from the command line. Includes options for specifying configuration files, disabling CUDA, enabling debug mode, and resuming training.
```bash
# Run training
python -m pix2tex.train --config config.yaml
# Train without GPU
python -m pix2tex.train --config config.yaml --no_cuda
# Debug mode
python -m pix2tex.train --config config.yaml --debug
# Resume training
python -m pix2tex.train --config config.yaml --resume
```
--------------------------------
### Recursive Stack Exchange Search
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Recursively searches Stack Exchange for mathematical content starting from specified question URLs.
```APIDOC
## Function: recursive_stack_exchange
### Description
Recursively searches through Stack Exchange questions for mathematical content. It starts from a given set of question URLs and explores linked questions up to a specified depth.
### Method
`pix2tex.dataset.scraping.recursive_stack_exchange`
### Parameters
#### Arguments
- **seeds** (List[str]) - A list of starting Stack Exchange question URLs.
- **depth** (int, optional) - The maximum depth of recursion for exploring linked questions. Defaults to 4.
- **skip** (List[str], optional) - A list of already visited question URLs to avoid redundant processing. Defaults to [].
- **base_url** (str, optional) - The base URL for Stack Exchange questions. Defaults to 'https://math.stackexchange.com/questions/'.
### Request Example
```python
# This function likely uses recursive_search internally with a specific parser.
# from pix2tex.dataset.scraping import recursive_stack_exchange
# seed_questions = [
# "https://math.stackexchange.com/questions/12345/example-question",
# "https://math.stackexchange.com/questions/67890/another-question"
# ]
# found_math, visited_urls = recursive_stack_exchange(seed_questions, depth=3)
# print(f"Found math on Stack Exchange: {found_math}")
```
### Response
#### Success Response
- **Tuple[List[str], List[str]]**: A tuple containing:
- A list of mathematical expressions found on Stack Exchange pages.
- A list of visited Stack Exchange question URLs.
#### Response Example
```json
{
"status": "success",
"found_math": ["\\int_0^\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}"],
"visited_urls": ["https://math.stackexchange.com/questions/12345/example-question", ...]
}
```
```
--------------------------------
### Include KaTeX CSS and JS Files from CDN
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/pix2tex/dataset/preprocessing/third_party/katex/README.md
This snippet shows how to include the KaTeX CSS and JavaScript files directly from a CDN. This is a common way to integrate KaTeX into a web page without self-hosting.
```html
```
--------------------------------
### Health Check API Endpoint
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Checks the health status of the running API. It sends a GET request to the root endpoint and expects a JSON response indicating the status.
```bash
curl http://localhost:8502/
# Response: {"message":"OK","status-code":200,"data":{}}
```
--------------------------------
### Run with Streamlit Frontend
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Instructions for running the API with a Streamlit frontend.
```APIDOC
## Streamlit Frontend
### Description
Instructions for running the API with its associated Streamlit frontend.
### Method
N/A
### Endpoint
N/A
### Parameters
None
### Request Example
```bash
# Run with Streamlit frontend
docker run --rm -it -p 8501:8501 --entrypoint python lukasblecher/pix2tex:api pix2tex/api/run.py
# Navigate to the frontend in your browser
# http://localhost:8501/
```
### Response
N/A
```
--------------------------------
### Docker Usage
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Instructions for running the Latex-OCR API using Docker.
```APIDOC
## Docker
### Description
Instructions for pulling the Docker image and running the API container.
### Method
N/A
### Endpoint
N/A
### Parameters
None
### Request Example
```bash
# Pull the Docker image
docker pull lukasblecher/pix2tex:api
# Run the API container
docker run --rm -p 8502:8502 lukasblecher/pix2tex:api
```
### Response
N/A
```
--------------------------------
### Dataset Generation from Command Line
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Command-line interface for generating datasets and tokenizers. Allows specifying equation files, image directories, tokenizer output, and vocabulary size.
```bash
# Generate dataset from command line
python -m pix2tex.dataset.dataset \
--equations path/to/math.txt \
--images path/to/images/ \
--tokenizer tokenizer.json \
--out dataset.pkl
# Generate tokenizer only
python -m pix2tex.dataset.dataset \
--equations path/to/math.txt \
--vocab-size 8000 \
--out tokenizer.json
```
--------------------------------
### Download and Prepare Datasets
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Creates dataset directories, downloads handwritten and PDF LaTeX data from Google Drive using gdown, and unzips them. It also splits the handwritten data into training and validation sets by moving a subset of images to a 'valimages' directory.
```python
!mkdir -p dataset/data
!mkdir images
# Google Drive ids
# handwritten: 13vjxGYrFCuYnwgDIUqkxsNGKk__D_sOM
# pdf - images: 176PKaCUDWmTJdQwc-OfkO0y8t4gLsIvQ
# pdf - math: 1QUjX6PFWPa-HBWdcY-7bA5TRVUnbyS1D
!gdown -O dataset/data/crohme.zip --id 13vjxGYrFCuYnwgDIUqkxsNGKk__D_sOM
!gdown -O dataset/data/pdf.zip --id 176PKaCUDWmTJdQwc-OfkO0y8t4gLsIvQ
!gdown -O dataset/data/pdfmath.txt --id 1QUjX6PFWPa-HBWdcY-7bA5TRVUnbyS1D
os.chdir('dataset/data')
!unzip -q crohme.zip
!unzip -q pdf.zip
# split handwritten data into val set and train set
os.chdir('images')
!mkdir ../valimages
!ls | shuf -n 1000 | xargs -i mv {} ../valimages
os.chdir('../../..')
```
--------------------------------
### Generate Training Configuration
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Creates a 'colab.yaml' configuration file with various training parameters. This includes model architecture details, optimizer settings, data paths, and training epochs. The 'debug' flag is set to true if wandb is not used.
```python
# generate colab specific config (set 'debug' to true if wandb is not used)
!echo {backbone_layers: [2, 3, 7], betas: [0.9, 0.999], batchsize: 10, bos_token: 1, channels: 1, data: dataset/data/train.pkl, debug: true, decoder_args: {'attn_on_attn': true, 'cross_attend': true, 'ff_glu': true, 'rel_pos_bias': false, 'use_scalenorm': false}, dim: 256, encoder_depth: 4, eos_token: 2, epochs: 50, gamma: 0.9995, heads: 8, id: null, load_chkpt: 'weights.pth', lr: 0.001, lr_step: 30, max_height: 192, max_seq_len: 512, max_width: 672, min_height: 32, min_width: 32, model_path: checkpoints, name: mixed, num_layers: 4, num_tokens: 8000, optimizer: Adam, output_path: outputs, pad: false, pad_token: 0, patch_size: 16, sample_freq: 2000, save_freq: 1, scheduler: StepLR, seed: 42, temperature: 0.2, test_samples: 5, testbatchsize: 20, tokenizer: dataset/tokenizer.json, valbatches: 100, valdata: dataset/data/val.pkl} > colab.yaml
```
--------------------------------
### Command Line Interface (CLI) for LaTeX OCR
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Shows how to use the `pix2tex` command-line tool for LaTeX OCR. It supports predicting from image files, multiple files, glob patterns, interactive mode, and various output rendering options.
```bash
# Predict from image file
pix2tex equation.png
# Output: equation.png: \frac{1}{2} \sum_{i=1}^{n} x_i^2
# Predict from multiple files
pix2tex eq1.png eq2.png eq3.png
# Use glob patterns
pix2tex "images/*.png"
# Interactive mode (reads from clipboard on Enter)
pix2tex
# > Predict LaTeX code for image ("h" for help).
# > [press Enter to read from clipboard]
# Show rendered LaTeX using XeLaTeX
pix2tex --show equation.png
# Render in browser using KaTeX
pix2tex --katex equation.png
# Adjust sampling temperature (0-1, lower = more deterministic)
# In interactive mode, type: t=0.1
# Force CPU usage
pix2tex --no-cuda equation.png
# Disable automatic image resizing
pix2tex --no-resize equation.png
# Launch GUI mode
pix2tex --gui
# Or use the dedicated command:
# latexocr
```
--------------------------------
### Im2LatexDataset Class - Python
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Demonstrates the usage of the Im2LatexDataset class for loading, preprocessing, and batching image-equation pairs. It covers tokenizer generation, dataset creation, saving, loading, updating, combining, and iterating over batches.
```python
from pix2tex.dataset.dataset import Im2LatexDataset, generate_tokenizer
# Generate a custom tokenizer
generate_tokenizer(
equations=['math.txt'], # Text file with equations
output='tokenizer.json',
vocab_size=8000
)
# Create dataset from images and equations
dataset = Im2LatexDataset(
equations='path/to/equations.txt',
images='path/to/images/',
tokenizer='tokenizer.json',
shuffle=True,
batchsize=16,
max_seq_len=1024,
max_dimensions=(1024, 512),
min_dimensions=(32, 32),
pad=False,
keep_smaller_batches=False,
test=False
)
# Save dataset to pickle
dataset.save('train_dataset.pkl')
# Load pre-existing dataset
dataset = Im2LatexDataset().load('train_dataset.pkl')
# Update dataset parameters
dataset.update(
batchsize=32,
shuffle=True,
test=True,
max_dimensions=(512, 256)
)
# Combine datasets
dataset2 = Im2LatexDataset().load('additional_data.pkl')
dataset.combine(dataset2)
# Iterate over batches
for tokens, images in dataset:
if tokens is not None and images is not None:
# tokens: dict with 'input_ids' and 'attention_mask' tensors
# images: tensor of shape (batch, 1, height, width)
print(f"Batch shape: {images.shape}")
print(f"Token ids shape: {tokens['input_ids'].shape}")
```
--------------------------------
### Render Dataset Equations
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Renders a dataset of LaTeX equations into images, handling various rendering options.
```APIDOC
## Function: render_dataset
### Description
Renders a provided dataset of LaTeX equations into images. It handles different rendering modes (equation or inline), output directories, and preprocessing options. Returns indices of equations that could not be rendered.
### Method
`pix2tex.dataset.render.render_dataset`
### Parameters
#### Arguments
- **dataset** (numpy.ndarray) - An array containing the LaTeX equations to render.
- **unrendered** (numpy.ndarray) - An array of integers, corresponding to the `dataset`, indicating the names of saved images for unrendered equations.
- **args** (Union[Namespace, Munch]) - An object containing additional arguments for rendering, such as:
- `mode` (str): 'equation' or 'inline'.
- `out` (str): Output directory path.
- `divable` (int): Common factor for division.
- `batchsize` (int): Number of samples to render per batch.
- `dpi` (int): Dots per inch for rendering.
- `font` (str): Math font to use.
- `preprocess` (str): Preprocessing options (e.g., 'crop', 'alpha off').
- `shuffle` (bool): Whether to shuffle the dataset before rendering.
### Request Example
```python
import numpy as np
from argparse import Namespace
# Assuming pix2tex.dataset.render is available
# from pix2tex.dataset.render import render_dataset
# Dummy data and args for example
data = np.array(["\\int x dx", "\\sum i^2"])
unrendered_indices = np.array([], dtype=int)
render_args = Namespace(
mode='equation',
out='./rendered_images',
divable=1,
batchsize=32,
dpi=300,
font='default',
preprocess='crop',
shuffle=False
)
# Call the function (actual call requires the module to be imported and functional)
# unrendered_eq_indices = render_dataset(data, unrendered_indices, render_args)
# print(f"Indices of unrendered equations: {unrendered_eq_indices}")
```
### Response
#### Success Response
- **numpy.ndarray**: An array containing the indices of equations that could not be rendered successfully.
#### Response Example
```json
{
"status": "success",
"unrendered_indices": [2, 5]
}
```
```
--------------------------------
### Generate Training Dataset PKL
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Generates a training dataset in PKL format using the pix2pix.dataset module. It combines image data from 'dataset/data/images' and 'dataset/data/train' with ground truth from 'dataset/data/CROHME_math.txt' and 'dataset/data/pdfmath.txt'. The output is saved to 'dataset/data/train.pkl'.
```python
!python -m pix2tex.dataset.dataset -i dataset/data/images dataset/data/train -e dataset/data/CROHME_math.txt dataset/data/pdfmath.txt -o dataset/data/train.pkl
```
--------------------------------
### Render TeX Math to a DOM Element (JavaScript)
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/pix2tex/dataset/preprocessing/third_party/katex/README.md
This JavaScript code demonstrates how to use `katex.render` to convert a TeX math expression into HTML and render it within a specified DOM element. It handles potential parsing errors by throwing a `katex.ParseError`.
```javascript
katex.render("c = \pm\sqrt{a^2 + b^2}", element);
```
--------------------------------
### Image Padding Utility
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Pads an input PIL Image to the next full divisible value of a specified integer. It also normalizes the image and inverts it if necessary.
```APIDOC
## pix2tex.utils.utils.pad
### Description
Pads an input PIL Image to the next full divisible value of a specified integer. It also normalizes the image and inverts it if necessary.
### Method
#### pad(img: PIL.Image, divable: int = 32) -> PIL.Image
### Parameters
#### Args
- **img** (PIL.Image) - input image
- **divable** (int, optional) - The value to divide the image dimensions by. Defaults to 32.
### Returns
- **PIL.Image** - The padded and potentially normalized/inverted image.
```
--------------------------------
### GUI Application
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Launch the graphical user interface for a user-friendly experience with screenshot capabilities and visual feedback.
```APIDOC
## GUI Application
### Description
A PyQt6-based graphical user interface (GUI) is provided for a more interactive and visual experience. It includes features like screenshot capture, MathJax rendering, and format conversion, supporting drag-and-drop and clipboard paste.
### Launching the GUI
```bash
# Launch GUI using the command line
latexocr
# or
pix2tex --gui
```
### Programmatic Launch
```python
from pix2tex.gui import main
from munch import Munch
# Launch GUI with default settings
arguments = Munch({
'config': 'settings/config.yaml',
'checkpoint': 'checkpoints/weights.pth',
'no_cuda': False,
'no_resize': False,
'temperature': 0.333
})
main(arguments)
```
### GUI Features
- **Screenshot Capture**: Press `Alt+S` (or `Option+S` on macOS).
- **Clipboard Paste**: Press `Ctrl+V`.
- **Drag and Drop**: Load image files directly into the application.
- **Format Options**: Choose output format (Raw, LaTeX-$, LaTeX-$$, Sympy).
- **Temperature Control**: Adjust the sampling temperature using a slider.
- **Retry Prediction**: Get alternative predictions for an image.
```
--------------------------------
### Render TeX Math to an HTML String (JavaScript)
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/pix2tex/dataset/preprocessing/third_party/katex/README.md
This JavaScript snippet shows how to use `katex.renderToString` to generate an HTML string representation of a TeX math expression. This is useful for server-side rendering or when you need the HTML output directly. Like `render`, it throws a `katex.ParseError` on invalid input.
```javascript
var html = katex.renderToString("c = \pm\sqrt{a^2 + b^2}");
// '...'
```
--------------------------------
### Image Resizing and Padding
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Resizes or pads an image to fit within specified maximum or minimum dimensions.
```APIDOC
## Function: minmax_size
### Description
Resizes or pads an image to fit into given dimensions, ensuring it meets specified minimum or maximum size constraints.
### Method
`pix2tex.cli.minmax_size`
### Parameters
#### Arguments
- **img** (PIL.Image.Image) - The image object to be resized or padded.
- **max_dimensions** (Tuple[int, int], optional) - The maximum width and height the image should not exceed. Defaults to None.
- **min_dimensions** (Tuple[int, int], optional) - The minimum width and height the image should meet. Defaults to None.
### Request Example
```python
from PIL import Image
from pix2tex.cli import minmax_size
# Load an image
img = Image.open("path/to/your/image.png")
# Resize image to max 512x512
resized_img = minmax_size(img, max_dimensions=(512, 512))
# Resize image to min 256x256
resized_img_min = minmax_size(img, min_dimensions=(256, 256))
# Resize image to fit within 800x600 and at least 300x300
resized_img_both = minmax_size(img, max_dimensions=(800, 600), min_dimensions=(300, 300))
```
### Response
#### Success Response
- **Image**: A PIL.Image.Image object that has been resized or padded according to the specified dimensions.
#### Response Example
```json
{
"status": "success",
"message": "Image processed successfully."
}
```
```
--------------------------------
### Training Script - Python
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Python script for training custom LaTeX OCR models. It loads configurations from a YAML file, parses arguments, sets random seeds, and initiates the training process.
```python
from pix2tex.train import train
from pix2tex.utils import parse_args, seed_everything
from munch import Munch
import yaml
# Load configuration
with open('config.yaml', 'r') as f:
params = yaml.load(f, Loader=yaml.FullLoader)
args = parse_args(Munch(params))
seed_everything(args.seed)
# Start training
train(args)
```
--------------------------------
### PyDemacro LaTeX Command Replacement
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Replaces custom LaTeX commands (\newcommand, \def, \let) in a document with their definitions.
```APIDOC
## Function: pydemacro
### Description
Processes a LaTeX document string to replace custom commands defined using `\newcommand`, `\def`, and `\let` with their corresponding definitions. This function helps in simplifying LaTeX source code for further processing.
### Method
`pix2tex.dataset.demacro.pydemacro`
### Parameters
#### Arguments
- **t** (str) - The input LaTeX document string.
### Request Example
```python
from pix2tex.dataset.demacro import pydemacro
latex_doc = "\\documentclass{article}\\newcommand{\\mycommand}[1]{Hello #1}\\begin{document}\\mycommand{World}!\\end{document}"
processed_doc = pydemacro(latex_doc)
print(processed_doc)
```
### Response
#### Success Response
- **str**: The processed LaTeX document string with custom commands replaced.
#### Response Example
```json
{
"status": "success",
"processed_document": "\\documentclass{article}Hello World!\\end{document}"
}
```
```
--------------------------------
### Use pix2tex from Python
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/README.md
Demonstrates how to use the pix2tex model directly within a Python script. It involves opening an image file using PIL and then passing the image object to the LatexOCR model for prediction. The predicted LaTeX code is then printed to the console.
```python
from PIL import Image
from pix2tex.cli import LatexOCR
img = Image.open('path/to/image.png')
model = LatexOCR()
print(model(img))
```
--------------------------------
### Seed Everything Utility
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Seeds all random number generators (RNGs) for reproducible results.
```APIDOC
## pix2tex.utils.utils.seed_everything
### Description
Seeds all random number generators (RNGs) for reproducible results.
### Method
#### seed_everything(seed: int)
### Parameters
#### Args
- **seed** (int) - The seed value to use for RNGs.
```
--------------------------------
### Utility Functions for Image and Text Processing
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
This Python code showcases various utility functions from the pix2tex library, including image padding and resizing, LaTeX post-processing, token-to-string conversion, and model path management. It also demonstrates how to retrieve optimizer and scheduler classes.
```python
from pix2tex.utils import (
pad, post_process, token2str, seed_everything,
parse_args, get_optimizer, get_scheduler, in_model_path
)
from pix2tex.cli import minmax_size
from PIL import Image
import torch
# Seed all random generators for reproducibility
seed_everything(42)
# Pad image to dimensions divisible by 32
img = Image.open('equation.png')
Padded_img = pad(img, divable=32)
# Resize image within bounds
resized_img = minmax_size(
img,
max_dimensions=(1024, 512),
min_dimensions=(32, 32)
)
# Post-process LaTeX output (remove extra whitespace)
raw_latex = r'\frac { 1 } { 2 } x ^ 2'
cleaned = post_process(raw_latex)
print(cleaned) # \frac{1}{2}x^2
# Convert token IDs to string
from transformers import PreTrainedTokenizerFast
tokenizer = PreTrainedTokenizerFast(tokenizer_file='tokenizer.json')
tokens = torch.tensor([[1, 45, 123, 67, 2]]) # [BOS] ... [EOS]
latex_strings = token2str(tokens, tokenizer)
print(latex_strings[0])
# Work within model directory context
with in_model_path():
# Current directory is pix2tex/model/
import os
print(os.listdir('checkpoints'))
print(os.listdir('settings'))
# Get optimizer and scheduler
from munch import Munch
args = Munch({'optimizer': 'AdamW', 'scheduler': 'StepLR'})
OptClass = get_optimizer(args.optimizer) # torch.optim.AdamW
SchedClass = get_scheduler(args.scheduler) # torch.optim.lr_scheduler.StepLR
```
--------------------------------
### Python API: LatexOCR Class for LaTeX OCR
Source: https://context7.com/lukas-blecher/latex-ocr/llms.txt
Demonstrates how to use the LatexOCR class from the pix2tex Python API to convert images to LaTeX code. It handles model loading and prediction, with options for custom configurations and image resizing.
```python
from PIL import Image
from pix2tex.cli import LatexOCR
# Initialize the model (downloads checkpoints automatically on first use)
model = LatexOCR()
# Load and predict from an image file
img = Image.open('equation.png')
latex_code = model(img)
print(latex_code)
# Output: \frac{1}{2} \sum_{i=1}^{n} x_i^2
# Predict from clipboard image (returns empty string if no image)
result = model(None)
# Retry last prediction with different resize
result = model(img, resize=False)
# Custom configuration
from munch import Munch
custom_args = Munch({
'config': 'settings/config.yaml',
'checkpoint': 'checkpoints/weights.pth',
'no_cuda': False, # Use GPU if available
'no_resize': False, # Enable image resizer
'temperature': 0.25 # Sampling temperature
})
model = LatexOCR(arguments=custom_args)
latex_code = model(img)
```
--------------------------------
### Read TeX Files
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Reads all TeX files within a specified path, concatenating their content into a single string. Supports .tar.gz archives.
```APIDOC
## Function: read_tex_files
### Description
Reads all TeX files located at the given file path. If the path points to a `.tar.gz` file, it will extract and read the TeX files from within the archive. Otherwise, it attempts to read the file directly as a text file. All content is concatenated into a single string.
### Method
`pix2tex.dataset.arxiv.read_tex_files`
### Parameters
#### Arguments
- **file_path** (str) - The path to the LaTeX source file or a `.tar.gz` archive containing LaTeX source files.
- **demacro** (bool, optional) - Deprecated. Indicates whether to call an external de-macro program. Defaults to False.
### Request Example
```python
from pix2tex.dataset.arxiv import read_tex_files
# Read a single .tex file
content_single = read_tex_files("path/to/your/document.tex")
# Read .tex files from a tar.gz archive
content_archive = read_tex_files("path/to/your/archive.tar.gz")
```
### Response
#### Success Response
- **str**: A single string containing the concatenated content of all read TeX files.
#### Response Example
```json
{
"status": "success",
"content": "\\documentclass{article}\\begin{document}This is the content of the tex file.\\end{document}"
}
```
```
--------------------------------
### Generate Validation Dataset PKL
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_training.ipynb
Generates a validation dataset in PKL format, similar to the training dataset. It uses 'dataset/data/valimages' and 'dataset/data/val' for images and the same ground truth files, saving the output to 'dataset/data/val.pkl'.
```python
!python -m pix2tex.dataset.dataset -i dataset/data/valimages dataset/data/val -e dataset/data/CROHME_math.txt dataset/data/pdfmath.txt -o dataset/data/val.pkl
```
--------------------------------
### Image to LaTeX Conversion using OCR
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/notebooks/LaTeX_OCR_test.ipynb
This Python code snippet handles the image upload and conversion process. It takes uploaded images, processes them using the initialized LaTeXOCR model to extract mathematical expressions, prints the predicted LaTeX code, and displays the results in a formatted table.
```python
imgs = upload_files()
predictions = []
for name, f in imgs:
img = Image.open(f)
math = model(img)
print(math)
predictions.append('\mathrm{%s} & \displaystyle{%s}'%(name, math))
Math(table%'\\'.join(predictions))
```
--------------------------------
### Demacro Error Exception
Source: https://github.com/lukas-blecher/latex-ocr/blob/main/docs/pix2tex.md
Custom exception raised during the demacro process.
```APIDOC
## Exception: DemacroError
### Description
Custom exception class for errors encountered during the LaTeX demacro process.
### Method
`pix2tex.dataset.demacro.DemacroError`
### Parameters
No direct parameters for instantiation, inherits from `Exception`.
### Request Example
```python
from pix2tex.dataset.demacro import DemacroError
try:
# Some operation that might raise DemacroError
raise DemacroError("Failed to process LaTeX macros.")
except DemacroError as e:
print(f"Caught an error: {e}")
```
### Response
#### Success Response
- **DemacroError instance**: An instance of the DemacroError exception.
#### Response Example
```json
{
"status": "error",
"error_type": "DemacroError",
"message": "Failed to process LaTeX macros."
}
```
```