FormaTeX

\begin{article}

draft — not published

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.

·5 min read·
Math Typesetting in LaTeX: From Basics to API Integration

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:

latex
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:

latex
\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

ModeSyntaxUse 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
latex
\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:

latex
\usepackage{amsmath}

It provides:

  • align, align* — aligned equations with &
  • gather — centered multi-line equations
  • cases — piecewise function notation
  • bmatrix, pmatrix — matrices with brackets
  • \dfrac, \tfrac — display/text-style fractions
  • \text{...} — normal text inside math mode
latex
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:

latex
\usepackage{amssymb}

Let $f: \mathbb{R} \to \mathbb{R}$ be a continuous function.

mathtools

An extension to amsmath with additional constructs:

latex
\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:

bash
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.pdf

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

\end{article}

Back to blog

\related{posts}

One quick thing

We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.

Cookie policy