Writing Math Formulas
local graph
Chapter 9 — Writing Math Formulas
Section titled “Chapter 9 — Writing Math Formulas”At the beginning of this book, in Chapter 1, Getting Started with LaTeX, we promised that LaTeX offers excellent quality for mathematical typesetting. Now, it’s time to show this. By the end of this chapter, you’ll be able to write clear and elegant mathematical text.
To make full use of LaTeX’s math capabilities, we’ll work through the following topics:
- Writing basic formulas
- Typesetting multi-line formulas
- Exploring the wealth of math symbols
- Building math structures
It’s quite an undertaking, so let’s get started.
Writing basic formulas
Section titled “Writing basic formulas”LaTeX provides three writing modes:
- Paragraph mode: Text is typeset as a sequence of words arranged into lines, paragraphs, and pages. That’s what we’ve used throughout the previous chapters.
- Left-to-right mode: Text is set as a continuous sequence of words without line breaks. For example, the argument of the
\mboxcommand is typeset in this mode, so we can use it to prevent a phrase from being hyphenated. - Math mode: Here, LaTeX treats letters as mathematical symbols. They will appear in italics, as is common practice for variables. Many symbols are available only in math mode, such as roots, summation signs, relation symbols, math accents, arrows, and various delimiters, such as parentheses, brackets, and braces. LaTeX ignores normal space characters between letters and symbols in math mode. Instead, the spacing is determined by the type of symbol, so spacing around relation signs is different from the spacing around opening or closing delimiters. Any mathematical expression must be written in this mode.
Now, we’ll enter the math mode for the first time.
Our first math text shall deal with the solutions of quadratic equations. We will typeset formulas with constants and variables, use superscripts for the squares and subscripts for the solutions, and include a root symbol. Finally, we will add cross-references to formulas. That’s quite a bit to cover, so let’s break it down into the following steps:
- Start a new document. For now, we don’t need any packages:
\documentclass{article}\begin{document}\section*{Quadratic equations}
- State the quadratic equation with its conditions. Use an
equationenvironment for the formula. Surround small pieces of math within text using\(and\):The quadratic equation\begin{equation}\label{quad}ax^2 + bx + c = 0,\end{equation}where \( a, b \) and \( c \) are constants and\( a \neq 0 \), has two solutions for the variable\( x \): - Write another equation for the solutions. The command for a square root is
\sqrt, and for a fraction, use the\fraccommand:\begin{equation}\label{root}x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.\end{equation} - Let’s introduce the discriminant and discuss the case when it’s zero. To get an unnumbered displayed equation, we surround the formula with
\[and\]:If the \emph{discriminant} \( \Delta \) with\[\Delta = b^2 - 4ac\]is zero, then the equation (\ref{quad}) has a double solution:(\ref{root}) becomes\[x = - \frac{b}{2a}.\]\end{document} - Compile the document. The equation references are unresolved in the first run and will appear as
(?). Compile again to let LaTeX resolve them, then look at the final output:
Figure 9.1 – An example of a mathematical text
Figure 9.1 – An example of a mathematical text
As mentioned in Chapter 1, Getting Started with LaTeX, writing formulas often feels similar to programming. We build expressions from commands; some take arguments, such as those for roots and fractions, while others are simple symbol commands, such as those for Greek letters. Most of these symbols work only in math environments, not in regular text. This chapter will help you master it, and the results will be well worth the effort.
The equation environment created a displayed formula. LaTeX centered it horizontally and added some vertical space above and below. Furthermore, it numbered these formulas consecutively.
However, \[ … \] and \( … \) are also, in reality, environments. Let’s look at them more closely in the next sections.
Embedding math expressions within text
Section titled “Embedding math expressions within text”LaTeX provides the math environment for in-text formulas:
\begin{math} expression\end{math}Since it can be tedious to write this environment for each small expression or symbol, LaTeX offers a shorter form that behaves the same:
\( expression\)You can place it all on one line as well, such as \( expression \).
A third way is to use a shortcut from TeX: $expression$. A disadvantage of the latter is that the opening and closing symbols are the same, which may easily lead to mistakes. Still, it’s much quicker to type, which is why it remains popular among LaTeX users, and it’s perfectly okay to use it.
Inline formulas save space and help keep explanations flowing, so they are recommended for short mathematical expressions within the text.
Displaying formulas
Section titled “Displaying formulas”For centered, displayed formulas, LaTeX provides the displaymath environment:
\begin{displaymath} expression\end{displaymath}With this environment, when the paragraph ends, some vertical space follows, then LaTeX displays the centered formula, and adds vertical space afterward. Because this environment already handles spacing, avoid leaving empty lines before or after it, as this would create additional vertical space from the extra paragraph breaks.
There’s also a shorter form for this environment. Here, it’s with square brackets, instead of parentheses:
\[ expression\]Placing the \[ and \] shortcuts on their own lines makes the code easier to read.
In older tutorials and on the internet, you may encounter the TeX form $$...$$ for displayed formulas. Do not use this with LaTeX, as it leads to incorrect vertical spacing.
Such displayed formulas stand out more because they are centered and surrounded by additional space. Choose the style that best supports the readability of your text.
For the rest of this chapter, all pieces of code will use math mode. We will either explicitly enter a math environment or assume we are already in math mode for short code snippets.
Numbering equations
Section titled “Numbering equations”Equations and formulas, in general, may be numbered. However, this applies only to displayed formulas. The equation environment handles this:
\begin{equation} \label{key} expression\end{equation}It looks similar to displaymath, but it’s numbered this time. The number will be displayed in parentheses on the right-hand side of the equation, as shown in Figure 9.1. You can use the key to cross-reference the equation with the \ref command, as we did in the previous chapter. If you don’t need a reference, you can omit the \label command. In general, it makes sense to number only the equations that you plan to reference.
If you load the amsmath package, which we’ll use later, you can write unnumbered equations with the starred equation* environment.
Adding subscripts and superscripts
Section titled “Adding subscripts and superscripts”Since exponents and indices are used so often, LaTeX provides concise commands for them. An underscore, _, creates a subscript, often used as an index:
{expression}_{subscript}A caret, ^, produces a superscript used for exponents:
{expression}^{superscript}As we see here, we use braces to define the relevant part of the expression. In the case of single letters, numerals, or symbols, you can omit the braces.
Subscripts and superscripts may be nested. If you use both subscripts and superscripts in the same expression, the order of ^ and _ doesn’t matter. Let’s look at an example:
\[ x_1^2 + x_2^2 = 1, \quad 2^{2^x} = 64 \]This gives us the following output:

Figure 9.2 – Subscripts and superscripts
Figure 9.2 – Subscripts and superscripts
Notice that the higher-level exponent is smaller than the lower one. When we nest subscripts or superscripts, the inner font size becomes smaller.
The name of the \quad command comes from traditional metal typesetting, where a square piece was inserted to add horizontal space. \quad creates a small space roughly the width of the letter M. The \qquad command produces twice that amount. Both commands are useful for quick spacing adjustments.
Using operators
Section titled “Using operators”Trigonometric, logarithmic, and other analytic and algebraic functions are commonly written with upright Roman letters, in contrast to variables, which are written in italics. Simply typing log would otherwise look like a product of the three variables: l, o, and g. LaTeX provides commands for many common functions, often called operators. Here’s an alphabetical list of the predefined ones: \arccos, \arcsin, \arctan, \arg, \cos, \cosh, \cot, \coth, \scs, \deg, \det, \dim, \exp, \gcd, \hom, \inf, \ker, \lg, \lim, \liminf, \limsup, \ln, \log, \max, \min, \Pr, \sec, \sin, \sinh, \sup, \tan, and \tanh.
You can write the modulo function in two ways: either use \bmod for a binary relation or use \pmod{argument} for a modulo expression in parentheses.
Some operators take subscripts, which appear below the operator in displayed formulas:
\[ \lim_{n=1, 2, \ldots} a_n \qquad \max_{x<X} x \]The output is as follows:

Figure 9.3 – Operators with subscripts
Figure 9.3 – Operators with subscripts
Superscripts would be set above the operator.
When operators are used inline within text, it’s slightly different:
Within text, we have \( \lim_{n=1, 2, \ldots} a_n \)and \( \max_{x<X} x \).The output is now as follows:
Figure 9.4 – Subscripts
That’s to avoid excessive line spacing.
In addition, Figures 9.30 and 9.31 show the positioning and size of subscripts and superscripts for operators.
In Chapter 10, Writing Advanced Mathematics, in LaTeX Cookbook, you can read how to define your own operators and how to fine-tune subscripts and superscripts for perfect alignment.
Typesetting roots
Section titled “Typesetting roots”Our first example in this chapter contained a square root written as \sqrt{value}. Because this command also supports higher-order roots, it accepts an optional argument specifying the order. The complete definition is as follows:
\sqrt[order]{value}Roots may be nested. We can see it in this example:
\sqrt[64]{x} = \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{x}}}}}}This produces the following:

Figure 9.5 – Nested roots
Figure 9.5 – Nested roots
LaTeX automatically adjusts the size of the root symbol to the height and the width of the value expression. This is why outer roots appear larger than inner roots.
Writing fractions
Section titled “Writing fractions”For inline formulas, you may just use a slash, /, to write fractions, such as \( (a+b)/2 \). For larger fractions, use the \frac command:
\frac{numerator}{denominator}Here is an example:
\[ \frac{n(n+1)}{2} \quad \frac{\frac{\sqrt{x}+1}{2}-x}{y^2} \]The output is as follows:

Figure 9.6 – Fractions and nested fractions
Figure 9.6 – Fractions and nested fractions
LaTeX automatically adjusts the separation line to match the width of the numerator and denominator.
Writing Greek letters
Section titled “Writing Greek letters”Mathematicians often use Greek letters, for example, to denote constants. To get a lowercase Greek letter, write the name as a command with a backslash. Here are the lowercase Greek letters with their corresponding LaTeX commands:

Figure 9.7 – Lowercase Greek letters
Figure 9.7 – Lowercase Greek letters
For some letters, variants are available:

Figure 9.8 – Alternative variants for some Greek letters
Figure 9.8 – Alternative variants for some Greek letters
As the omicron looks like the letter o, there’s no command for it. The same applies to most uppercase Greek letters, which look identical to their Roman counterparts. For example, there’s no \Alpha or \Beta commands; just type A or B instead. The uppercase Greek letters that differ from Roman letters can be written as follows:

Figure 9.9 – Uppercase Greek letters
Figure 9.9 – Uppercase Greek letters
You can see that lowercase Greek letters are typeset in italics, and uppercase Greek letters are written upright. This convention reflects traditional mathematical typesetting. The frugality of using only italic lowercase letters and a limited number of upright Greek letters stems from space limitations in character tables in the early days of TeX.
If you want to have upright Greek letters, you may add \usepackage{upgreek} and then use the following commands:

Figure 9.10 – Upright lowercase Greek letters
Figure 9.10 – Upright lowercase Greek letters
The following additional variants are also available:

Figure 9.11 – Alternative upright variants for some Greek letters
Figure 9.11 – Alternative upright variants for some Greek letters
The upright Greek letters are taken from the Euler font and not from the default Computer Modern fonts.
Writing script letters
Section titled “Writing script letters”For the 26 uppercase letters A, B, C, …, Z, there’s a calligraphic shape, produced by \mathcal:
\[ \mathcal{A}, \mathcal{B}, \mathcal{C}, \ldots, \mathcal{Z} \]This is how they look:

Figure 9.12 – Calligraphic letters
Figure 9.12 – Calligraphic letters
Several packages offer alternative calligraphic fonts, such as zapfino and xits.
Producing an ellipsis
Section titled “Producing an ellipsis”You already know that the \ldots command produces a low ellipsis. It also works in math mode. We use the low ellipsis mainly between letters and commas. Between operation and relation symbols, a centered ellipsis is more common. Also, a matrix may require a diagonal or a vertical ellipsis. Here’s how we can produce them:

Figure 9.13 – Ellipsis in various positions
Figure 9.13 – Ellipsis in various positions
For a diagonal ellipsis in the opposite direction, you can write \reflectbox{$\ddots$}. The \reflectbox command requires \usepackage{graphicx} in your preamble.
Changing the font, style, and size
Section titled “Changing the font, style, and size”In Chapter 2, Formatting Text and Creating Macros, we looked at ways to modify the text font. In math mode, we can use additional commands to change the font style, as follows:

Figure 9.14 – Math font commands
Figure 9.14 – Math font commands
For example, once you add \usepackage{dsfont} to your document preamble, you can write \mathds{Z} to get a double-stroke letter Z.
Although letters in math mode are italicized by default, LaTeX treats them as separate symbols, resulting in a different spacing than in an italic word. For instance, in math mode, fi may represent the product of the f and i variables but not the fi ligature. Compare these two versions:
\(Definition\) and \textit{Definition}This gives us the following:

Figure 9.15 – Plain math writing versus italic text
Figure 9.15 – Plain math writing versus italic text
The version on the right is clearly better.
Also, \textit typesets its arguments using the italic math font, not the standard text font. For text within formulas, please read the Inserting text into formulas section later in this chapter.
To make an entire math expression bold, use the \boldmath declaration before the expression, that is, outside math mode. The \unboldmath declaration switches back to the standard typeface. The latter is also used outside math mode.
To make only a part of a formula bold, you can switch to left-to-right mode with the \mbox command and use \boldmath in its argument.
Four different math styles are available, which determine the typesetting and font size. You can use these commands within math mode:
\textstyleenforces inline style for symbol sizes and positioning of subscripts and superscripts, even when used in a displayed formula\displaystyletreats symbol sizes, subscripts, and superscripts as in a displayed formula, even when used inline in text\scriptstyleswitches to a smaller font size, such as for subscripts and superscripts\scriptscriptstyleuses a much smaller font size for the nested script style
\textstyle differs from \displaystyle in mainly two ways: with \textstyle, variable-sized symbols are smaller, and subscripts and superscripts are usually placed beside the expression instead of below and above, respectively. Otherwise, the font size is the same.
LaTeX switches the style automatically. If you write a simple exponent, it will be typeset in script style with a smaller font size.
You may force the desired style using one of the four commands listed here. So, for instance, you can insert \displaystyle into a formula, so even within the text, it would appear like in a displayed formula: bigger fraction and bigger sum signs. Furthermore, subscripts are placed below, and superscripts are placed above. Note, this increases the line spacing.
Customizing displayed formulas
Section titled “Customizing displayed formulas”Two document class options can change how displayed formulas appear:
fleqnfor flush left equations: LaTeX will align all displayed formulas at the left marginleqnofor left equation numbers: all numbered formulas will get the numbers on the left side instead of the right
Equations and formulas are often part of a larger structure rather than standing alone. We may run into situations such as the following:
- A formula is too long to fit on one line
- Several formulas need to be listed one after another
- An equation must be transformed step by step
- A chain of inequalities spans multiple lines
- Several formulas must be aligned at their relation symbols
We may also encounter situations where we need to write multi-line equations, often with some form of alignment. The amsmath package provides specialized environments for almost all such situations, which will be the topic of our next section.
Typesetting multi-line formulas
Section titled “Typesetting multi-line formulas”We’ll use the amsmath package to typeset a very long formula and a system of equations:
- Start a new document. Here, we use A6 paper size to work with a smaller text width:
\documentclass{article}\usepackage[a6paper]{geometry}
- Load the
amsmathpackage, and begin the document:\usepackage{amsmath}\begin{document} - Use the
multlineenvironment to spread a long equation over three lines. End each line with a double backslash,\\, except the last one:\begin{multline}\sum = a + b + c + d + e \\+ f + g + h + i + j \\+ k + l + m + n\end{multline}\end{document} - Compile and look at the equation:

Figure 9.16 – A formula spanning three lines
Figure 9.16 – A formula spanning three lines
- Now, we handle a system of equations. Use the
gatherenvironment to add these equations. Again, end all lines with\\, except the last one:\begin{gather}x + y + z = 0 \\y - z = 1\end{gather} - Compile again, and look at the equations, which appear horizontally centered to each other:

Figure 9.17 – A system with two equations
Figure 9.17 – A system with two equations
- Equation systems are often aligned at the equal sign. Let’s do this. Use the ampersand symbol,
&, to mark the point where we wish to align:\begin{align}x + y + z &= 0 \\y - z &= 1\end{align} - Compile again; now the equations are aligned as desired:

Figure 9.18 – A system with two aligned equations
Figure 9.18 – A system with two aligned equations
Because we loaded the amsmath package, we now have access to several multi-line math environments. Each line in such an environment is ended by \\, except the last one. Otherwise, if we add \\ to the last line, LaTeX would assume a new line and number it, even if the line is empty.
The alignment depends on the environment. Here’s a list of the amsmath multi-line environments:
multline: The first line is left-aligned, the last line is right-aligned, and all others are centeredgather: Each line is centeredalign: Use&to mark a symbol where you want to align the formulas; use another&symbol to end a column if you need several aligned columnsflalign: This is similar toalign, but the columns are flushed to the left and the right margin, respectivelyalignat: This allows alignment at several places, each of which must be marked with&split: This is similar toalign, but can be used within another math environmentaligned,gathered, andalignedat: These are used for an aligned block within amathenvironment, which can either be displayed math or inline math
The last environments, including split, are often placed inside an outer equation environment so that several lines share a single equation number.
Numbering can be adjusted as follows.
Numbering lines in multi-line formulas
Section titled “Numbering lines in multi-line formulas”In multi-line math environments, each line is numbered just like a regular equation. If you want to suppress the number of a specific line, place the \notag command before the end of the line. It’s often best to number only the lines you plan to reference.
If you prefer a particular style of numbering, such as a symbol or name as a tag for a formula, you can use the \tag command, such as \tag{$\star$} to mark it with a star, or \tag{name} for tagging it as (name).
If you would like to avoid numbering altogether, use a starred version, such as align* or gather*.
Inserting text into formulas
Section titled “Inserting text into formulas”To place regular text into a formula, standard LaTeX provides the \mbox{text} command. The amsmath package adds more flexible options:
\text{words}inserts words in regular text font within a math formula and adjusts its size to the current math style.\textproduces smaller text within subscripts or superscripts.\intertext{text}suspends the formula, prints the text as its own paragraph, then resumes the multi-line formula while preserving the alignment. This is useful for longer annotations.
These commands are helpful when you want to include text within math environments.
Let’s now look into mathematical symbols.
Exploring the wealth of math symbols
Section titled “Exploring the wealth of math symbols”Let’s go beyond writing variables and basic math operators. Mathematical writing often requires a wide range of symbols: relation signs, unary and binary operators, function-like operators, sum and integral symbols, arrows, and many others. LaTeX and additional packages provide thousands of symbols for these purposes.
In this section, we’ll look at a selection of math symbols and the commands that produce them. We’ll cover many of the symbols available in standard LaTeX; the latexsym package adds a few more. Further packages, such as the amssymb package, offer an even larger collection.
Binary operation symbols
Section titled “Binary operation symbols”Along with plus and minus, LaTeX supports a variety of other binary operators:

Figure 9.19 – Binary operation symbols
Figure 9.19 – Binary operation symbols
You need to include \usepackage{latexsym} in your document preamble to use the symbols in the last row.
Relation symbols
Section titled “Relation symbols”Values of expressions might be equal, in which case you just need an equal sign, but many other relations are possible. For example, objects may be congruent, parallel, or they might have any other relation:

Figure 9.20 – Binary relation symbols
Figure 9.20 – Binary relation symbols
You can negate any relation by inserting \not before it. So, for not equivalent, use \not \equiv to create a crossed-out \equiv symbol.
Inequality relation symbols
Section titled “Inequality relation symbols”If expressions are not equal, we can express that in several ways. The simplest are the basic < and > symbols, but there are also relations such as less-than-or-equal and greater-than-or-equal:

Figure 9.21 – Inequality relation symbols
Figure 9.21 – Inequality relation symbols
Here, \neq looks precisely like what you get when you write \not=, as in the previous section.
Subset and superset symbols
Section titled “Subset and superset symbols”LaTeX provides many symbols for describing how sets relate to one another:

Figure 9.22 – Subset and superset symbols
Figure 9.22 – Subset and superset symbols
Again, you can use \not to negate such a set relation.
Arrows
Section titled “Arrows”LaTeX provides a wide range of arrow symbols:

Figure 9.23 – Arrows
Figure 9.23 – Arrows
Arrows are used in many contexts: for implications, mappings between sets, limits, sequences, vector notation, and various descriptive expressions.
Harpoons
Section titled “Harpoons”LaTeX provides a special kind of arrow symbol called harpoons, which look like one-sided arrowheads:

Figure 9.24 – Harpoons
Figure 9.24 – Harpoons
Harpoons are used, for example, in chemical reaction formulas.
Symbols derived from letters
Section titled “Symbols derived from letters”Mathematics uses various symbols that resemble stylized letters:

Figure 9.25 – Symbols derived from letters
Figure 9.25 – Symbols derived from letters
Mathematicians often use \in, \forall, and \exists in statements.
Miscellaneous symbols
Section titled “Miscellaneous symbols”Here are some LaTeX symbols that do not fit into the categories mentioned previously:

Figure 9.26 – Additional LaTeX symbols
Figure 9.26 – Additional LaTeX symbols
The Comprehensive LaTeX Symbol List lists over 20,000 symbols sorted into categories in one single PDF document. If you need to look up a symbol, this document is the best place to start. With TeX Live, you can open it at the command prompt as follows:
texdoc symbolsOr you can visit https://texdoc.org/pkg/symbols to browse it.
Another fascinating approach is handwritten symbol recognition. You draw a symbol with the mouse or your finger on a touchscreen, and the software tries to recognize it and tells you its code. Let’s have a quick look:
- Visit https://detexify.kirelabs.org.
- Draw the symbol in the white box. It doesn’t matter if it’s a shaky mouse sketch, like this:

Figure 9.27 – Handwritten symbol
Figure 9.27 – Handwritten symbol
- After a moment, the tool shows suggestions for symbols along with the corresponding LaTeX commands:

Figure 9.28 – Symbol and code suggestions
Figure 9.28 – Symbol and code suggestions
Detexify also provides a name-based search. Click the Symbols button at the top, enter a phrase into the filter, and Detexify will show symbols and commands that match your query.
Writing units
Section titled “Writing units”When writing units in text, they should not look like variables. For instance, m for meters should not look exactly like an m variable, and s may stand for seconds, but not for an s variable. A typographical convention is to use an upright font shape for units, while variables are written in italics. It’s also common to place a thin space between the value and the unit. So, for 10 meters, you may write 10\,\mathrm{m}. In physics, chemistry, and engineering, we have much more complex units, such as meters per second squared (m/s²) for acceleration. The siunitx package supports correct and consistent typesetting of such units. It requires reading some documentation before you can use it, but it’s worth the effort. Run texdoc siunitx at the command prompt or visit https://texdoc.org/pkg/siunitx.
Variable-sized operators
Section titled “Variable-sized operators”For sums, products, integrals, and set operations, LaTeX provides operator symbols that adjust their size automatically: they appear bigger in display style and smaller in text style.

Figure 9.29 – Variable-sized operators
Figure 9.29 – Variable-sized operators
Here’s an equation in text style:
\( \int_a^b \! f(x) \, dx = \lim_{\Delta x \rightarrow 0} \sum_{i=1}^{n} f(x_i) \,\Delta x_i\)This code gives us the following:

Figure 9.30 – Inline text style equation
Figure 9.30 – Inline text style equation
And here’s the same equation in displayed style:
\[ \int_a^b \! f(x) \, dx = \lim_{\Delta x \rightarrow 0} \sum_{i=1}^{n} f(x_i) \,\Delta x_i\]This time, we get the following:

Figure 9.31 – Displayed style equation
Figure 9.31 – Displayed style equation
As you can see, the operator symbols are noticeably larger in displayed style.
Variable-sized delimiters
Section titled “Variable-sized delimiters”Delimiters such as parentheses, brackets, and braces can change size depending on the expression they enclose. The following are such LaTeX delimiters:

Figure 9.32 – Variable-sized delimiters
Figure 9.32 – Variable-sized delimiters
LaTeX provides special commands to adjust them automatically. If you place a \left or \right command right before such a delimiter, LaTeX automatically matches its size to the height of the enclosed expression. We have to use these size macros in pairs. To match a pair, if you don’t want a second delimiter, use \left. or \right. to get an invisible delimiter on one side.
Automatically adjusting delimiters is especially useful for larger structures, such as matrices, so let’s look at those next.
Building math structures
Section titled “Building math structures”Variables and constants are straightforward, but mathematical writing often involves more complex objects, such as binomial coefficients, vectors, and matrices. In this section, we’ll look at how to typeset such structures.
Let’s begin with simple arrays.
Creating arrays
Section titled “Creating arrays”To arrange mathematical expressions inside a larger expression, you can use the array environment. It works much like a tabular environment, but it’s for math mode, and all its entries are written in math mode as well.
For example, you can place an array inside variable-sized parentheses:
\[ A = \left( \begin{array}{cc} a_{11} & a_{12} \\ a_{21} & a_{22} \end{array} \right)\]This produces a matrix:

Figure 9.33 – A simple array
Figure 9.33 – A simple array
There are also dedicated commands for matrices, which we’ll look at next.
Typesetting matrices
Section titled “Typesetting matrices”The amsmath package provides several matrix environments. A standard matrix can be produced with the pmatrix environment:
\documentclass{article}\usepackage{amsmath}\begin{document}\[ A = \begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{pmatrix}\]\end{document}This yields the following:

Figure 9.34 – A simple matrix
Figure 9.34 – A simple matrix
You may notice that the parentheses sit closer to the matrix entries than in the array example in the previous section. This tighter spacing is part of the amsmath style.
Here are the amsmath matrix environments and their delimiters:
matrix: No delimiterspmatrix: Parentheses,( )bmatrix: Square brackets,[ ]Bmatrix: Braces,{ }vmatrix:| |Vmatrix:|| ||smallmatrix: More compact, no delimiters; you can add them manually, if needed
The compact smallmatrix environment is useful for matrices that appear within regular text.
Writing binomial coefficients
Section titled “Writing binomial coefficients”You could write binomial coefficients using an array together with delimiters, but the amsmath package provides a shorter and cleaner command: \binom for binomial coefficients. Try this:
\binom{n}{k} = \frac{n!}{k!(n-k)!}The result is as follows:

Figure 9.35 – A binomial coefficient in an equation
Figure 9.35 – A binomial coefficient in an equation
This syntax is much simpler than using an array or a matrix for such a small expression.
Underlining and overlining
Section titled “Underlining and overlining”The \overline command puts a line above its argument:
\overline{\Omega}This produces the following:

Figure 9.36 – An overlined omega symbol
Figure 9.36 – An overlined omega symbol
The corresponding command for placing a line below is \underline.
You can also use braces instead of lines. The \underbrace and \overbrace commands create braces below or above an expression:
N = \underbrace{1 + 1 + \cdots + 1}_nThis gives the following:

Figure 9.37 – An underbrace below an expression
Figure 9.37 – An underbrace below an expression
A subscript placed on an underbrace is written below it, and a superscript placed on an overbrace appears above.
Setting accents
Section titled “Setting accents”In Chapter 2, Formatting Text and Creating Macros, we’ve already seen how to write accents in text mode. For math mode, we need different commands. We can apply them to any letter. Here’s the list of math accents shown using the lowercase letter a as an example:

Figure 9.38 – Various math accents
Figure 9.38 – Various math accents
Extensible accents, also called wide accents, automatically adjust to the width of their arguments.
Putting a symbol above or below another one
Section titled “Putting a symbol above or below another one”Beyond the array environment, amsmath provides commands for stacking expressions directly:
\underset{expression below}{expression}places one expression below another, using subscript-sized text\overset{expression above}{expression}puts an expression above another, using superscript-sized text
This is an example of how we can use these commands:
\underset{\circ}{\cap} \neq \overset{\circ}{\cup}This gives us the following:

Figure 9.39 – Putting a symbol above or below another one
Figure 9.39 – Putting a symbol above or below another one
Another handy command is \stackrel{expression above}{relation}. See the following formula, for example:
X \stackrel{\text{def}}{=} 0This results in the following:

Figure 9.40 – Text above a relation symbol
Figure 9.40 – Text above a relation symbol
\stackrel puts an expression above a relation symbol.
Writing theorems and definitions
Section titled “Writing theorems and definitions”LaTeX provides dedicated environments for theorems, definitions, and similar statements. Returning to our first example in this chapter, we could use the \newtheorem command to define a Theorem environment, thm, as follows:
\newtheorem{thm}{Theorem}Next, we can declare a Definition environment. Let’s call it dfn here:
\newtheorem{dfn}[thm]{Definition}We can use an optional argument referring to an existing environment (in this case, thm). This makes the new environment use the same counter as the already existing environment. In our case here, this means that after Theorem 1 follows Definition 2.
We can use these environments as follows:
\begin{dfn} A quadratic equation is an equation of the form \begin{equation} \label{quad} ax^2 + bx + c = 0, \end{equation} where \( a, b \) and \( c \) are constants and \( a \neq 0 \).\end{dfn}\begin{thm} A quadratic equation (\ref{quad}) has two solutions for the Variable \( x \): \begin{equation} \label{root} x_{1,2} = \frac{-b \pm \sqrt{b^2-4ac}}{2a}. \end{equation}\end{thm}Take a look at the output:

Figure 9.41 – A definition and a theorem
Figure 9.41 – A definition and a theorem
In the output, such environments are numbered and labeled Definition and Theorem, respectively. In Chapter 11, Developing Large Documents, we will use this approach to create a complete document containing definitions, theorems, and lemmas.
There are two special packages offering much more flexibility:
amsthmprovides several styles, allows fine-grained customization, and includes a proof environmentntheoremoffers similar features but handles traditional quod erat demonstrandum (what was to be shown) end marks for proofs in a better way
If you would like to use such environments, look at their documentation and compare the relevant features to decide which package is best for you. As usual, execute texdoc amsthm and texdoc ntheorem at the command prompt, or visit https://texdoc.org/pkg/amsthm and https://texdoc.org/pkg/ntheorem.
Choose one of those closely related packages; don’t load both.
Extended math tools
Section titled “Extended math tools”Take a look at all the mathematics typesetting options in amsmath by entering texdoc amsmath at the command line or visiting https://texdoc.org/pkg/amsmath.
The mathtools package extends amsmath and adds many features. If you are looking for something that standard LaTeX or amsmath doesn’t offer, mathtools is the next place to check. Here are some of its features:
- Tools for fine-tuning mathematical typesetting, such as more compact superscript styles
- Vertical alignment of limits for consecutive operators
- Adjusting the width of operators
- Improved control over tags, including modifying their appearance and only showing tags for equations that have been referenced
- Extensible symbols – more arrows with automatic width adjustment, as well as extensible brackets and braces to be set under or over expressions
- New math environments for more flexible matrices, cases, improved multi-line formulas, and arrows between aligned expressions
- Tighter spacing for shorter intertext
- Declaration of paired delimiters
- Additional symbols, such as a vertically centered colon, along with combinations of relation symbols with colons and shortcuts for auto-sized parentheses
- Techniques for spreading lines in multi-line formulas, setting left subscripts and superscripts, typesetting math within italicized text, and creating multi-line fractions
Have a look at the documentation of this valuable package and find out which commands can be applied to achieve the styles and alignments listed here. Open it by running texdoc mathtools at the command line or go to https://texdoc.org/pkg/mathtools.
In Chapter 10, Writing Advanced Mathematics, in LaTeX Cookbook, you can find a lot of examples showing enhancements with the mathtools package. Visit https://latex-cookbook.net/tag/mathematics/ to see and run examples online that include fine-tuning math formulas, automatic line breaking in equations, plotting functions, and drawing diagrams and geometry pictures.
Summary
Section titled “Summary”You can now write complex math formulas, and you’ve got the essential tools for writing scientific texts. We worked with the amsmath package, which provides us with many features tailored to traditional mathematical typesetting.
You can find further code examples in Chapter 10, Writing Advanced Mathematics, of LaTeX Cookbook, with compilable code on the book’s website at https://latex-cookbook.net/chapter-10.
At this point, you can now fine-tune math expressions, align and number equations, and work with a wide range of mathematical symbols from various symbol fonts. In the next chapter, we’ll look at fonts in general.