Professional Documents
Culture Documents
Andreea-Ina Radu
March 16, 2021
1 Introduction
Welcome to the LATEX tutorial. This is a cheat-sheet to get you started with
LATEX and by the end of it, you will have knowledge to recreate (most of) this
document.
2 Structure
This is a high-level overview of how a LATEX document is structured. We first
declare the class, then any packages we use (explained just next). We can
then create custom document-wide commands, or set parameters, either for
our packages or typesetting ones e.g. if we’d want to change the geometry or
margins of all pages, if we’d want all the document to be landscape, if we want
all bullets from bullet lists to be ∗ instead of •. Then, we can declare what the
title, author and date of our document is.
We then declare the document environment. This is the main “body” of the
document. The content of our document (text, images, tables, etc) will go in
here. The \maketitle command generates the (printed) title from the previ-
ously declared commands. We usually put this just after \documentclass{...},
as we want these details at the top of our document.
Before we close the document environment, we have to declare the bibliog-
raphy, if we have any. In this example, we will use a separate bibliography file.
We will choose the bibliography and referencing style with
\bibliographystyle{...}
and will let our main document know the name of our bib file with the command
\bibliography{...}
1
\documentclass{article}
\usepackage{hyperref}
... Declare packages here
Preamble
2
3 Document Classes
Any LATEX file will start with the \documentclass[OPTIONS]{...} declaration.
With the document class, you can choose what type of document you want.
For example, if you’re writing an assignment, you probably want the article
class. If you’re writing a long report, you probably want report and for a book
you can use book. A full list of possible classes can be found here: https:
//ctan.org/topic/class. The book and report classes also have chapters,
whereas article does not. Not all of them may be installed on your system or
Overleaf. You can also write your own custom class, but that’s a story for
another day . This document uses
\documentclass[a4paper]{article}
4 Packages
Very plain LATEX can do a good number of things on its own, but packages
make life easier. They can add all sorts of features to our document, or make
things more customisable, in a simpler fashion than directly modifying all sorts
of LATEX parameters. We need to declare that we want to use a package at the
top of the LATEX file, just after we specify the document class. For example, if
I want to have a URL link in my document, and I want that URL to open in a
browser when I click on it, I can use the package hyperref and I can declare it
with
\usepackage{hyperref}
5 Headers
If you’re writing a document for an assignment or a report, you would organise
it into sections, subsections, subsubsections and paragraphs:
3
If you don’t want the sections, subsections, subsubsections to be numbered,
you can use the starred version of the respective command:
8 Lists
8.1 Unordered lists
\begin{itemize} • This is my first item
\item This is my first item
\item This is my second item • This is my second item
\end{itemize}
4
8.2 Ordered (numbered) lists
\begin{enumerate}
\item This is my first item 1. This is my first item
\item This is my second item
2. This is my second item
\begin{enumerate}
\item Second item first subitem (a) Second item first
\item Second item second subitem subitem
\end{enumerate}
(b) Second item second
\end{enumerate}
subitem
\usepackage{enumitem}
at the top of the document, where we said we’ll declare packages we use, in
the preamble.
Then, we can set the label either globally, still in the preamble:
\setlist[enumerate,1]{label={(\Alph*.)}}
The 1 refers to the depth of the list. In case you have nested lists, and you
want to change lower level labels, you need to set those too.
Or only for one list (in which case you don’t need to set it in the preamble!),
in the text:
\begin{enumerate}[label=\Alph*.]
\item This is my first item A. This is my first item
\item This is my second item
B. This is my second item
\end{enumerate}
\setlist{nosep}
Or only for one list (in which case you don’t need to set it in the preamble!),
in the text:
\begin{enumerate}[nosep]
1. This is my first item
\item This is my first item
2. This is my second item
\item This is my second item
\end{enumerate}
9 Graphics
You can insert images into you document easily. We’ll use the graphicx package
for that. In your preamble add:
5
\usepackage{graphicx}
Then, in the text, where you want the image to appear, use the following
command:
\includegraphics[OPTIONS]{PATH_TO_FILE}
\includegraphics[width=0.5\linewidth]{daffodil.jpg}
Result:
The image will always be placed where you insert the command. You can
also horizontally center the image in the page, by surrounding it with the center
environment:
\begin{center}
\includegraphics[width=0.5\linewidth]{daffodil.jpg}
\end{center}
Result:
\begin{center}
This text will be centred on the page!
\end{center}
Result:
6
Figure 1: This is my daffodil.
10 Floats
“Floats are containers for things in a document that cannot be broken over a
page”. They float in the document, and may be cleverly re-positioned to get
the best flow. You can have as much or as little control over their positioning
as you want.
We now know how to insert an image, but what if we want to add a caption?
Also, inserting it as above will not allow LATEX to work its typesetting magic
and determine the best place for our image. So, the best practice is to use a
figure environment:
\begin{figure}
\centering
\includegraphics[width=0.5\linewidth]{daffodil.jpg}
\caption{This is my daffodil.}
\end{figure}
• t – top
• b – bottom
• p – a special page only for floats
7
• h – here; approximately where we placed it in the (source) text, but it
may be moved if it breaks the flow terribly
\begin{figure}[ht]
\centering
\includegraphics[width=0.5\linewidth]{daffodil.jpg}
\caption{This is my daffodil -- ht.}
\end{figure}
E.g. ht tells it I want the figure here, but if it really doesn’t work in that
place, it can put it at the top of the page (it may put it on the next page at the
top!).
11 Tables
Tables follow a similar approach as figures. The floating environment for tables
is table. Then, we define how we want our table to be structured. Now, there
are many environments that can be used to do that. I will show my favourite
one, but you can look others up. The package used to create the tables in this
document is tabularx1 and we need to add that to the preamble:
\usepackage{tabularx}
8
\begin{table}[h!]
\caption{My first table}
\centering
\begin{tabularx}{0.9\linewidth}{ l | c | r | X }
Column 1 & Column 2 & Column 3 & Column 4 \\
\hline
item 11 & item 12 & item 13 & stretchy paragraph column 1 \\
left item 21 & centred item 22 & right item 23 & stretchy
paragraph column 2 \\
\hline
\end{tabularx}
\end{table}
Result:
9
• Column 1 & Column 2 & Column 3 & Column 4 \\ is the first row of
the table; cell content is separated by the & marker. We then mark that
the row has ended by the new line marker \\.
• \hline triggers a horizontal line spanning the width of the table.
10
You’ll notice that the name of my labels includes a prefix that gives me
an idea of what they are (sec:LABEL NAME for section, fig:LABEL NAME for
figure, tab:LABEL NAME for table). This is not mandatory, but it helps me
keep organised. It’s also great if you have some form of autocompletion (like
Overleaf does). When you type \ref{...} it will show you all the labels you’ve
placed, so using prefixes is a sort of filtering system (e.g. if I type \ref{sec
the autocomplete will only show me the section labels). If I label a subsection,
I would usually include the section label name in it too. For example, for
Subsection 8.2, I libelled the subsection with \label{subsec:lists:ordered},
so I know it’s part of the Lists section. But it’s fully up to you how you want
to organise your document.
13 Maths
Maths mode has a plethora of cool things you can do, and we could have a
whole workshop only on this! I’m going to try and give you a crash course into
its basic usage, but really, even I haven’t discovered all the ways to use it!
LATEX differentiates between text mode (what we’ve been using so far), and
maths mode, which beautifully typesets mathematical content. Let’s see what
we can do.
• $...$ is generally used if you want to insert short, letter-based maths into
the text; e.g. a variable name: Let x be. . . (Code: Let $x$ be).
• \(...\) if you have longer, more complex maths; e.g. an equation:
The sum of the first n natural numbers can be calculated as
P n n(n+1)
i=1 = 2 .
Code:
The sum of the first $n$ natural numbers can be calculated
as \( \sum_{i=1}^{n}=\frac{n(n+1)}{2} \).
Let’s dissect it!
P
– \sum is a macro for the symbol
– _ and ^ are the symbols for subscript and superscript, respectively
(or powers and indices); if the subscript or superscript contains more
than one characters we need to enclose it with {...}.
– \frac{...}{...} is a command that lets us typeset fractions. The
first argument is the numerator and the second argument is the de-
nominator.
11
13.2 Display mode
13.2.1 Unnumbered
If we want the equation we write to be on its own line, we need to use the
\[...\] markers (you may or may not have a blank line between the text and
this type of maths mode; I prefer to have it for consistency):
The sum of the first $n$ natural numbers can be calculated as:
\[ \sum_{i=1}^{n}=\frac{n(n+1)}{2} \]
13.2.2 Numbered
If we also want to add a number to this equation (and even a label!), we need to
use a different environment. The equation environment is the most basic and
doesn’t require other packages:
n
X n(n + 1)
= (1)
i=1
2
The sum of the first n natural numbers can be calculated with the formula
in Equation 1.
Code:
\begin{equation}\label{eq:sum_n}
\sum_{i=1}^{n}=\frac{n(n+1)}{2}
\end{equation}
The sum of the first $n$ natural numbers can be calculated with the
formula in Equation~\ref{eq:sum_n}.
13.2.3 Alignment
If we want to align some equations based on a character, for example we have
a pair of equations to solve, or we are showing the steps of how we worked
something out, we can use the align environment, from the amsmath package:
2x − 5y = 8 (2)
3x + 9y = −12 (3)
or (proving that the sum of any 3 even natural numbers is a multiple of 6):
12
2n + (2n + 2) + (2n + 4) = 6n + 6
= 6(n + 1)
Code:
\begin{align}
2x - 5y &= 8 \\
3x + 9y &= -12
\end{align}
\begin{align*}
2n + (2n + 2) + (2n + 4) &= 6n + 6 \\
&= 6(n + 1)
\end{align*}
You can notice that the starred version of the environment does not number
the equations.
(P ∨ ¬Q) → R ∼
= ¬(P ∨ ¬Q) ∨ R
∼
= (P ∧ ¬¬Q) ∨ R
∼
= (P ∧ Q) ∨ R
Code:
13
\begin{align*}
(P \lor \lnot Q) \to R &\cong \lnot (P \lor \lnot Q) \lor R \\
&\cong (P \land \lnot \lnot Q) \lor R \\
&\cong (P \land Q) \lor R
\end{align*}
14.1 Verbatim
This is the most basic environment. It does not require any additional packages.
The verbatim environment prints out exactly the text you give it. It also has an
in-line version, \mintinline{latex}{...}. Remember, an ad literam package
such as verbatim does not play nicely if nested with itself.
\begin{verbatim}
This is a verbatim environment
if __name__ == "__main__":
print("Hello world!")
\end{verbatim}
Result:
if __name__ == "__main__":
print("Hello world!")
14.2 Algorithms
The algorithm2e2 package is very useful for algorithms. You don’t always
need to give the exact source code, you may want to just explain an algorithm.
While the package is called algorithm2e, the environment is called algorithm.
There are other packages as well, but I’ll only be showing this one. Have fun
experimenting with others!
\begin{algorithm}
\SetKwInOut{Input}{input}
\SetKwProg{Fn}{def}{\string:}{end}
\SetKwFunction{FnLen}{length}
\SetKwFunction{FnSwap}{swap}
algorithm2e.pdf
14
$swapped \leftarrow true$\;
\While{$swapped$}{
$swapped \leftarrow false$\;
\For{$i=0$ \KwTo $n-1$}{
\If{$L[i] > L[i+1]$}{
$\FnSwap(A[i], A[i+1])$\;
$swapped \leftarrow true$\;
}
}
}
}
\caption{Bubblesort algorithm}\label{alg:bubblesort}
\end{algorithm}
def bubbleSort(L):
input: L – list to be sorted
n ← length(L);
swapped ← true;
while swapped do
swapped ← f alse;
for i = 0 to n − 1 do
if L[i] > L[i + 1] then
swap(A[i], A[i + 1]);
swapped ← true;
end
end
end
end
Algorithm 1: Bubblesort algorithm
15
• \Input is the above defined macro; we simply state what the input to the
bubbleSort function is
• At the end, we add a caption and a label, for later referencing in text
algorithm2e has a lot of keywords and options which can be customised,
feel free to explore them.
14.3.1 Listings
While the package name is listings3 , the environment we need to use is
lstlisting. The syntax is (the language name needs to be capitalised):
\begin{lstlisting}[language=YOUR_LANGUAGE]
CODE GOES HERE
\end{lstlisting}
Example output:
def b u b b l e s o r t ( o u r l i s t ) :
swapped = True
while ( swapped ) :
swapped = F a l s e
f o r i in range ( len ( o u r l i s t ) − 1 ) :
i f o u r l i s t [ i ] > o u r l i s t [ i +1]:
o u r l i s t [ i ] , o u r l i s t [ i +1] = o u r l i s t [ i +1] , \
our list [ i ]
swapped = True
3 http://mirror.ox.ac.uk/sites/ctan.org/macros/latex/contrib/listings/listings.
16
14.3.2 Minted
For the minted4 package, the environment is also called minted. The syntax is:
\begin{minted}{YOUR_LANGUAGE}
CODE GOES HERE
\end{minted}
Example output:
def bubble_sort(our_list):
swapped = True
while(swapped):
swapped = False
for i in range(len(our_list) - 1):
if our_list[i] > our_list[i+1]:
our_list[i], our_list[i+1] = our_list[i+1], \
our_list[i]
swapped = True
15 Bibliography
Lastly, we’ll look at how to use bibliography. I won’t go into the best ways of
managing bibliographies, you may use any software you want, or do it manually.
The important thing to know is that you need the bibliographical information
for some article, book, website to be in a specific format. For academic works,
Google Scholar or Semantic Scholar are pretty good, as you can use them to
find the articles and they give the info in the format required. You’d just have
to copy and paste it (Figure 3)! Collect the info on the items you want to cite
in a .bib file and put it in the same folder as your main LATEX source file.
The idea is that each item you want to cite has an entry in your .bib file.
For example:
@inproceedings{radu2016leia,
title={LeiA: A lightweight authentication protocol for CAN},
author={Radu, Andreea-Ina and Garcia, Flavio D},
booktitle={European Symposium on Research in Computer Security},
pages={283--300},
year={2016},
organization={Springer}
}
• @inproceedings is the entry type. In this case, the paper was pub-
lished in some proceedings; most popular types are @book, @article,
@inproceedings, @conference, @manual (a technical manual), @masterthesis,
@phdthesis, @techreport, @unpublished or @misc. @misc is mostly used
to cite web pages, but can be anything that doesn’t fit the other types
(e.g. some personal correspondence, lecture notes)
4 http://mirror.ox.ac.uk/sites/ctan.org/macros/latex/contrib/minted/minted.pdf
17
Figure 3: Getting the BibTeX information from Google Scholar.
Citing websites is the only type that needs to be manually created if you’re
not using software and browser plugins. A bib item for a website looks like this:
@misc{paperpile_bibtex,
title={A complete guide to the BibTeX format},
howpublished={\url{https://www.bibtex.com/g/bibtex-format/}}
author={Paperpile},
year={na}
}
We need to tell our main document two things in order to make this work
(best practice is to put this at the end of the document, but before any appen-
dices):
18
• the style for the citations; e.g. this document uses
\bibliographystyle{plain}
Another popular style is apalike, which inserts the surnames of the au-
thors and the year in the text as citation ([Radu and Garcia, 2016]) e.g.,
instead of the [2] we currently see. alpha is also quite popular; it creates
a (at most) 3-letter key from the surnames of the first three authors and
appends the last two digits of the publication year (e.g. [RG16]).
• the .bib file where we have all the bibliographical information. We do
this with the \bibliography{FILENAME.bib} command.
Now we can cite our references in the text! We do this with the command
\cite{KEYWORD}.
The output of
is:
For example, the first paper I published in my PhD was [2] and Paperpile [1]
have a good explanation on BibTeX formats!
You’ll be able to see the References section at the end of the document, with
the full info of the paper and website we cite.
If you are not using Overleaf to compile your document (so if you’re using
LATEX on your own system directly, through some editor of your choice),
you must remember the following: you must compile the document
multiple times, to get the references included correctly in the document.
You need to run:
pdflatex <document_name.tex>
bibtex <document_name.tex>
pdflatex <document_name.tex>
pdflatex <document_name.tex>
16 Table of Contents
Inserting a table of contents is very easy! It automatically gets generated from
your \section and \subsection titles. Just insert \tableofcontents wher-
ever you want it to appear. I’ve put mine here, but you’d usually insert it at
the beginning of your document. You’ll notice that this table of contents picked
up the headings I exemplified in Section 5. You will also notice that the starred
subsection I used does not appear in the table of contents.
Contents
1 Introduction 1
19
2 Structure 1
3 Document Classes 3
4 Packages 3
5 Headers 3
8 Lists 4
8.1 Unordered lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
8.2 Ordered (numbered) lists . . . . . . . . . . . . . . . . . . . . . . 5
8.3 Changing the list marker . . . . . . . . . . . . . . . . . . . . . . . 5
8.4 List spacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
9 Graphics 5
10 Floats 7
11 Tables 8
13 Maths 11
13.1 In-line mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
13.2 Display mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
13.2.1 Unnumbered . . . . . . . . . . . . . . . . . . . . . . . . . 12
13.2.2 Numbered . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
13.2.3 Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
13.3 Useful symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
13.4 An example from reasoning . . . . . . . . . . . . . . . . . . . . . 13
15 Bibliography 17
16 Table of Contents 19
17 Done! 21
20
17 Done!
I hope this gives you a better idea of how to approach LATEX! Remember, there
are many, many things you can do with it. Google and StackOverflow are your
friends, and so are the manuals of various packages.
Enjoy your beautiful documents!
References
[1] Paperpile. A complete guide to the bibtex format. https://www.bibtex.
com/g/bibtex-format/.
21