1. Formatting Text
  2. Making Lists
  3. Adding Cross-References
  4. Referencing Equations

In this installment of our $\mathrm{\LaTeX}$ tutorial, we discuss formatting text and cross-referencing. We also introduce the amsmath package.

Formatting Text

Let's begin with some simple formatting. Consider the following code:

\section{Formatting text}
\subsection{Bold text}
This is \textbf{really} important. 
\subsection{Italic text}
This is \textit{really} important.
\subsection{Underlined text}
This is \underline{really} important.

Here is what we get:

  • We used the \section command and the \subsection command to break our document up into sections. This allows us to structure our work, and is very useful. One can even go even further and use the \subsubsection command to create subsections of subsections. We will come back to this shortly.
  • To produce bold text, use \textbf{}.
  • To produce italic text, use \textit{}.
  • To produce underlined text, use \underline{}.

As an alternative to using \textit{} for italic text, one can also use the command \emph{}, which is short for “emphasis”. You can think of this as “smart italics”. Roughly speaking, if you apply \emph to normal text, it will italicise it. If you apply \emph to text that is already in italics, \emph  will de-italicise it. If you apply \emph to bold text, it will produce bold italics. Ergo:

This is \emph{really} important. 

\textit{This is \emph{really} important.}

\textbf{This is \emph{really} important.}

This example also illustrates another important point: spacing. Unlike a word processor, $\mathrm{\LaTeX}$ only creates new lines and new paragraphs when you explicitly tell it to. This is what the blank line did—it instructed $\mathrm{\LaTeX}$ to begin the next sentence on a new line. Suppose instead we had simply written:

This is \emph{really} important. 
\textit{This is \emph{really} important.}
\textbf{This is \emph{really} important.}

Then everything would appear on the same line:

So much for bold, italics, and underline. What about[1] strikethrough?

Unlike the bold, italics, and underline, there is no way to do this “out of the box”. We need to use a package. There are several possible packages one can use to do this. The most robust is probably ulem, which we add into the preamble as \usepackage[normalem]{ulem}.

Recall that the syntax for the \usepackage command is \usepackage[options]{name}. Thus in this case we are calling the \ulem package with the normalem option. The reason for this is that the \ulem does more than just enable strikethrough text. It also tweaks the way underlining works (more on this shortly). In particular, by default it changes the \emph command we discussed above so that it underlines text instead. This is somewhat undesirable, and hence the normalem option (which is short for “normal emphasis”) turns this feature off.

Here is a code snippet showing ulem in action.

  1. Note however that there are very few instances in formal mathematical writing where it is appropriate to use strikethrough. Just because you can, doesn't mean you should! ↩︎

This is really \sout{pointless} important.

Note the command to actually invoke strikethrough is \sout (which is short for “strike out”).

Since we're discussing ulem, let us point out one other feature that this package grants us. The \underline command we discussed above leaves a fixed gap between the lowest letter in a word and the line. This means that if a word has a letter with a low-hanging tail (eg. a “g”), then the line appears lower than on a word with no such letters. Take a look at this:

Learning \underline{maths} is \underline{exhilarating}.

Does this matter?

If you are a normal, well-adjusted person, then no, it probably doesn't.

If however you a picky, pedantic person who likes everything to be pixel-perfect, then yes, it is an abomination that needs fixing. The ulem package allows us to do so, via the “improved underline” command \uline.

Learning \uline{maths} is \uline{exhilarating}.

This also illustrates the following general principle in $\mathrm{\LaTeX}$:

There are often multiple ways to achieve the same thing.

Making Lists

Lists come in two forms. We have unordered lists, like this:

  • Apples.
  • Pears.
  • Washing machines.

and ordered lists, like this:

  1. Monday.
  2. A slice of cheese.
  3. Tuesday.

These are handled in $\mathrm{\LaTeX}$ as so:

Lists come in two forms. We have unordered lists, like this:
    \item Apples.
    \item Pears.
    \item Washing machines.
and ordered lists, like this:
    \item Monday.
    \item A slice of cheese.
    \item Tuesday. 

The only difference in the syntax is that unordered lists use \begin{itemize} ... \end{itemize} and ordered lists use \begin{enumerate} ... \end{enumerate}. In both cases each individual list item is prefaced with \item.

Adding Cross-References

Often when writing $\mathrm{\LaTeX}$ one wants to be able to reference something already written. This is done via the \label command and the \ref command.  

\title{My BSc thesis}
Fear is the path to the dark side.
Establish the claim from Section \ref{intro} we will.
\subsection{First step}
Fear leads to anger.
\subsection{Second step}
Anger leads to hate.
\subsection{Third step}
Hate leads to suffering.
\subsection{Fourth step}
Combine the observations from Subsections \ref{1st}, \ref{2nd}, and \ref{3rd}. Completes the proof this does.
\section{Concluding Remarks}
Do. Or do not. There is no try.

As Yoda shows us, referencing a section is a two-stage affair.

  • First add \label{blah} after the section tag you want to reference.
  • Then write \ref{blah} where you want the reference to be.

$\mathrm{\LaTeX}$ magically sorts the numbering out. Thus if you later reorder the sections, or add a new one, the reference \ref{blah} will always point to the section with the \label{blah} tag, even if the numbering changes.

Pro Tip: It doesn't matter what you use as a label. Similarly to naming variables in programming, it is helpful to give the reference a name that will help you to easily identify it later. Thus if, for example, you have a section titled Historical Remarks in your thesis, a label such as \label{historical} would be better than \label{section14} or \label{another-boring-bit}.

Note also in this example we wrote \date{} in the preamble but did not specify an actual date. By leaving it empty we are simply telling $\mathrm{\LaTeX}$ not to print the date.

Yoda had two hierarchical levels in his thesis: sections, and subsections. In fact, $\mathrm{\LaTeX}$ recognises six different levels:

Name $\mathrm{\LaTeX}$ code What it looks like
Part \part{} Part I, Part II, ...
Section \section{} Section 1
Subsection \subsection{} Section 1.1
Subsubsection \subsubsection{} Section 1.1.1
Paragraph \paragraph{} No number
Subparagraph \subparagraph{} No number, indented
  • A \paragraph{} or \subparagraph{} heading will not appear in the Table of Contents (more on this in a later lecture).
  • If one uses a different documentclass then additional options are available. For example, \documentclass{book} has a heading \chapter{} which comes between \part{} and \section{} in the hierarchy.

Here is an example of all of these headings at work.

We begin with a historical digression.
\subsection{The work of Gauss}
Gauss was a wonderful mathematician.
\subsubsection{Gauss's childhood}
He was smarter than you by the time he was 4. 
\paragraph{An anecdote}
When he was seven Gauss wowed his teachers by correctly computing $1 + 2 + \ldots + 99 + 100$ in his head. Can you do this?
\subparagraph{But how?} 
Easy peasy. We just add all the numbers up one-by-one and try not to lose count. \textit{Deep breath.} 1 + 2 = 3, 3 + 3 = 6, 6 + 4 = 10, 10 + 5 = 15, 15 + 6 = \ldots D'oh! Lost count already.

Referencing Equations

It is also useful to be able to number a displayed equation. This will be our first use of the amsmath package.

To reference an equation one needs to slightly change the syntax for displayed math mode. Instead of using \[ ... \] (or any of the other variants), one uses \begin{equation} ... \end{equation}, and then the reference itself is invoked by \eqref.  

The following equation is called \textit{Euler's Identity}:
e^{i \pi} + 1 = 0.
Equation \eqref{nice} is often regarded as ``the greatest equation ever''.  

Again, $\mathrm{\LaTeX}$ sorts the numbering out automatically. Using \eqref instead of \ref surrounds the reference in parentheses, which helps distinguish references to equations from other references.

Note also one other important feature of this last snippet. To write quotation marks in $\mathrm{\LaTeX}$, you cannot just type normal quotation marks: “  ”

Instead, you must type two backticks `` to open the quote, and then two apostrophes '' to close the quote.

By default, equations are numbered consecutively, starting at (1). You may prefer that equations are numbered according to section, so that the second equation in Section 3 is labelled as (3.2). This can be achieved by adding the command \numberwithin{equation}{section} to the preamble. Compare:

The code for the left-hand picture is here:

% \numberwithin{equation}{section} 
% Remove the % from the line above to get the right-hand picture
In this paper we prove that 
2 = \text{window}.
We start from the well-known fact that
1 + 1 = 2.
Next, we recall the visually self-evident fact that
1 + 1 = \text{window}. 
Then by combining \eqref{easy} with \eqref{draw-a-picture} with obtain \eqref{wow}. 

Observe that the third and fourth lines begins with a %. This tells $\mathrm{\LaTeX}$ to ignore those lines. This is a useful way to write “comments” to yourself (and possibly your coauthors) that won't get printed. If one removes the % from the third line then the new command \numberwithin{equation}{section} is seen by $\mathrm{\LaTeX}$, and this results in the numbering seen in the right-hand picture.

Another important thing to notice in this code snippet is that to produce “window” inside a math environment, we had to first re-enter text mode. This is done by \text{}. If we omitted this then the the letters of window would be italic and spaced incorrectly. The command \text{} requires amsmath.

The command \numberwithin{equation}{section} is only scratching the surface. Every single aspect of the numbering can be customised to your heart's content—for instance, you might want to label your equations with hieroglyphs and have them reset each time you reach a section which is divisible by 23[1]. We will discuss these customisations in a later lecture.

  1. Please do not do this. Whoever is responsible for reading your work will not be amused. ↩︎