A command-line tool to create beautiful PDF equations from LaTeX markup. This script demonstrates file operations, subprocess management, and cross-platform compatibility in Python.
Many years ago, when I was a college student, it was difficult to get a free equation editor, so I made this script. Now it is very easy to find free equation editors, however this script remains a simple and useful example for working with LaTeX programmatically.
- Convert LaTeX equations to high-quality PDF files
- Input validation and error handling
- Cross-platform support (Linux, macOS, Windows)
- Automatic cleanup of temporary files
- Customizable equation scaling
- Safe filename handling
- Automatic detection of LaTeX compiler (pdflatex preferred)
- Python 3.6 or higher
- No external Python packages required (uses only standard library)
You need a LaTeX distribution installed on your system:
Linux:
sudo apt-get install texlive texlive-latex-extra # Debian/Ubuntu
sudo dnf install texlive-scheme-full # FedoramacOS:
brew install --cask mactexWindows:
- MiKTeX: https://miktex.org/
- TeX Live: https://tug.org/texlive/
The script will automatically detect and use pdflatex (preferred) or fall back to latex + dvipdfm.
No installation needed! Just download ltxeq.py and run it.
# Clone or download
git clone <repository-url>
cd eqeditor-main
# Make executable (optional, Linux/macOS)
chmod +x ltxeq.pypython ltxeq.py -e "2x=\pi+c\rho" -o "equation"This creates equation.pdf and opens it in your default PDF viewer.
Simple equation:
python ltxeq.py -e "E=mc^2" -o "einstein"Complex integral:
python ltxeq.py -e "\int_0^\infty e^{-x^2}dx = \frac{\sqrt{\pi}}{2}" -o "gaussian"Custom scale (larger equation):
python ltxeq.py -e "\frac{-b \pm \sqrt{b^2-4ac}}{2a}" -o "quadratic" --scale 8Don't open PDF automatically:
python ltxeq.py -e "\nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}" -o "maxwell" --no-showKeep temporary files for debugging:
python ltxeq.py -e "\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}" -o "euler" --keep-tempVerbose output:
python ltxeq.py -e "a^2 + b^2 = c^2" -o "pythagoras" -v| Option | Description | Default |
|---|---|---|
-e, --equation |
LaTeX equation string (required) | - |
-o, --output |
Output filename (without extension) | eqtem |
--scale |
Scale factor for equation size | 5 |
--no-show |
Don't open PDF after creation | False |
--keep-temp |
Keep temporary LaTeX files | False |
-v, --verbose |
Print detailed information | False |
-h, --help |
Show help message | - |
- Validation: Checks equation syntax (balanced braces, brackets, parentheses)
- Template: Wraps equation in standalone LaTeX document with required packages
- Compilation: Compiles using pdflatex (or latex → dvipdfm)
- Cleanup: Removes temporary files (.aux, .log, .dvi, etc.)
- Display: Opens PDF in system default viewer
The script includes robust error handling:
- Validates LaTeX syntax before compilation
- Checks if LaTeX is installed
- Verifies successful compilation
- Sanitizes filenames to prevent injection attacks
- Provides clear error messages with suggestions
"LaTeX is not installed"
- Install a LaTeX distribution (see Requirements section)
- Ensure
pdflatexorlatexis in your PATH
"Invalid equation syntax"
- Check for balanced braces:
{} - Check for balanced brackets:
[] - Check for balanced parentheses:
()
PDF doesn't open automatically
- Use
--verboseto see detailed output - Try
--no-showand open PDF manually
Compilation timeout
- Check equation syntax for infinite loops
- Use
--verboseto see LaTeX output
MIT
Free Software, Hell Yeah!