You are on page 1of 24

Why should you use LATEX?

We’re all great at Matlab now, so it’s time for something new.

LATEXis as much a programming language as it is a typesetting


language.
• LATEX is the standard tool for typesetting documents that use
mathematics.
• It will (try to) format figures and documents for you.
• LATEX is especially good at referencing and bibliographies.
• Code is saved as text files, making it highly portable.
• It can be used for more than just reports – it’s quite good at
making presentations!
What isn’t LATEX?

• LATEX is not a word processor, and isn’t used the same way.
• Work is done in text files, which LATEX processes entirely and
decides how best to typeset the document.
• The output is either a .dvi or .pdf file. A .dvi file can easily be
converted to .ps or .pdf files.
Getting started with LATEX

Installing LATEX on your computer can be a challenge.

Everything you need can be found here:


https://www.latex-project.org/get/#tex-distributions

I recommend using MacTeX if you’re running a Mac, and MikTeX


if you’re running Windows. Here are some installation guides:
• Windows:
https://www.latex-tutorial.com/installation/
• Mac: https://www.tug.org/mactex/

If you have trouble, start by signing up for Overleaf:


(https://www.overleaf.com/), which is an online
implementation. Eventually, you’ll want your own copy though.
Getting started with LATEX

• Let’s create a document:

\documentclass{article}

\begin{document}
Hello, world!. $1 + 1 = 2$.
\end{document}

• Make sure that the build box either has LaTeX =⇒ DVI or
LaTeX =⇒ PDF.
• Build your document. Something should have just happened...
What exactly just happened?

• LATEX interpreted the information you wrote, and compiled it


into a document.
• The first section registered as text, while the section
surrounded by $ symbols registered as an equation.
• You should see a console window with something like this at
the bottom:
LaTeX-Result: 0 Error(s), 0 Warning(s), 0 Bad
Box(es), 1 Page(s)
• Errors will sometimes cause compiling the document to fail
entirely. These typically need to be addressed.
• Warnings and bad boxes tend to be less serious, and can
normally be disregarded if you can’t see any problems.
The structure of a document

A LATEX document has two parts:

• Preamble: This part is before the main document, and


contains instructions for LATEX regarding how the document
should be handled. It tells LATEX what format the document is
supposed to be, what packages should be loaded, what
commands need to be defined, etc.

• Body: This part is where the text of your document is


located. LATEX will try to display this in a way that is
consistent with the prescribed document format.
Title Box

LATEX can be used to set up a title box:

\documentclass[12pt]{article}
\include{amsmath}
\include{amssymb}
\include{graphicx}

\title{My Sample Document}


\author{H. W. Jones Jr.}
\date{\today}

\begin{document}
\maketitle
Hello, world!. $1 + 1 = 2$.
\end{document}

The first few lines are useful command packages. We’ll talk about
those next lecture. For now, include them in every document.
Sections and Subsections

You can break documents up into many smaller sections:


• \section{...}
• \subsection{...}
• \subsubsection{...}
• \paragraph{...}
• \subparagraph{...}
Sections and Subsections

Let’s take our document from before, and adjust the body to
include this:

\begin{document}
\maketitle

\section{In Which Our Hero Sets Forth On A Quest}


Hello, world!

\subsection{Sophisticated Mathematical Results}


$1 + 1 = 2$.

\subsection{A comprehensive list of even prime numbers}


There is only one: $2$.

\end{document}
Labels and Referencing

A useful feature of LATEX is how it handles cross-referencing:

\section{Introduction}\label{S.intro}
This section is Section \ref{S.intro}. This reference will continue to
point to the right part of the document, even if I add a new section
before this.

\begin{equation}\label{E.addition}
1 + 1 = 2.
\end{equation}

I can also use labels to point at equations, such as Equation


\ref{E.addition}, and quite a lot of other document elements.

You typically need to compile twice. Once for LATEX to learn the
labels, and once to connect the cross-references.
Typesetting Text

Before we start with equations, we should learn how to typeset


text. Try the following lines:

\textit{This text is italics}


\textbf{This text is bold}
\textsc{This text is in small capitals}
\textrm{This text is in roman font}
\texttt{This text is in teletype font}

There are other options as well. Feel free to explore colours and
underlines, and anything else that seems like it should be possible.
Typesetting Text

Before we start with equations, we should learn how to typeset


text. Try the following lines:

This text is italics


This text is bold
This text is in small capitals
This text is in roman font
This text is in teletype font

There are other options as well. Feel free to explore colours and
underlines, and anything else that seems like it should be possible.
Font Size

Dealing with fonts can in LATEX can be unintuitive. It would be


nice if we could just specify the points, but no such luck. Instead,
we have

\tiny{tiny} \scriptsize{scriptsize} \footnotesize{footnotesize}


\small{small} \normalsize{normalsize} \large{large} \Large{Large}
\LARGE{LARGE}

tiny scriptsize footnotesize small normalsize large Large LARGE


I sometimes use scriptsize for figures, but otherwise the default size
normally does the job.
A Note About Symbols

LATEX is a programming language, which makes use of symbols


such as $, \, % and { }. This makes it tricky if you want to use
them in-text. Typically these require special commands to produce:

\LaTeX\ is a programming language, which makes use of symbols such as


\$, \textbackslash, \% and \{ \}. This makes it tricky if you want to
use them in-text. Typically these require special commands to produce:

Use the left quote (below the escape key) once to make ‘ and twice
to make “. Use the right quote (next to the semi-colon) to make ’
and ” in the same way. Never use the double quotation mark.
Lists

Writing lists is easy. Try the following:

\begin{itemize}
\item One thing in a list.
\item Another thing in a list.
\end{itemize}

\begin{enumerate}
\item The first numbered thing I have to say.
\item The second numbered thing I have to say.
\item The third numbered thing I have to say.
\end{enumerate}

Lists are a very common source of compile errors. If you open an


itemize or enumerate environment, make sure you close it!
Lists

Writing lists is easy. Try the following:

• One thing in a list.


• Another thing in a list.

1. The first numbered thing I have to say.


2. The second numbered thing I have to say.
3. The third numbered thing I have to say.

Lists are a very common source of compile errors. If you open an


itemize or enumerate environment, make sure you close it!
Writing Equations

Typesetting text is nice, but writing equations is the real reason for
using LATEX. We will only scratch the surface here.
Equations come in two varieties: Inline formulas and displayed
formulas.

Inline formulas are surrounded by dollar signs: $1 + 1 = 2$.

Displayed formulas live in the equation environment:


\begin{equation}
1 + 1 = 2
\end{equation}

The equation environment can also be accessed using square braces, but
this won’t be labelled or have equation numbers.
\[1 + 1 = 2.\]
Writing Equations
Typesetting text is nice, but writing equations is the real reason for
using LATEX. We will only scratch the surface here.

Equations come in two varieties: Inline formulas and displayed


formulas.

Inline formulas are surrounded by dollar signs: 1 + 1 = 2.

Displayed formulas live in the equation environment:

1 + 1 = 2. (1)

The equation environment can also be accessed using square braces, but this won’t be
labelled or have equation numbers.

1 + 1 = 2.
Writing Equations

This is a very brief summary of some things you can do in LATEX.

Addition and subtraction: $1 + 1 - 1 = 1$.

Multiplication: $1 \cdot 1 \times 1 = 1$.

Superscripts and subscripts: $x^2 - Z {50}$.

Roots: $\sqrt{25+75} = 10$.

Addition and subtraction: 1 + 1 − 1 = 1.

Multiplication: 1 · 1 × 1 = 1.

Superscripts and subscripts: x 2 − Z50 .



Roots: 25 + 75 = 10.
Writing Equations
This is a very brief summary of some things you can do in LATEX.

Sums, fractions, infinity: \[\sum {n=1}^{\infty} \frac{1}{2^n} = 1.\]

Integrals (and spacing, text): \[\int {0}^{1} x \,\mathrm{d}x = 1/2.\]

Greek letters: $\alpha + \theta + \Theta + \lambda + \Lambda$.

Sums, fractions, infinity:



X 1
n
= 1.
n=1
2

Integrals (and spacing, text):


Z 1
x dx = 1/2.
0

Greek letters: α + θ + Θ + λ + Λ.
Writing Equations

This is a very brief summary of some things you can do in LATEX.

Functions: $f(x,y) = \sin(x) + \log(y) - \arcsin(x+y).$

Limits: \[\lim {x \to 0} f(x) = 1.\]

Brackets: \[\left(\left[\left{\frac{x+1}{2}\right}\right]\right).\]

Functions: f (x, y ) = sin(x) + log(y ) − arcsin(x + y ).

Limits:
lim f (x) = 1.
x→0

Brackets:  


x +1
.
2
Aligning Equations

Often it’s helpful to be able to line up multiple equations, or long


equations that span more than one line

\begin{align}
x + y + z &= 1,\\
\nonumber 2y - z &= 5 + 3x,\\
x - y - 2z &= 2.
\end{align}

x + y + z = 1, (2)
2y − z = 5 + 3x,
x − y − 2z = 2. (3)

The & symbols line up in the final expression. Sometimes it will


look weird for no obvious reason, and require a bit of tweaking.
Matrices

Matrices are going to come up a lot. They’re a bit tricky, and


deserve their own slide.

\begin{equation*}
M = \begin{bmatrix}
a & b & c \\
d & e & f \\
g & h & i \\
\end{bmatrix}
\end{equation*}

 
a b c
M = d e f
g h i

Note: The * removes numbering from the equation environment.


Please, make it stop!

Don’t worry - that was a lot to take in.

You’ll get a chance to practice this, and some other things, in the
first practical session. You’ll be given some documents, and asked
to do your best to reproduce them. Have fun.

You might also like