latex
the commands, environments, and packages for everyday documents and math, with the math rendered.
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
- papers and reports
- math-heavy notes
- anything with equations
- documents you keep in git
on this page
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.2,3
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.
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 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}
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} \]
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} \]
an integral. \infty is infinity, and \, puts a thin space before the :11
\[ \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} \]
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}
\]
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
| greek | command | greek | command |
|---|---|---|---|
\alpha | \beta | ||
\gamma | \delta | ||
\epsilon | \varepsilon | ||
\theta | \lambda | ||
\mu | \pi | ||
\sigma | \phi | ||
\omega | \Omega | ||
\Delta | \Sigma |
| relation | command | relation | command |
|---|---|---|---|
\leq | \geq | ||
\neq | \approx | ||
\equiv | \sim | ||
\in | \notin | ||
\subset | \subseteq |
| operator | command | operator | command |
|---|---|---|---|
\times | \cdot | ||
\pm | \div | ||
\sum | \prod | ||
\int | \partial | ||
\nabla | \sqrt{x} | ||
\cup | \cap |
| arrow | command | arrow | command |
|---|---|---|---|
\to | \leftarrow | ||
\Rightarrow | \Leftarrow | ||
\Leftrightarrow | \iff | ||
\mapsto | \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.
- geometry (opens in a new tab): page size and margins. it knows the standard paper sizes and centers the text block.
- amsmath (opens in a new tab): the math above:
align,equation*,cases, the matrices,\text. it ships with latex. - enumitem (opens in a new tab): labels and spacing for
itemize,enumerate, anddescription. - hyperref (opens in a new tab): turns cross-references into links in the pdf, and adds
\urland\href. load it after your other packages.19 - graphicx (opens in a new tab):
\includegraphics, with options likewidthandangle. - booktabs (opens in a new tab):
\toprule,\midrule, and\bottomrule, for tables like the ones in books and journals. - xcolor (opens in a new tab): colors by name, plus tints, shades, and mixes, for
\textcolorand the rest.
compiling
pdflatex paper.tex: writes a pdf directly.20xelatex paper.tex: unicode input and opentype fonts.20lualatex paper.tex: unicode and opentype too, with lua built in. it grew out of pdftex.20latexmk -pdf paper.tex: runs latex, and bibtex when needed, as many times as the references take.-xelatexand-lualatexswitch the engine.21tectonic paper.tex: one self-contained program, built on xetex and tex live. it downloads packages as it needs them and reruns until done.22- tex live: the full tex system, for linux, macos, and windows. mactex is all of tex live for macos, plus a few mac apps.23
- overleaf: latex in the browser, with nothing to install.24
pitfalls
- unescaped special characters.
_and^outside math are errors. a bare&belongs in tables and alignments, and in text it stops the build withMisplaced alignment tab character &. write\_,\^{}, and\&.6,25 %eats the rest of the line.50% offloses everything after the%, with no error. write50\% off.6- spaces after commands. a command spelled with letters, like
\LaTeX, swallows the space after it, so\LaTeX isprints as one word. write\LaTeX{} isor\LaTeX\ is.26 - overfull
\hboxwarnings. tex couldn’t break a line well, so it left one too wide, running into the margin. reword the sentence first, or load microtype.\sloppytrades the warning for loose word spacing.27 ??instead of a number. latex writes labels to the.auxfile and reads them back on the next run, so the first run can’t fill them in. run it twice, or let latexmk count.17
sources
-
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. ↩
-
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. ↩
-
computers & typesetting (opens in a new tab), knuth’s page for the texbook and the rest of the series that documents tex. ↩
-
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. ↩
-
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). ↩ -
reserved characters (opens in a new tab) and printing them (opens in a new tab) in the unofficial latex2e reference manual. ↩ ↩2 ↩3
-
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 -
itemize (opens in a new tab) and description (opens in a new tab) in the unofficial latex2e reference manual. ↩
-
the enumitem manual (opens in a new tab): the
labelkey with\alph*, andnosep. ↩ -
l2tabu (opens in a new tab), section 1.6, and the tex faq on why to use
\[...\](opens in a new tab). ↩ -
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\normcommand (4.14), and\text(6). ↩ ↩2 ↩3 ↩4 ↩5 ↩6 -
katex (opens in a new tab) and its list of supported functions (opens in a new tab). ↩
-
greek letters (opens in a new tab) and arrows (opens in a new tab) in the unofficial latex2e reference manual. ↩
-
tabular (opens in a new tab) in the unofficial latex2e reference manual. ↩
-
the booktabs manual (opens in a new tab), sections 2 and 3. ↩
-
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). ↩ -
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
-
\newcommandand\renewcommand(opens in a new tab) in the unofficial latex2e reference manual. ↩ -
the hyperref manual (opens in a new tab): why it loads last, and
\hrefand\url. ↩ -
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
-
latexmk (opens in a new tab) on ctan, and the
-pdf,-xelatex, and-lualatexoptions in its manual (opens in a new tab). ↩ -
tectonic (opens in a new tab), on its own site. ↩
-
ctan’s pages for tex live (opens in a new tab) and mactex (opens in a new tab). ↩
-
overleaf (opens in a new tab), on its own site. ↩
-
overleaf’s page on the misplaced alignment tab character (opens in a new tab) error. ↩
-
backslash-space (opens in a new tab) in the unofficial latex2e reference manual: why a command eats the space after it. ↩
-
the tex faq on overfull boxes (opens in a new tab), and l2tabu (opens in a new tab), section 1.8, on
\sloppy. ↩