Math Mode

Now that we have seen some of the basics of LaTeX, it is time to explore its most relevant capability for us - typesetting math. Math expressions are full of specialized symbols, in all sorts of odd heights and sizes, which once made mathematical texts a nightmare for those at the printing press and typewriter. The language of LaTeX allows us to render math with an ease and elegance that might send any manual printer into a jealous rage.

Math mode

In order to write math in LaTeX, we enter an environment called math mode.

Math mode is required for LaTeX to understand the special commands that we will use. Trying to implement most of them outside of this environment will result in an error message.

Just as with text formatting, we use special characters to show where math mode begins and ends.

A woman, representing Arithmetic, writes equations on a tablet.
Writing math in the dark times before TeX [Image from Iconologia, or, Moral emblems by Cesare Ripa (1709).]

Math mode begins with \( and ends with \)

Dollar signs?

You might recall from the previous section, that the dollar sign $ was a way to enter math mode. In the original TeX language, inline math mode was enclosed by single dollar signs and display math mode was enclosed by double dollar signs. These characters work in Overleaf as well, and you will probably see them in many other LaTeX tutorials.

However, I suggest using the LaTeX-specific notation \( \). When using the dollar signs, the opening and closing tags are the same, which can make it difficult to see where math mode begins and ends, and leave room for more errors.

Let's try it out - and start small.

Exercise 4.1.1

Type the following sentence in your project: "Let x be a real number."

Next, try putting a letter x into math mode. Compile the preview again.

What difference do you notice?

Solution 4.1.1

Outside of math mode, with the code

x

the x appears in Roman font (that is, not italic and not bold).

Within math mode, with the code

\( x \)

the x is italicized in the preview.

By convention, variables should always be put in italics. For this reason, any letters typed in math mode are automatically italicized.

Visually, in the PDF, there is no difference between \textit{x} and \( x \). [You may remember that we put variables in \textit{} in Exercise 3.1.2.] However, it is good practice to make the markup reflect the content - since a variable is a mathematical object, it belongs in math mode.

So, whenever you refer to a variable in your writing, be sure to put it in math mode!

Opening and closing tags

Cartoon of the opening and closing tags for math mode waving hello and goodbye.

Just as with the braces of parameters, or the beginning and ending tags for a document, we need both math mode's opening and closing commands to be present and matching.

Some things to watch out for to avoid errors:

  • Order of characters - the backslash comes before each parenthesis.

  • Matching parentheses - the opening and closing must be of the same type. Depending on the size of your text, this could be an easy error to miss if you are not careful to look for it.

Inline and display modes

If you have a math textbook handy, take a look through the pages. You will likely see a mix of expressions included in sentences, and larger ones that are centered on the page.

For example, consider the page from Michael Corral's Elementary Calculus pictured here. In the second image, the expressions highlighted in yellow are typeset in inline math mode. This is the math mode we have already been introduced to, where the math stays in the same line as the text.

The expressions highlighted in pink are in display math mode. Display mode is entered using square brackets instead of parentheses: opening with \[ and closing with \]

This command automatically adds line breaks before and after the expression, and positions it in the center of the page. In addition, certain expressions (such as fractions) are larger in display mode - we will learn more about this later.

Display mode highlights important equations or expressions, making the text easier to parse. The spacing draws the reader's eye to the most important elements.

Display mode begins with \[ and ends with \]

Line breaks in math mode

Another difference between inline and display math mode can be see in expressions spanning multiple lines.

Inline math, since it is integrated into a paragraph, will wrap across multiple lines in the same way that text does.

On the other hand, there are no line breaks in display math. If an expression is too long, it will simply keep going off the edge of the page.

If you'd like to try for yourself, you can use this long sum, shown below. Here is the inline version:

The sum of the first thirty numbers is \(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30\)

And here is the display version:

The sum of the first thirty numbers is \[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30\]

Overleaf flags the line containing the too-long expression with a yellow warning triangle. If you place your cursor over the warning, you will see a message about an "overfull \hbox" - meaning that the line is too long to fit on the page.

If you wish to have multiple lines in display style, you must put each line in a separate set of brackets, \[ \]. Later in this tutorial, we will learn about a better way to organize multiple lines of equations in display mode.

Example

For an example of how both inline and display math mode can be used, let's look at a familiar theorem.

The following code

\textbf{The Pythagorean Theorem.} If a right triangle has a hypotenuse of length \(c\) and two other sides of lengths \(a\) and \(b\), then \[a^{2} + b^{2} = c^{2}\]

results in the following:

Text resulting from the code above. The arrangement of the text is described below.

The variables a, b, and c are first introduced as part of the sentence, using inline math mode. Display mode is used for the key part of the theorem, the Pythagorean equation.

A word of warning

Note that we shouldn't use display mode for every math expression. For example, what would the Pythagorean Theorem typeset above look like if we put all variables in display mode?

Pythagorean theorem with all variables in display mode, as described below.

The sentence is now broken into short fragments. The single characters in display mode are more difficult to read, and the reader must zig-zag through the changing alignment.

In general, use display mode only for important and/or lengthy expressions, and not for single characters.

Exercise 4.1.2

Now, let's use our knowledge of text and math typesetting so far to typeset a theorem you might remember from Algebra.

Euclid's Division Lemma. For any two positive integers a and b, there exist unique integers q and r such that  a = bq + r. The number q is called the quotient and the number r is called the remainder.

Raw text to copy:

Euclid's Division Lemma. For any two positive integers a and b, there exist unique integers q and r such that a = bq + r. The number q is called the quotient and the number r is called the remainder.

Solution 4.1.2

Euclid's Division Lemma can be typeset with the following code:

\textbf{Euclid’s Division Lemma.} For any two positive integers \(a\) and \(b\), there exist unique integers \(q\) and \(r\) such that \[a = bq + r\] The number \(q\) is called the \textit{quotient} and the number \(r\) is called the \textit{remainder}.

Note that the variables are all in math mode. Display mode is used only for the important equation, for better readability.

Now that we know how to enter math mode, let's take a closer look at how to typeset some essential symbols.