(La)TeX style guide

These are my personal style guidelines for writing .tex files. These notes are built on my personal experience. You can also find elements of style guidelines in Knuth’s The TeXbook, in particular Chapters 16 through 19.

~ ˆ ~

Document structure

A .tex file is divided into two parts: the preamble and the body. The file is formatted as follows:

<preamble>

\begin{document}

<body>

\end{document}

Note that the body is not indented (see Nesting & environments).

Preamble

The preamble is divided into three parts: loading the document class, loading packages, and running commands. The preamble should look like this:

% Loading the document class
\documentclass{...}

% Loading packages
\usepackage{...}
...

% Run commands
\MyCommand{...}
...

While \documentclass and \usepackage are commands, we distinguish them due to their special role. Exceptions apply to the above partition:

Packages and commands should be loaded and run in a reasonable order. Try to maintain order by grouping related commands together. My suggestions:

Note that packages may need to be loaded in specific orders in order to work properly.

Body

The body consists of sections, paragraphs, and environments (text and math). The content can be regular text, math content, labels, or commands.

Commands declaring new sections, paragraphs, and environments that create paragraphs are preceded and followed by a single blank line:

... <content>

\section{<title>}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\begin{theorem}
  ...
\end{theorem}

<more content> ...

If you do not want to start a new paragraph after such an environment, add a single % to the blank line after the environment. See Nesting & environments for details about formatting environments.

New section commands may be accompanied by other commands, such as labels. These commands belong to the same code block as the \section (or similar) command and should not be separated by a blank line.

~ ˆ ~

Commands

Naming conventions

I don’t have any suggestions for naming commands. Typically they are UpperCamelCase or lowerCamelCase in packages, but I tend to keep macros lowercase.

Common macros

When defining math operators, use \DeclareMathOperator (from the library amsopn, which is loaded automatically by amsmath).

Usage

Commands that are used outside of the regular flow of text should preferably be on their own line, but sometimes it is more meaningful to have several commands on the same line.

By default, use curly braces around command arguments. Exceptions:

Example:

$2^n$, but $2^{\mathbb{N}}$.

You can write \LaTeX. But using \LaTeX{} in the middle of a sentence requires curly braces.

\[
  \left\{
    \sum_{i = 1}^n x_i
  \right\}
\]

Long and multiple arguments

If a command has a long argument, the long argument should be put on its own indented block: see Nesting & environments for details about formatting code blocks.

If a command has multiple arguments, then each argument is enclosed by curly braces and there is no space between consecutive arguments. Use % to remove unwanted whitespace (see Whitespace).

Example:

\title[%
  short title that is still somewhat long%
]{%
  long title that is even longer than the short title, so long that even the
  short title is somewhat long%
}

If an argument has a key-value syntax, then they should be formatted like function arguments in a programming language. Pick a programming language style guide and stick to it: see, for instance, the Google C++ style guide. I suggest writing key-value pairs as <key>=<value>, i.e., with no space around the equals sign. This is PEP 8’s recommendation for keyword arguments in Python.

~ ˆ ~

Text & general formatting

Whitespace

Line wrapping

There are two options for line wrapping. Either you respect an 79 character limit, or you do not. In the latter case, do not insert newlines in the middle of a sentence (unless the sentence is broken up by, say, an equation). You may go to a new line at the end of a sentence, even if you are not starting a new paragraph, but this is not required.

Punctuation

~ ˆ ~

Math

Air out math expressions, but keep the code similar to the expected output. General rules:

Regarding $ ... $ versus \( ... \) for inline math:

Example:

Write math expressions, both inline $e^{i \pi} + 1 = 0$, and display
\[
  \operatorname{li}(x) \coloneq \int_0^x \frac{\textnormal{d} t}{\ln t}
  \qquad \textnormal{and} \qquad
  \Pi_0(x) \coloneq \frac{1}{2} \left(
    \sum_{p, n : p^n < x} \frac{1}{n} + \sum_{p, n : p^n \leq x} \frac{1}{n}
  \right).
\]

Line breaks

Sometimes, mathematical expressions are too long to fit legibly on a single line: we then use line breaks. Line breaks in math content should prioritize symbols in the same order as syntax precedence: for example, break at the equals sign (=) before breaking at a plus sign (+) in one of the expressions. This also applies to grid symbols: break at a line break command (\\) before breaking at an ampersand (&) (see Arrays & grids). The order of priority could be as follows:

\\ > & > = > \cdot (multiplication) > + and so on.

Line breaks come before the breaking symbol, except for line break commands (e.g. \\, \cr), where the line break comes after. Essentially, having the breaking symbol at the beginning of the line tells the reader that the line is not over. When starting a new line, formatting commands like \\, \cr, and & do not increase the indentation, while visible elements like \cdot, +, and so on, do.

Example:

\begin{align*}
  \sum_{k = 1}^10 k & = \frac{1}{2} (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
    + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1) \\
  & = \frac{1}{2} (11 + 11 + 11 + 11 + 11 + 11 + 11 + 11 + 11 + 11) \\
  & = \frac{1}{2} 10 \cdot 11 \\
  & = 55.
\end{align*}

Symbols at line breaks should not be placed on their own line. An exception is for binary relations surrounded by whitespace commands. These can be on their own line:

\[
  ...
  \quad \text{and} \quad
  ...
\]

Another way to break a line is to move content in brackets into an indented block: see Nesting & environments.

Equivalent macros

Sometimes there are several ways to write the same thing that are either identical or very similar. Some rules:

~ ˆ ~

Nesting & environments

Most complex TeX code comes in the form of nested blocks: this includes environments, but also the arguments of commands and math expressions inside brackets.

Code blocks & block delimiters

A code block consists of an opening block delimiter, followed by inner content, followed by a closing block delimiter. (Block) delimiters include:

Importantly, code blocks can and will be nested. In general, the inner content of a code block is indented once relative to the delimiters.

The formatting of delimiters is separated into the inner and outer formatting. Delimiters should be symmetrical in their inner formatting. Examples:

If the code block is written on a single line, include a space between the delimiters and the inner content. Exception: brackets, when the inner content is simple (see Math).

Example:

\begin{equation}
  \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
\end{equation}

Delimiters on their own line should have the same indentation level. Delimiters do not need to be symmetrical in their outer formatting.

When inner content is on its own line, it should be indented once more than the delimiters. As an exception, consecutively nested delimiters are allowed to be on the same indentation level. They can also share the same line (separated by a space) if that is more meaningful.

Example:

\[
  f \colon \left\{ \begin{array}{ccc}
    X & \to & Y \\
    x & \mapsto & f(x).
  \end{array} \right.
\]

Environment options

Sometimes an environment allows for extra arguments. These should be formatted as a regular command with multiple arguments (see Long and multiple arguments):

\[
  \begin{tikzpicture}[scale=1, ...]
    ...
  \end{tikzpicture}
\]

Local commands

Sometimes, we need to call some commands inside of an environment. The typical example of this is giving a label. These commands are placed after the opening delimiter, at the same indentation level. These commands most likely do not directly produce visual content, and should be each given their own line.

Example:

\begin{equation}
\label{E:linear-system}
  A x = b
\end{equation}

See Labels for more guidelines about label names.

Exceptions:

Lists

Lists and enumerations have some special rules.

Example:

A smaller list:
\begin{itemize}
  \item Item 1
  \item Item 2
\end{itemize}
and larger list:
\begin{itemize}
\item[\bullet] \label{I:1}
  Use this formatting when the item is quite long, say over 79 characters, or
  when it starts wrapping in your editor.
\item
  Be consistent in each list!
\end{itemize}

Arrays & tables

Arrays and tables are a special kind of code block that encode two-dimensional grid layouts in one-dimensional code. This complexity requires some special attention. The inner content of these code blocks is typically broken up into several lines. In its simplest form, each line corresponds to an outputted row. See Line breaks for details about how to format multiple-line code blocks.

While in general, symbols at line breaks should not be on their own line, we allow an exception for blocks where the items of each row are already on their own lines, in which case the line break can be on its own line, but with one less indentation:

\[
  \begin{array}
    ...
    & ...
    & ...
  \\
    ...
    & ...
    & ...
  \end{array}
\]

~ ˆ ~

Labels

Labels should be of the form <envir>:<name>, where

See Local commands for an example of a labeled environment.

Comments

Comments are normally introduced by a single %. This can be ignored when separating parts of the code with a long comment (e.g. a line of 80 %’s).