Skip to content

Repository files navigation

📐 LaTeX2Img-CLI

A high-performance, beautiful LaTeX to image conversion tool and library powered by Puppeteer and KaTeX.

License: MIT Node.js Version

LaTeX2Img-CLI allows you to transform text containing LaTeX equations into crisp, high-resolution PNG images. Perfect for documentation, sharing equations on social media, or generating assets for web projects.


✨ Features

  • 🚀 Lightning Fast: Powered by KaTeX for near-instant math typesetting.
  • 🎨 Premium Aesthetics: Uses the Inter font for a modern, clean look.
  • 📱 High Resolution: Captures at 2x DPI for ultra-sharp text and symbols.
  • 🛠 Flexible: Supports mixed content (text + math), multiple delimiters ($, $$, \(, \[), and custom styling.
  • 💻 CLI & Library: Use it directly from your terminal or integrate it into your Node.js apps.

🚀 Installation

# Clone the repository
git clone https://github.com/prajdabre/latex2img-cli.git
cd latex2img-cli

# Install dependencies
npm install

# Link for global CLI usage
npm link

🛠 Usage

1. Command Line Interface (CLI)

After linking, you can use the latex2img command:

latex2img 'The mass-energy equivalence: $E = mc^2$' -o output.png -s 32 -w 600

Options

  • -o, --output <path>: Output image path (default: output.png)
  • -s, --font-size <number>: Font size in pixels (default: 24)
  • -p, --padding <number>: Padding in pixels (default: 20)
  • -w, --width <number>: Fixed width for text wrapping
  • -b, --background <color>: Custom background color (CSS value)
  • -t, --theme <name>: Theme choice (modern, handwritten, chalkboard)
  • -g, --grounded [level]: Extract boxes to .json (char or equation)
  • -d, --debug [level]: Render boxes on image (char or equation)
  • -n, --noise <0-1>: Apply artifact synthesis (blur, jitter, ink bleed)

🔬 Grounded STEM Training Data

This tool is designed for generating high-quality grounded synthetic training data for STEM OCR models.

  • JSON Metadata: Using --grounded produces a JSON file mapping every symbol to its precise [x, y, w, h] coordinates.
  • Debug View: Using --debug renders those boxes (red for characters, blue for equations) directly on the image for verification.
  • Artifact Synthesis: Using --noise applies randomized jitter, skew, and ink-bleed effects to simulate messy handwriting or poor scanning conditions, perfect for testing model robustness.

Example of debug output: Debug Quadratic

🎨 Themes

Theme Description Preview
modern (default) Clean, professional Inter font on white/transparent background. View
handwritten Organic "Architects Daughter" font with a slight tilt and ink-blue color. View
chalkboard Classroom aesthetic with "Patrick Hand" font and slate-board background. View

2. Node.js Library

const latexToImage = require('./index');

async function convert() {
    await latexToImage(
        "Quadratic formula: $$x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$$",
        'quadratic.png',
        {
            fontSize: 28,
            padding: 40,
            backgroundColor: '#f8f9fa'
        }
    );
}

convert();

🧪 Examples

Description Rendered Result Input Text
Basic Math (Inline + Block) Basic Math Link
Calculus (Integrals) Calculus Link
Linear Algebra (Matrices) Matrix Algebra Link
Physics (Standard Model) Physics Link
Chemistry (Reactions) Chemistry Link
Stress Test (Overflow/Wide) Wide Equation Link
Multi-line (Lists/Notes) Multi-line Link
Theorem (Mixed Content) Theorem Link
Quadratic (Large Block) Quadratic Link
Einstein (Inline) Einstein Link
Grounded (Metadata) Grounded JSON
Synthetic (Noisy) Synthetic JSON

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.


Created by Raj Dabre

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages