## Special Characters

The backslash key \ is used to tell $\mathrm{\LaTeX}$ to expect a command. There are other special keys too—for instance, $ tells$\mathrm{\LaTeX}$to expect maths. But what if you actually want to type a dollar sign? The trick is to type \$. In this case the \ tells $\mathrm{\LaTeX}$ to treat the dollar sign as a normal character, rather than as a math mode entry command. This idea works with all special characters, apart from with the backslash itself.

There are ten special characters in total. Here they are:

Special character What does it do? How to print it
\ Starts a command \textbackslash
$ Enters/exits math mode \$
% Comment \%
^ Superscript \^
_ Subscript \_
{ and } Surrounds characters \{ and \}
& Used in multi-line equations \&
~ Unbreakable space \~
# Used when defining macros \#

We have already met most of these, but let us clarify a few more important points:

• The backslash key is the exception. You cannot print a backslash by typing \\. This is because \\ instead tells $\mathrm{\LaTeX}$ to start a new paragraph. More on this below.
• The % sign tells $\mathrm{\LaTeX}$ to ignore the rest of the current line. This is best used at the start of a line to mark that line as a “comment”.
• The { and } characters are used to surround multiple characters that $\mathrm{\LaTeX}$ should treat as a single character. This is mainly used inside math mode in tandem with superscripts and subscripts. Surrounding a single character with { and } has no effect: both $x^{2}$ and $x^2$ print $x^2$.

Of the three remaining special characters, & and ~ will be discussed today. We will come back to # in a few lecture's time when we discussing creating macros.

## Line Breaks and Spacing

$\mathrm{\LaTeX}$ usually decides by itself where to put line breaks. It's mostly pretty good, but it sometimes gets it wrong. The ~ command is used to tell $\mathrm{\LaTeX}$ it may not make a line break at this space.

This is useful when writing people's initials. For instance, in the following document, the linebreak after the first “R.” in “George R. R. Martin” is confusing:

\documentclass{article}
\title{Calulus in Westeros}
\date{297 AC}
\begin{document}
\maketitle
We prove the mass-energy relationship $E = mc^2$ does \textit{not} hold in George R. R. Martin's mystical world, and explore several implications of this.
\end{document}

So to fix this we can replace the spaces with unbreakable spaces:

\documentclass{article}
\title{Calulus in Westeros}
\date{297 AC}
\begin{document}
\maketitle
We prove the mass-energy relationship $E = mc^2$ does \textit{not} hold in George~R.~R.~Martin's mystical world, and explore several implications of this.
\end{document}


Unfortunately this has the undesirable effect that “Martin” is now hyphenated:

When faced with a problem like this, usually the best thing to do is simply to rewrite your sentence slightly:

\documentclass{article}
\title{Calulus in Westeros}
\date{297 AC}
\begin{document}
\maketitle
We prove the mass-energy relationship $E = mc^2$ does \textit{not} hold in the world of George~R.~R.~Martin's stories, and explore several implications of this.
\end{document}


If you are feeling really stubborn however, there are several other things you can try:

• The command \linebreak tells $\mathrm{\LaTeX}$ it must break a line here.
• The command \nolinebreak tells $\mathrm{\LaTeX}$ it may not break a line here.
• As a last resort, you can surround text that you don't want to be broken up inside an invisible box, like this: \mbox{George R. R. Martin}. This will prevent $\mathrm{\LaTeX}$ from breaking up the expression “George R. R. Martin” at all. Warning: This will often mess up the spacing and cause things to stick out into the margin. Not recommended unless you're desperate.

There are several ways to start a new line in $\mathrm{\LaTeX}$:

• Leave a blank line. We already saw this last lecture. This also starts a new paragraph (i.e. the next line is indented).
• Use the \\ command. This also starts a new paragraph (i.e. the next line is indented).
• Use the \newline command. This does not start a new paragraph (i.e. the next line is not indented).
• Use the two commands \hfill and \break.  The command \hfill inserts as much horizontal space as it can (in this case, until the end of the line). Then \break ends the current line. This does not start a new paragraph (i.e. the next line is not indented).
• If you want to leave an entire blank line between two paragraphs you can combine these commands. For instance, ending a paragraph with \\ and then leaving a blank line results in an empty line between paragraphs.
• Alternatively you can use the \vspace{} command to leave vertical space. The amount of space you leave is dictated by what value you give it. For instance, \vspace{2cm} leaves a 2cm gap. The command \hspace{} works in the same way, only it leaves a horizontal space.

Here are some of these commands in action:

\documentclass{article}
\begin{document}
Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes.

Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes. \\
Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes. \newline
Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes. \hfill \break
Blah blabla bla. Beep beep. Miao. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes. \\

Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes.

\vspace{5pt}

Blah blabla bla. Beep beep. Miaow. Yada yada boooooring. Some more random text. Boop. Woof moo. Much nonsense. Yes.
\end{document}


The \vspace{} command allows for finer control, but it requires a specific length to be inputted. There are seven different units that $\mathrm{\LaTeX}$ understands. They are:

Unit Meaning
mm millimetre
cm centimetre
in inch
pt a “point” (≈ 1/3 mm)
ex ≈ the height of an “x”
em ≈ the width of an “M”
mu 1/18 of an em

## Multi-Line Equations and Matrices

Not all equations are as short and snappy as $e^{i \pi}+1 = 0$. Sometimes the equation you wish to type is too long to fit on a single line, and you need to split it onto multiple lines. Alternatively, sometimes you might want to display several lines of working.

The amsmath package we discussed last time allows for such advanced wizardry. It offers several different solutions, the most useful of which are align and multiline.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Here is an interesting computation:
\begin{align}
e^{i \pi} + 2 & = (e^{i \pi} + 1) + 1 \\
& = 0 + 1 \\
& = 1.
\end{align}
\end{document}

The salient points are:

• The \begin{align} ... \end{align} commands replaces the \begin{equation} ... \end{equation} commands.
• The \\ starts a new line of the equation.
• The & character tells $\mathrm{\LaTeX}$ how to align the equations. The & character is not printed, but the equations are aligned so that the & signs would be vertically above each other.
• Each line of the align environment is numbered. To later reference a line, add a \label command to that particular line. If you don't want a particular line to have a number, you can add \nonumber to that line. Alternatively, if you don't want any of the equations to be numbered, use the command \begin{align*} ... \end{align*}.

Here is a more complex example.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Here is an interesting computation:
\begin{align}
e^{i \pi} + 2 & = (e^{i \pi} + 1) + 1 & \text{using properties of addition,} \nonumber \\
& = 0 + 1 & \text{using Euler's Identity,} \nonumber \\
& = 1 & \text{using arcane magic.} \label{nice}
\end{align}
We leave it as an exercise to the reader to generalise \eqref{nice} to the statement $e^{i \pi} + n = n-1$.
\end{document}


Note in this case there are two & signs on each line, and only the third equation is numbered.

If you only have one equation, but it is too long to fit on one line, use multiline instead.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Here is a random function.
\begin{multline}
\label{fred}
f(t) = 324t^{232} + 53593845t^{11} - \sin t - \frac{1}{\cos t}34252 \sqrt{t} \\
+ \log 5.3340t^t + ( 84392423t^{-25653} - \pi)t^{-3}.
\end{multline}
We call the function \eqref{fred} the \textbf{Fred function}. There is no reason for this.
\end{document}


In this example we also used some common operators: \sin, \cos , \exp and \sqrt{}.

Another useful amsmath command is the cases environment. Here is an example.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\begin{document}
Let $f,g \colon [0,1] \to \mathbb{R}$ be two continuous functions such that $f(1) = g(0)$. Then the function $h \colon [0,1] \to \mathbb{R}$ defined by
$h(t) = \begin{cases} f(2t), & 0 \le t \le \frac{1}{2}, \\ g(2t-1), & \frac{1}{2} \le t \le 1, \end{cases}$
is also continuous.
\end{document}

• Unlike the align command, the cases environment does not replace the display math environment, but rather goes it.
• The syntax is similar to align: the & character indicates how the equations should be aligned. The \\ command starts a new line.
• Note in this example we also need the amssymb package because of the $\mathbb{R}$.
• We used the command \colon when specifying the domain and range of the functions $f$ and $g$. It is not enough to just write a normal colon.

Finally, let us briefly discuss matrices, since the syntax is again similar.

1. Try it for yourself—there is a difference. (Okay, perhaps I am being a little pedantic here...) ↩︎

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Consider the matrix
$M = \begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 2 & 1 \\ 0 & 2 & 0 & 0 \\ 1 & 1 & 0 & 0 \end{pmatrix}$
Then
$M^3 = \begin{pmatrix} 2 & 7 & 2 & 3 \\ 7 & 2 & 12 & 7 \\ 2 & 12 & 0 & 2 \\ 3 & 7 & 2 & 2 \end{pmatrix}$
\end{document}

• The pmatrix environment produces a matrix with round brackets. If you want one with square brackets, use bmatrix instead.
• The & sign is used to separate the cells. The \\ starts a new line.
• You don't need to specify the size of the matrix—$\mathrm{\LaTeX}$ will work it out automatically by how many & and \\ you use. It will throw an error, however, if your first row has four entries and the second row has twelve.
• Fun Fact: The computation of $M^3$ was the “amazing” problem that Matt Damon solved in the movie Good Will Hunting.