\begin{article}
Math Typesetting in LaTeX: From Basics to API Integration
LaTeX is the gold standard for typesetting mathematical equations. Learn the syntax, key packages, and how to compile math-heavy documents via the FormaTeX API.

Every mathematical journal, physics textbook, and quantitative research paper is typeset in LaTeX. This is not a coincidence — LaTeX's math mode is a dedicated typesetting engine refined over 40 years to produce equations that are readable, precisely spaced, and resolution-independent. If your application generates documents with formulas, LaTeX is the correct tool for programmatic PDF generation.
Why Math Rendering Matters
Math rendering in Word, HTML+MathJax, and image-based approaches all have trade-offs:
- Word equations export as low-resolution images in many PDF export paths
- MathJax/KaTeX requires JavaScript and looks different across renderers
- Image-based formulas are not searchable, not copy-pasteable, and not accessible
- LaTeX math is vector, searchable, perfectly spaced, and supported by every academic tool
When your documents need to be published, submitted to journals, or printed at scale, LaTeX produces better PDFs than Word and is the only correct answer.
LaTeX Math Syntax Basics
LaTeX has two math modes: inline and display.
Inline Math
Wrap expressions in $...$ for math that flows with the text:
The quadratic formula is $x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.This produces: The quadratic formula is x = (−b ± √(b²−4ac)) / 2a — inline, correctly sized, properly spaced.
Display Math
Use \[...\] or the equation environment for standalone equations:
\begin{equation}
E = mc^2
\end{equation}The equation environment also adds automatic numbering, which you can reference with \label and \ref.
Inline vs. Display Math
| Mode | Syntax | Use for |
|---|---|---|
| Inline | $...$ | Formulas in running text |
| Unnumbered display | \[...\] | Important formulas, no numbering |
| Numbered display | \begin{equation} | Formulas you will reference |
| Multi-line | \begin{align} | Systems, derivations |
\begin{align}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} &= 0 \\
\nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} &= \mu_0 \mathbf{J} + \mu_0 \varepsilon_0 \frac{\partial \mathbf{E}}{\partial t}
\end{align}This produces Maxwell's equations in aligned display form, numbered, with correct spacing around the = signs and & alignment.
Key Packages
amsmath
amsmath is the essential math package — add it to every document with equations:
\usepackage{amsmath}It provides:
align,align*— aligned equations with&gather— centered multi-line equationscases— piecewise function notationbmatrix,pmatrix— matrices with brackets\dfrac,\tfrac— display/text-style fractions\text{...}— normal text inside math mode
f(x) =
\begin{cases}
x^2 & \text{if } x \geq 0 \\
-x & \text{if } x < 0
\end{cases}amssymb
Adds hundreds of math symbols: \mathbb{R}, \mathcal{F}, \leq, \geq, \infty, \partial, and many more:
\usepackage{amssymb}
Let $f: \mathbb{R} \to \mathbb{R}$ be a continuous function.mathtools
An extension to amsmath with additional constructs:
\usepackage{mathtools}
% Paired delimiters that scale automatically
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
The norm is $\norm{x}^2 = x \cdot x$.Use \left( and \right) to make delimiters scale to their contents automatically: \left(\frac{a}{b}\right) produces correctly sized parentheses around a fraction.
Compiling Math Documents via API
Any LaTeX document with math compiles via the FormaTeX API. The pdflatex engine handles amsmath and amssymb without any special configuration. For a full breakdown of when to choose each engine, see the complete guide to LaTeX engines:
LATEX='
\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
The Gaussian integral:
\begin{equation}
\int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt{\pi}
\end{equation}
\end{document}'
curl -X POST https://api.formatex.io/api/v1/compile \
-H "X-API-Key: $FORMATEX_KEY" \
-H "Content-Type: application/json" \
-d "{\"content\": $(echo "$LATEX" | jq -Rs .), \"engine\": \"pdflatex\"}" \
--output math.pdfimport os
import requests
latex = r"""
\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}
The Gaussian integral:
\begin{equation}
\int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt{\pi}
\end{equation}
\end{document}
"""
response = requests.post(
"https://api.formatex.io/api/v1/compile",
headers={"X-API-Key": os.environ["FORMATEX_KEY"]},
json={"content": latex, "engine": "pdflatex"},
)
response.raise_for_status()
with open("math.pdf", "wb") as f:
f.write(response.content)const latex = `
\\documentclass{article}
\\usepackage{amsmath,amssymb}
\\begin{document}
The Gaussian integral:
\\begin{equation}
\\int_{-\\infty}^{\\infty} e^{-x^2}\\, dx = \\sqrt{\\pi}
\\end{equation}
\\end{document}
`;
const response = await fetch("https://api.formatex.io/api/v1/compile", {
method: "POST",
headers: {
"X-API-Key": process.env.FORMATEX_KEY!,
"Content-Type": "application/json",
},
body: JSON.stringify({ content: latex, engine: "pdflatex" }),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.log ?? error.error);
}
const buffer = Buffer.from(await response.arrayBuffer());
require("fs").writeFileSync("math.pdf", buffer);For documents with bibliography and cross-referenced equation numbers, use the latexmk engine — it runs the required compilation passes automatically so all references resolve correctly. You can also automate academic paper PDF generation end-to-end using the API.
Next Steps
- Sign up for free — 15 compilations per month, no card required
- API reference — endpoint schema and error format
- Engine guide — when to use pdflatex, xelatex, lualatex, or latexmk
Related Articles
- The Complete Guide to LaTeX Engines — Understand when to use pdfLaTeX, XeLaTeX, LuaLaTeX, or latexmk for math-heavy documents
- Automating Academic Paper PDFs with the LaTeX API — End-to-end PDF automation for papers, theses, and scientific reports that rely on math typesetting
- Why LaTeX Produces Better PDFs Than Word — Typography and equation quality comparison showing why LaTeX is the standard for scientific publishing
- LaTeX PDF Generation in Python — Full Python code examples for compiling math documents via REST API without a local TeX Live install
- Getting Started with FormaTeX — First API key, first compilation, and error handling basics for new users
\end{article}
\related{posts}




