tools

latex

the commands, environments, and packages for everyday documents and math, with the math rendered.

report a mistake
.md

you write the content and its structure as plain text. tex does the typesetting.

reserved
10 characters that need escaping
engine
pdflatex xelatex or lualatex for opentype fonts
math
amsmath align, cases, matrices
build
latexmk reruns until the references settle
reach for it
  • papers and reports
  • math-heavy notes
  • anything with equations
  • documents you keep in git
on this page
  1. document structure
  2. text
  3. lists
  4. math
  5. tables
  6. figures and images
  7. custom commands
  8. packages worth knowing
  9. compiling
  10. pitfalls
  11. sources

latex is a document preparation system: you write the content and mark its structure in plain text, and tex, the typesetting program underneath, works out how the page looks.1 leslie lamport wrote it in the early 1980s as a set of macros for donald knuth’s tex.23

loading the renderer

a latex editor and the page latex.js renders from it, updated as you type. five examples to start from: an article, math, lists, text and symbols, and a resume. an error shows under the editor with its line, and the last page that worked stays up.

edit the source and the page follows. pick an example to start over.

the playground runs latex.js, a latex to html translator written in javascript, in your browser.4 it handles the core: sections, text, lists, and math between $...$ or \[...\]. tables, figures, footnotes, custom commands, and the equation and align environments need a real latex install.

document structure

a file is a class, a preamble, and a body. \documentclass comes first and sets the kind of document. everything between it and \begin{document} is the preamble, where packages load. the body is what gets typeset.

\documentclass{article}      % or report, book, letter
\usepackage{amsmath}         % packages load in the preamble

\title{a short paper}
\author{your name}
\date{\today}                % \date{} leaves the date off

\begin{document}
\maketitle                   % prints the title block

\section{introduction}       % 1 introduction
text of the section.

\subsection{background}      % 1.1 background
\section*{thanks}            % starred: no number, not in the table of contents

% a comment runs to the end of the line
\end{document}

the levels go \part, \chapter, \section, \subsection, \subsubsection, \paragraph, \subparagraph. article has no \chapter. report and book do.5

text

\emph{emphasis}, \textbf{bold}, \texttt{monospace}
{\small smaller} and {\Large larger}    % a size lasts until its group ends

``double quotes'' and `single quotes'
pages 12--14                            % en dash, for ranges
a pause---like this---in a sentence     % em dash
well-known                              % hyphen

\# \$ \% \& \_ \{ \}                     % backslash in front
\textasciitilde{} \textasciicircum{} \textbackslash{}

a claim.\footnote{its note, at the foot of the page.}

one line\\
the next line, same paragraph

a blank line starts a new paragraph.

ten characters mean something to latex: #, $, %, &, _, {, }, ~, ^, and \. seven print with a backslash in front. the last three need commands of their own: \\ is a line break, and \~ and \^ put an accent on the next letter.6

quotes open with backticks and close with apostrophes, not straight quotes. one hyphen joins words, two make an en dash for ranges, three an em dash. sizes, smallest to largest: \tiny, \scriptsize, \footnotesize, \small, \normalsize, \large, \Large, \LARGE, \huge, \Huge.7

\\ ends a line but not the paragraph. a blank line ends the paragraph, and several blank lines count as one. for extra space, use \vspace{1em}, not a stack of \\. \footnote puts a numbered note at the bottom of the page.7

lists

\begin{itemize}              % bullets
  \item a point
  \item another
  \begin{enumerate}          % lists nest, up to four deep
    \item numbered
    \item and counted
  \end{enumerate}
\end{itemize}

\begin{description}
  \item[term] its meaning. the label prints in bold.
\end{description}

every entry starts with \item.8 the enumitem package takes options for the labels and the spacing:9

\usepackage{enumitem}                 % preamble

\begin{enumerate}[label=(\alph*)]     % (a), (b), (c)
  \item first
  \item second
\end{enumerate}

\begin{itemize}[nosep]                % no extra space between items
  \item tight
  \item list
\end{itemize}

math

inline math goes between dollar signs, like πr2\pi r^2 in this sentence. display math gets a line of its own: \[...\]. you’ll also see $$...$$. that’s plain tex: in latex it spaces displays inconsistently and breaks the fleqn option, so use \[...\].10

equation numbers a display. amsmath adds equation* without the number, and align for several lines: & goes before the symbol to line up on, \\ ends every line but the last, and each line gets a number (align* for none). a blank line anywhere inside math is an error.11

the area is $\pi r^2$.            % inline. \( \pi r^2 \) works too

\[ e^{i\pi} + 1 = 0 \]            % display, no number

\begin{equation}                  % display, numbered
  E = mc^2 \label{eq:energy}      % \ref{eq:energy} prints its number
\end{equation}

\begin{align}                     % lined up at the &, one number per line
  f(x) &= (x + 1)^2 \\
       &= x^2 + 2x + 1
\end{align}
f(x)=(x+1)2=x2+2x+1\begin{align} f(x) &= (x + 1)^2 \\ &= x^2 + 2x + 1 \end{align}

examples

each one as you’d type it in a .tex file, then rendered. the rendering on this page is katex, a library that typesets tex math as html when the site builds. it does math only, not whole documents.12

a fraction and a square root:

\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
x=−b±b2−4ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}

a sum with limits. _ sets the bottom, ^ the top, and in a display they sit below and above the sigma:11

\[ \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \]
∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}

an integral. \infty is infinity, and \, puts a thin space before the dxdx:11

\[ \int_0^\infty e^{-x} \, dx = 1 \]
∫0∞e−x dx=1\int_0^\infty e^{-x} \, dx = 1

a matrix. & splits the columns and \\ the rows. pmatrix draws parentheses, bmatrix brackets:11

\[ A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \]
A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}

cases, for a function defined piece by piece. \text sets words inside math:11

\[
  |x| = \begin{cases}
    x  & \text{if } x \geq 0 \\
    -x & \text{otherwise}
  \end{cases}
\]
∣x∣={xif x≥0−xotherwise|x| = \begin{cases} x & \text{if } x \geq 0 \\ -x & \text{otherwise} \end{cases}

symbols

a few of each. capital greek letters only have commands where they look different from latin ones, like \Delta and \Omega. \to is short for \rightarrow.13

greekcommandgreekcommand
α\alpha\alphaβ\beta\beta
γ\gamma\gammaδ\delta\delta
ϵ\epsilon\epsilonε\varepsilon\varepsilon
θ\theta\thetaλ\lambda\lambda
μ\mu\muπ\pi\pi
σ\sigma\sigmaϕ\phi\phi
ω\omega\omegaΩ\Omega\Omega
Δ\Delta\DeltaΣ\Sigma\Sigma
relationcommandrelationcommand
≤\leq\leq≥\geq\geq
≠\neq\neq≈\approx\approx
≡\equiv\equiv∼\sim\sim
∈\in\in∉\notin\notin
⊂\subset\subset⊆\subseteq\subseteq
operatorcommandoperatorcommand
×\times\times⋅\cdot\cdot
±\pm\pm÷\div\div
∑\sum\sum∏\prod\prod
∫\int\int∂\partial\partial
∇\nabla\nablax\sqrt{x}\sqrt{x}
∪\cup\cup∩\cap\cap
arrowcommandarrowcommand
→\to\to←\leftarrow\leftarrow
⇒\Rightarrow\Rightarrow⇐\Leftarrow\Leftarrow
⇔\Leftrightarrow\Leftrightarrow  ⟺  \iff\iff
↦\mapsto\mapsto↑\uparrow\uparrow

tables

\begin{tabular}{l c r}       % one letter per column: left, center, right
  \hline
  item  & count & price \\   % & between cells, \\ ends a row
  \hline
  apple & 3     & 1.20  \\
  pear  & 12    & 0.80  \\
  \hline
\end{tabular}

column letters are l, c, r, and p{3cm} for a column of wrapped text that wide. a | between them draws a vertical line. \hline draws a rule under a row.14

booktabs replaces \hline with three rules, heavier at the top and bottom, with more space around them. its manual gives two guidelines: no vertical lines, and no double lines.15

\usepackage{booktabs}        % preamble

\begin{tabular}{l r}
  \toprule
  item  & price \\
  \midrule
  apple & 1.20  \\
  pear  & 0.80  \\
  \bottomrule
\end{tabular}

figures and images

\usepackage{graphicx}                         % preamble

\begin{figure}[htbp]                          % here, top, bottom, or a page of floats
  \centering
  \includegraphics[width=0.8\textwidth]{plot} % finds plot.pdf or plot.png
  \caption{error against step size.}
  \label{fig:plot}                            % after the caption
\end{figure}

figure~\ref{fig:plot} shows the error.        % ~ is a space that won't break

a figure floats: latex puts it where it fits, which can be a page or more away from where it sits in the source. width, height, scale, and angle size and turn the image.16 \label goes after \caption, inside the figure.17

custom commands

\usepackage{amsmath, amssymb}                 % for \lVert and \mathbb

\newcommand{\R}{\mathbb{R}}                   % no arguments
\newcommand{\norm}[1]{\lVert #1 \rVert}       % one argument, used as #1

$f \colon \R^n \to \R$ and $\norm{x - y}$     % in the body

name a piece of notation once, and changing it later is one edit. \newcommand stops with an error if the name is taken. \renewcommand replaces one on purpose. a command takes up to nine arguments, #1 to #9.18 the \norm definition comes from the amsmath guide.11

packages worth knowing

one line each, from the package’s page on ctan, the tex archive.

compiling

pitfalls

sources

  1. about latex (opens in a new tab) on the latex project’s site: a document preparation system, and why it isn’t a word processor. ↩

  2. leslie lamport’s note on latex (opens in a new tab) in his list of writings: in the early 80s he needed macros for a book he planned, and knuth had started releasing the current version of tex. ↩

  3. computers & typesetting (opens in a new tab), knuth’s page for the texbook and the rest of the series that documents tex. ↩

  4. latex.js (opens in a new tab) by michael brade (mit), its source (opens in a new tab), and its limitations (opens in a new tab) page: it parses latex with a peg grammar and writes html, with css that imitates latex’s output. it can’t define new macros, and a package only works once its macros are rewritten in javascript. ↩

  5. the unofficial latex2e reference manual on document classes (opens in a new tab), sectioning (opens in a new tab), and \maketitle (opens in a new tab). ↩

  6. reserved characters (opens in a new tab) and printing them (opens in a new tab) in the unofficial latex2e reference manual. ↩ ↩2 ↩3

  7. the unofficial latex2e reference manual on text symbols (opens in a new tab) (quotes and dashes), font sizes (opens in a new tab), \\ (opens in a new tab), \par (opens in a new tab), and \footnote (opens in a new tab). ↩ ↩2

  8. itemize (opens in a new tab) and description (opens in a new tab) in the unofficial latex2e reference manual. ↩

  9. the enumitem manual (opens in a new tab): the label key with \alph*, and nosep. ↩

  10. l2tabu (opens in a new tab), section 1.6, and the tex faq on why to use \[...\] (opens in a new tab). ↩

  11. the amsmath user’s guide (opens in a new tab): limits on sums (section 2), displayed equations (3), matrices (4.1), spacing like \, (4.2), the \norm command (4.14), and \text (6). ↩ ↩2 ↩3 ↩4 ↩5 ↩6

  12. katex (opens in a new tab) and its list of supported functions (opens in a new tab). ↩

  13. greek letters (opens in a new tab) and arrows (opens in a new tab) in the unofficial latex2e reference manual. ↩

  14. tabular (opens in a new tab) in the unofficial latex2e reference manual. ↩

  15. the booktabs manual (opens in a new tab), sections 2 and 3. ↩

  16. the unofficial latex2e reference manual on floats (opens in a new tab), for the placement letters and why a float drifts, and \includegraphics (opens in a new tab). ↩

  17. the cross-referencing lesson (opens in a new tab) on learnlatex.org, and cross references (opens in a new tab) in the unofficial latex2e reference manual. ↩ ↩2

  18. \newcommand and \renewcommand (opens in a new tab) in the unofficial latex2e reference manual. ↩

  19. the hyperref manual (opens in a new tab): why it loads last, and \href and \url. ↩

  20. ctan’s pages for pdftex (opens in a new tab), xetex (opens in a new tab), and luatex (opens in a new tab). ↩ ↩2 ↩3

  21. latexmk (opens in a new tab) on ctan, and the -pdf, -xelatex, and -lualatex options in its manual (opens in a new tab). ↩

  22. tectonic (opens in a new tab), on its own site. ↩

  23. ctan’s pages for tex live (opens in a new tab) and mactex (opens in a new tab). ↩

  24. overleaf (opens in a new tab), on its own site. ↩

  25. overleaf’s page on the misplaced alignment tab character (opens in a new tab) error. ↩

  26. backslash-space (opens in a new tab) in the unofficial latex2e reference manual: why a command eats the space after it. ↩

  27. the tex faq on overfull boxes (opens in a new tab), and l2tabu (opens in a new tab), section 1.8, on \sloppy. ↩