LATEX Tutorial PDF
LATEX Tutorial PDF
LATEX Tutorial PDF
Workbook
i
ii
Contents
1 Introduction 1
1.1 What is LATEX? . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Before You Start . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Document Structure 3
2.1 Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Creating a Title . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Labelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Typesetting Text 11
3.1 Font Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Coloured Text . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Font Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.5 Comments & Spacing . . . . . . . . . . . . . . . . . . . . . . . 14
3.6 Special Characters . . . . . . . . . . . . . . . . . . . . . . . . 15
4 Tables 17
4.1 Practical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 Figures 21
5.1 Practical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Equations 23
6.1 Inserting Equations . . . . . . . . . . . . . . . . . . . . . . . . 23
6.2 Mathematical Symbols . . . . . . . . . . . . . . . . . . . . . . 24
6.3 Practical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7 Inserting References 27
iii
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.2 The BibTeX file . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.3 Inserting the bibliography . . . . . . . . . . . . . . . . . . . . 28
7.4 Citing references . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.5 Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7.6 Practical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8 Further Reading 31
iv
Chapter 1
Introduction
1
is Portable Document Format (PDF), which appears as it will be printed and
can be transferred easily between computers.
Although the code in this workbook should work in any LATEX editor, specific
examples and screenshots refer to TeXworks 0.4.5 on Windows 7.
2
Chapter 2
Document Structure
2.1 Essentials
ò Start TeXworks.
Line numbers are not essential, but will make it easier to compare your code
with the screenshots and find errors.
Syntax colouring will highlight commands in blue and can make it easier to
spot mistakes.
\documentclass[a4paper,12pt]{article}
\begin{document}
A sentence of text.
\end{document}
3
The \documentclass command must appear at the start of every LATEX
document. The text in the curly brackets specifies the document class. The
article document class is suitable for shorter documents such as journal
articles and short reports. Other document classes include report (for longer
documents with chapters, e.g. PhD theses), proc (conference proceedings),
book and slides. The text in the square brackets specifies options — in this
case it sets the paper size to A4 and the main font size to 12pt.
The \begin{document} and \end{document} commands enclose the text
and commands that make up your document. Anything typed before \begin
{document} is known as the preamble, and will affect the whole document.
Anything typed after \end{document} is ignored.
The empty lines aren’t necessary1 , but they will make it easier to navigate
between the different parts of the document as it gets longer.
4
2.2 Troubleshooting
If there is an error in your document and TeXworks cannot create the PDF
the Typeset button will change to red with a white X (Abort typesetting
button) and the Console output at the bottom of the screen will stay open.
If this happens:
Points to note:
• \today is a command that inserts today’s date. You can also type in
a different date, for example \date{November 2013}.
• Article documents start the text immediately below the title on the
same page. Reports put the title on a separate page (like this work-
book).
5
Figure 2.1: TeXworks screenshot showing the maketitle command.
2.4 Sections
You should divide your document into chapters (if needed), sections and sub-
sections. The following sectioning commands are available for the article
class:
• \section{...}
• \subsection{...}
• \subsubsection{...}
• \paragraph{...}
• \subparagraph{...}
The title of the section replaces the dots between the curly brackets. With
the report and book classes we also have \chapter{...}.
\section{Introduction}
This is the introduction.
\section{Methods}
\subsection{Stage 1}
The first part of the methods.
6
\subsection{Stage 2}
The second part of the methods.
\section{Results}
Here are my results.
2.5 Labelling
You can label any of the sectioning commands so they can be referred to in
other parts of the document. Label the section with \label{labelname}.
Then type \ref{labelname} or \pageref{labelname}, when you want to
refer to the section or page number of the label.
7
ò Type Referring to section \ref{sec1} on page \pageref{sec1}
in the Results section.
ò Click on the Typeset button and check the PDF. You may need to
typeset the document twice before the references appear in the PDF.
8
ò Type the following on a new line below \maketitle:
\pagenumbering{roman}
\tableofcontents
\newpage
\pagenumbering{arabic}
The \newpage command inserts a page break so that we can see the effect of
the page numbering commands. The first 14 lines of code should now look
like figure 2.4.
9
10
Chapter 3
Typesetting Text
ò Add some more text to your document and experiment with different
text effects.
11
The basic colour names that \usepackage{color} knows about are black,
red, green, blue, cyan, magenta, yellow and white:
{\color{colour_name}text}
Where colour_name is the name of the colour you want, and text is the text
you want to be coloured.
12
ò Experiment with different font sizes in your document.
3.4 Lists
LATEX supports two types of lists: enumerate produces numbered lists, while
itemize is for bulleted lists. Each list item is defined by \item. Lists can
be nested to produce sub-lists.
\begin{enumerate}
\item First thing
\item Second thing
\begin{itemize}
\item A sub-thing
\item Another sub-thing
\end{itemize}
\item Third thing
\end{enumerate}
1. First thing
2. Second thing
• A sub-thing
• Another sub-thing
3. Third thing
It is easy to change the bullet symbol using square brackets after the \item,
for example, \item[-] will give a dash as the bullet. You can even use words
as bullets, for example, \item[One].
The following code:
13
\begin{itemize}
\item[-] First thing
\item[+] Second thing
\begin{itemize}
\item[Fish] A sub-thing
\item[Plants] Another sub-thing
\end{itemize}
\item[Q] Third thing
\end{itemize}
Produces:
- First thing
+ Second thing
Fish A sub-thing
Plants Another sub-thing
Q Third thing
Produces:
14
Multiple consecutive spaces in LATEX are treated as a single space. Several
empty lines are treated as one empty line. The main function of an empty
line in LATEX is to start a new paragraph. In general, LATEX ignores blank
lines and other empty space in the .tex file. Two backslashes (\\) can be
used to start a new line.
If you want to add blank space into your document use the \vspace{...}
command. This will add blank vertical space of a height specified in typo-
graphical points (pt). For example, \vspace{12pt} will add space equivalent
to the height of a 12pt font.
Ask the tutor, or check the .tex file of this workbook, if you need help.
15
16
Chapter 4
Tables
\begin{tabular}{...}
Where the dots between the curly brackets are replaced by code defining the
columns:
For example, {lll} (i.e. left left left) will produce 3 columns of left-aligned
text with no vertical lines , while {|l|l|r|} (i.e. |left|left|right|) will produce
3 columns — the first 2 are left-aligned, the third is right-aligned, and there
are vertical lines around each column.
The table data follows the \begin command:
17
• \hline inserts a horizontal line.
\begin{tabular}{|r|l|} 8 here’s
\hline 86 stuff
8 & here’s \\ 2008 now
\cline{2-2}
86 & stuff \\
\hline \hline
2008 & now \\
\hline
\end{tabular}
4.1 Practical
ò Write code to produce the following tables:
18
Year
City 2006 2007 2008
London 45789 46551 51298
Berlin 34549 32543 29870
Paris 49835 51009 51970
Ask the tutor, or look at the .tex file of this workbook, if you need help.
19
20
Chapter 5
Figures
\begin{figure}[h]
\centering
\includegraphics[width=1\textwidth]{myimage}
\caption{Here is my image}
\label{image-myimage}
\end{figure}
[h] is the placement specifier. h means put the figure approximately here (if
it will fit). Other options are t (at the top of the page), b (at the bottom of
the page) and p (on a separate page for figures). You can also add !, which
overrides the rule LATEX uses for choosing where to put the figure, and makes
it more likely it will put it where you want (even if it doesn’t look so good).
\centering centres the image on the page, if not used images are left-aligned
by default. It’s a good idea to use this as the figure captions are centred.
includegraphics{...} is the command that actually puts the image in your
document. The image file should be saved in the same folder as the .tex file.
[width=1\textwidth] is an optional command that specifies the width of
the picture - in this case the same width as the text. The width could also
be given in centimeters (cm). You could also use [scale=0.5] which scales
the image by the desired factor, in this case reducing by half.
21
\caption{...} defines a caption for the figure. If this is used LATEX will add
“Figure” and a number before the caption. If you use captions, you can use
\listoffigures to create a table of figures in a similar way to the table of
contents (section 2.6, page 8).
\label{...} creates a label to allow you to refer to the table or figure in
your text (section 2.5, page 7).
5.1 Practical
ò Add \usepackage{graphicx} in the preamble of your document (be-
fore the \begin{document} command).
ò Type the following text at the point where you want your image in-
serted:
\begin{figure}[h!]
\centering
\includegraphics[width=1\textwidth]{ImageFilename}
\caption{My test image}
\end{figure}
Replace ImageFilename with the name of your image file, excluding the file
extension. If there are any spaces in the file name enclose it in quotation
marks, for example "screen 20".
22
Chapter 6
Equations
One of the main reasons for writing documents in LATEX is because it is really
good at typesetting equations. Equations are written in ‘math mode’.
1+2=3
1+2=3 (6.1)
The number 6 refers to the chapter number, this will only appear if you are
using a document class with chapters, such as report.
Use \begin{eqnarray}...\end{eqnarray} to write equation arrays for a
series of equations/inequalities. For example —
\begin{eqnarray}
23
a & = & b + c \\
& = & y - z
\end{eqnarray}
Produces:
a = b+c (6.2)
= y−z (6.3)
For unnumbered equations add the star symbol * after the equation or
eqnarray command (i.e. use {equation*} or {eqnarray*}).
6.2.2 Fractions
Fractions are inserted using \frac{numerator}{denominator}.
1
http://detexify.kirelabs.org
24
$$\frac{a}{3}$$ produces:
a
3
Fractions can be nested —
$$\frac{y}{\frac{3}{x}+b}$$ produces:
y
3
x
+b
6.2.3 Roots
Square root symbols are inserted using \sqrt{...} where ... is replaced by
the square root content. If a magnitude is required it can be added using
optional square brackets [...].
$$\sqrt{y^2}$$ produces: q
y2
$$\sqrt[x]{y^2}$$ produces: q
x
y2
x=1
25
alphabet — for those that are different capitalise the first letter of the name
to produce a capital Greek letter.
For example —
$\alpha$ = α
$\beta$ = β
$\delta, \Delta$ = δ, ∆
$\theta, \Theta$ = θ, Θ
$\mu$ = µ
$\pi, \Pi$ = π, Π
$\sigma, \Sigma$ = σ, Σ
$\phi, \Phi$ = φ, Φ
$\psi, \Psi$ = ψ, Ψ
$\omega, \Omega$ = ω, Ω
6.3 Practical
ò Write code to produce the following equations:
e = mc2 (6.1)
c
π= (6.2)
d
d x
e = ex (6.3)
dx
d Z∞
f (s)ds = f (x) (6.4)
dx 0
f (i) (0) i
= 0∞
X
f (x) = x (6.5)
i i!
xi
r
x= y (6.6)
z
Ask the tutor, or look at the .tex file of this workbook, if you need help.
26
Chapter 7
Inserting References
7.1 Introduction
LATEX includes features that allow you to easily cite references and create
bibliographies in your document. This document will explain how to do this
using a separate BibTeX file to store the details of your references.
@article{
Birdetal2001,
Author = {Bird, R. B. and Smith, E. A. and Bird, D. W.},
Title = {The hunting handicap: costly signaling in human
foraging strategies},
Journal = {Behavioral Ecology and Sociobiology},
Volume = {50},
Pages = {9-19},
Year = {2001} }
27
Each reference starts with the reference type (@article in the example
above). Other reference types include @book, @incollection for a chapter in
an edited book and @inproceedings for papers presented at conferences1 .
The reference type declaration is followed by a curly bracket, then the ci-
tation key. Each reference’s citation key must be unique - you can use
anything you want, but a system based on the first author’s name and year
(as in the example above) is probably easiest to keep track of.
The remaining lines contain the reference information in the format
Field name = {field contents},.
You need to include LaTeX commands in your BibTeX file for any special
text formatting - e.g. italics (\emph{Rattus norvegicus}), quotation marks
(‘‘...’’), ampersand (\&).
Surround any letters in a journal article title that need to be capitalised
with curly brackets {...}. BibTeX automatically uncapitalises any capital
letters within the journal article title. For example, “Dispersal in the con-
temporary United States” will be printed as “Dispersal in the contemporary
united states”, but “Dispersal in the contemporary {U}nited {S}tates” will
be printed as “Dispersal in the contemporary United States”.
You can type the BibTeX file yourself, or you can use reference management
software such as EndNote to create it 2 .
Type the following where you want the bibliography to appear in your doc-
ument (usually at the end):
\bibliographystyle{plain}
\bibliography{Doc1}
Where references is the name of your .bib file.
1
See the Bibliography Management chapter in the LaTeX Wikibook http://en.
wikibooks.org/wiki/LaTeX/Bibliography_Management for a full list of the reference
types that BibTeX knows about, and their required and optional fields.
2
Instructions for using EndNote with LaTeX are available on the HowTo wiki https:
//www.wiki.ed.ac.uk/x/sZpKBg
28
7.4 Citing references
Type \cite{citationkey} where you want to cite a reference in your .tex
document. If you don’t want an in text citation, but still want the reference
to appear in the bibliography, use \nocite{citationkey}.
To include a page number in your in-text citation put it in square brackets
before the citation key: \cite[p. 215]{citationkey}.
To cite multiple references include all the citation keys within the curly brack-
ets separated by commas: \cite{citation01,citation02,citation03}.
7.5 Styles
Plain The citation is a number in square brackets (e.g. [1]). The bibliog-
raphy is ordered alphabetically by first author surname. All of the authors’
names are written in full.
Abbrv The same as plain except the authors’ first names are abbreviated
to an initial.
Unsrt The same as plain except the references in the bibliography appear
in the order that the citations appear in the document.
29
2010]) and \citet{...} for a citation where only the year is in brackets
(e.g. Koppe [2010]). There are lots of other ways that you can modify
citations when using the natbib package - see the package’s reference sheet
for full details3 .
Natbib comes with three bibliography styles: plainnat, abbrvnat and un-
srtnat. These format the bibliography in the same way as the plain, abbrv
and unsrt styles, respectively.
7.6 Practical
ò Create a new file in TeXworks (File menu > New).
ò Click the Save button, the Save File window will open.
ò Give the file the same name as your .tex document (for example, Doc1)
and save it as a BibTeX database in the same folder as your .tex file.
ò Switch to your .bib file, choose BibTeX from the typeset menu and
click the Typeset button.
ò Switch to your .tex file and typeset it twice. The in-text citations and
reference list should be inserted.
3
Reference sheet for natbib usage http://mirror.ctan.org/macros/latex/contrib/
natbib/natnotes.pdf
30
Chapter 8
Further Reading
LATEX Project
http://www.latex-project.org/
Official website - has links to documentation, information about installing
LATEX on your own computer, and information about where to look for help.
The Not So Short Introduction to LATEX2e
http://ctan.tug.org/tex-archive/info/lshort/english/lshort.pdf
A good tutorial for beginners.
LATEX Wikibook
http://en.wikibooks.org/wiki/LaTeX/
Comprehensive and clearly written, although still a work in progress. A
downloadable PDF is also available.
Comparison of TEX Editors on Wikipedia
http://en.wikipedia.org/wiki/Comparison_of_TeX_editors
Information to help you to choose which LATEX editor to install on your own
computer.
TeX Live
http://www.tug.org/texlive/
“An easy way to get up and running with the TeX document production
system”. Available for Unix and Windows (links to MacTeX for MacOSX
users). Includes the TeXworks editor.
Workbook Source Files
http://edin.ac/17EQPM1
Download the .tex file and other files needed to compile this workbook.
31