LaTeX Guide
LaTeX Guide
Contents
1 Introduction 2
1.1 What is LATEX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 How LATEX differs to traditional word processors . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Getting LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Web based LATEX editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.6 Some useful and interesting web links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Getting started 4
2.1 LATEX editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Commands and Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 The structure of a tex file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 The top matter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Entering text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Program code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Sectioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.9 Table of contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Typesetting equations 11
3.1 Aligned equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Superscripts and subscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.5 Brackets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.7 Greek characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.8 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.9 Piecewise functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.10 Text in equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Referencing 19
5.1 Referencing sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.2 Cross referencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
A Article template 23
1
A Beginner’s Guide to LATEX
1 Introduction
1.1 What is LATEX?
LATEX (pronounced ‘lay-tech’) is a document preparation system that is widely used by mathematicians
because it is easy to produce professional standard documents with beautifully typeset mathematics.
LATEX is based upon TEX (pronounced ‘tech’), a low level typesetting language written by Donald Knuth
in 1982.
• sharelatex – http://www.sharelatex.com
2 Getting started
A LATEX document consists of a plain text file that contains the commands that are used to create the
typeset document (known as the ‘tex file’). This plain text file can be created using any text editor you
want but it must have the extension .tex appended to the end of the filename. The tex file is then
compiled where it is parsed to the LATEX program and provided there are no errors, an output file will be
produced that will contain the typeset document.
\command
For example, the command \LaTeX\ produces the LATEX logo. If a command requires an argument then
it is placed within curly brackets { }, i.e.,
\command{argument}
\begin{environment name}
...
\end{environment name}
For example, the center environment centres all text that appears in between the \begin{center} and
\end{center} commands.
2.2.1 Comments
Like with any markup or programming language, comments in the source file are useful for helping
understand what is going on. In LATEX any text following a \% symbol is treated as a command and is
ignored.
2.2.2 Measurements
Some commands require a length to be specified. In these cases LATEX recognises the following units:
\newcommand
\renewcommand
\newenvironment
\renewenvironment
The use of these commands is outside the scope of this document, but for more information on using
these, click on the following link: Wikibooks: LaTeX/Macros.
\documentclass{...}
\usepackage{...}
\begin{document}
...
\end{document}
The area between \documentclass{...} and \begin{document} is known as the preamble and is used
to tell LATEX which packages are required (see section 2.3.2) and contains commands that affect the entire
document. After the preamble, the text of the document is contained within the \begin{document} and
\end{document} commands.
2.3.2 Packages
Packages are enhancements that allows authors to use commands to perform actions that would be very
complicated using basic LATEX. Packages are activated using usepackage command
\usepackage[options]{package}
where package is the name of the package and options is a list of keywords that trigger special features
in the package. There are hundreds of available packages and different documents will require different
packages. Below is a list of common packages that you may find useful
Package Description
geometry An easy way to set the page margins
amsmath, amssymb, American Mathematics Society (AMS) packages that provide en-
amsthm chancements to the typesetting of mathematics in LATEX documents
biblatex Enhanced referencing package that allows Harvard style referencing
graphicx Include image files in a document
fancyhdr Fancy header and footers
listings Insert code into a document with syntax highlighting
tikz A vector drawing package
automatically appends fig., table., eq. etc. to the cross-reference
cleveref
based on the type of environment being referenced
Automatically add hyperlinks for references and easy cross-
hyperref
referencing
For example, to include these packages in your preamble the following would appear in the preamble
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authoryear,natbib]{biblatex}
\addbibresource{references}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage{listings}
\usepackage{cleveref}
\usepackage{hyperref}
\documentclass[11pt,a4paper,twoside]{article}
\begin{document}
\maketitle
\end{document}
The \title{...} and \author{...} fields are compulsory and LATEX will return an error if these aren’t
specified. The \date{...} field is optional, if it is left blank then LATEX will not use the date in the
title; if no \date command is specified then LATEX will use the date that the document is compiled. If an
affiliate institution is required, this can be written in the \author field, for example,
\documentclass[11pt,a4paper,twoside]{article}
\begin{document}
\maketitle
\end{document}
If we compile the above tex file we get the output shown below
A LATEX Document
Joe Bloggs
School of Computing, Mathematics and Digital Technology
Manchester Metropolitan University
September 2013
2.4.1 Abstract
LATEX provides an environment for including an abstract in an article document class. To add an
abstract in your document, include the following in the preamble:
\begin{abstract}
...
\end{abstract}
\setlength{\parindent}{0pt}
\setlength{\parskip}{11pt}
Since certain characters have special meaning in LATEX then in order to use these same characters in a
document we need to use commands to do this. Below is a list of the special characters in LATEX and the
command used to enter these into a document
The appearance and size of text can be changed using the following commands:
Command Description
\textbf{...} bold text
\emph{...} emphasised text
\underline{...} underlined text
\texttt{...} typewriter font
{\tiny ...} tiny
\begin{center}
This text is centred on the page
\end{center}
2.6 Lists
LATEX provides environments to handle bulleted and numbered lists. For a simple bulleted list, use the
itemize environment, for example, the source
\begin{itemize}
\item First item
\item Second item
\end{itemize}
produces
• First item
• Second item
Numbered lists can be produced using the enumerate environment, for example.
\begin{enumerate}
\item First item
\item Second item
\end{enumerate}
produces
1. First item
2. Second item
If you have a file containing the code you want to include in your document then this can be included in
your document using
\lstinputlisting{myfile.m}
This is particularly useful if you are still making changes to the code.
2.8 Sectioning
LATEX provides an easy way to structure a document using sections/chapters. A typical document will
have at most three levels of sectioning with sections, subsections and subsubsections being nested within
each other. A new section can be started using the command
\section{Section heading}
This section’s content ...
\subsection{Subsection heading}
This subsection’s content ...
\subsubsection{Subsubsection heading}
This subsubsection’s content ...
produces
1 Section heading
This section’s content ...
Note that the section numbering is automatically included in the output. If you do not want the section
numbering to appear in the document, replace the section command with section*.
1 Section heading 1
1.1 Subsection heading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Subsubsection heading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Similar commands exist for generating list of figures and lists of tables, these are: \listoffigures and
\listoftables respectively.
3 Typesetting equations
There are two main types of equations in LATEX: inline equations that appear along side normal text and
display equations that appear on their own line centered on the page. To write an inline equation, we
enclose the equation between \( and \). For example,
The roots of a quadratic polynomial of the form \( 0 = ax^2 + bx + c \) can be
calculated using the quadratic formula.
produces
The roots of a quadratic polynomial of the form 0 = ax2 + bx + c can be calculated using the
quadratic formula.
To write a display equation, we enclose the equation between \[ and \]. For example, the source:
The quadratic formula is \[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
produces
Note that the equation number has been added to the right of the equation as is standard practice (or
to the left if the fleqno option was used in the \documentclass command). LATEX will automatically
increment the equation numbers when you include more equations in your document.
Note that each line of the multiline equation has an equation number appended to the right. To suppress
this for an individual line we can use the \notag command on the line that we do not want the equation
number to appear, for example,
\begin{align}
\notag f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
\end{align}
produces
f (x) = (x + a)(x + b)
= x2 + (a + b)x + ab (4)
To suppress all equation numbers, use the align* environment, for example,
\begin{align*}
f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
\end{align*}
produces
f (x) = (x + a)(x + b)
= x2 + (a + b)x + ab
3.2 Symbols
Mathematical symbols are entered either directly from the keyboard (only +, −, =, !, /, (, ), [, ], <, >, |,
0
and : can be accessed in this way) or by using the appropriate LATEX command. A list of some of the
more common mathematical symbols is given below
For example
produces
\[ z_{n+1} = z_n^2 + c \]
produces
zn+1 = zn2 + c
3.4 Fractions
A fraction can be specified using the \frac{numerator}{denominator} command. The size of the
fraction will depend on whether it appears in an inline or display equation. For example
The fraction \( \frac{a}{b} \) appears in an inline equation and is smaller whilst the
following fraction appears in a display equation and is larger \[ \frac{a}{b}. \]
produces
The fraction ab appears in an inline equation and is smaller whilst the following fraction appears in
a display equation and is larger
a
.
b
If you want to control whether an inline or display fraction is used, the commands \tfrac and \dfrac
specify and inline or display fraction respectively.
3.5 Brackets
Brackets can be entered directly from the keyboard with the exception of curly brackets that use the
commands \{ and \}. Brackets surrounding an expression can be automatically sized to match the
vertical height of the expression using \left and \right commands immediately preceding the bracket
command. For example
produces
a
b
Note that both \left and \right must be used else LATEX will return an error. If you only require a
bracket on one side of an expression, an empty bracket can be specified using a full-stop symbol. For
example
produces
dy
dx x=1
3.6 Operators
Mathematical operators can be entered using the appropriate command. Some operators require an
argument that is placed in curly brackets. For certain operators such as summations and limits, the
superscript and subscripts are placed underneath and above the operators respectively. Below is a list of
some common operators:
√ √
a \sqrt{a} n
a \sqrt[n]{a} exp \exp log \log
α, A, β, B, γ, Γ, δ, ∆
3.8 Matrices
To typeset a matrix, use either the matrix, pmatrix, bmatrix or vmatrix environments depending on
the type of parenthesis required (none, rounded brackets, square brackets and vertical lines respectively).
As with tables, \\ specifies a line break and & specifies a column break, e.g.,
\[ A = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix} \]
produces
1 2 3
A = 4 5 6
7 8 9
1
Ekinectic = mv 2 Joules
2
4.2 Captions
Figure and table captions should can be specified using the \caption{...} command. The caption will
appear above or below the figure/table depending on where the caption command is used relative to the
commands used to construct the figure/table. Figure captions should appear below the figure, therefore
the caption command should be used after the \includegraphics command. Table captions usually
appear above a table and therefore the command should be used before the tabular environment (see
section 4.3). For example the source
\begin{figure}[htb]
\centering
\includegraphics[width=0.6\textwidth]{Images/mandelbrot.png}
\caption{The Mandelbrot set}
\label{fig:mandelbrot set}
\end{figure}
produces
Note that the figure has been centred on the page using the \centering command. The image file
mandelbrot.png is contained within the Images directory and has been scaled so that its width is 0.6
times that of the width used for the text in the document. LATEX automatically appends a figure number
to the caption depending on how many figures have been defined previously. The figure caption will also
appear in the list of figures if the \listoffigures command has been used.
\begin{tabular}{columns}
...
\end{tabular}
where the columns argument is used to specify the number of columns and the horizontal alignment of
each of the columns using the following:
l left-justified column
c centred column
r right-justified column
| vertical line
|| double vertical line
Text in the rows and columns of a table are separated by & and \\ respectively. Horizontal lines that
span the width of the table can be inserted using the \hline command. For example, the source
\begin{table}[htb]
\centering
\caption{A simple table}
\label{tab:simple table}
\begin{tabular}{| l | c | r |}
\hline \hline
\(x\) & \(x^2\) & \(x^3\) \\
\hline
2 & 4 & 8 \\
4 & 16 & 64 \\
6 & 36 & 216 \\
8 & 64 & 512 \\
10 & 100 & 1000 \\
\hline \hline
\end{tabular}
\end{table}
produces
5 Referencing
One of the main benefits of using LATEX over other word processing programs is that it is very easy to
insert references citing a source and use cross-referencing within a document.
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authoryear,natbib]{biblatex}
\addbibresource{references}
Note that this assumes that the bib file (section 5.1.2) has the filename references.bib.
type Specifies the type of the source being cited (see section 5.1.3 for
information about the various types of sources.
@refkey A unique text string used by LATEX to identify the source. A
common method of generating the refkey is to combine the first
author’s name with the year of publication, e.g., bloggs:2013.
author name The names of the author’s are listed here using the keyword and to
separate each author. Author’s names should take the form other,
a.n. where the surname is given first followed by a comma with
the initials separated by a fullstop.
title The title of the source.
year The year the source was first published.
• @online – webpage
@book{higham:1994,
author = {Higham, N.},
title = {Handbook of Writing for the Mathematical Sciences},
publisher = {SIAM},
year = {1994}
}
@article{shiach:2005,
author = {Shiach, J.B. and Mingham, C.G.},
title = {A temporally second-order accurate Godunov-type scheme for solving the
extended Boussinesq equations},
journal = {Coastal Engineering},
year = {2009},
volume = {56},
pages = {32 -- 45}
}
5.1.5 Citations
Where you want a citation to be inserted, use a variation of the \cite command depending on the type
of citation given in the list below.
Citation command Format
\citet{refkey} name (year)
\citep{refkey} (name, year)
\citeauthor{refkey} name
\citeyear{refkey} year
\citeyearpar{refkey} (year)
For example, the source
An excellent guide for writing mathematical documents was written by
\citet{higham:1994}.
produces (provided the bib file entry from section 5.1.3 was included in the bib file)
An excellent guide for writing mathematical documents was written by Higham (1994).
If you don’t want the author’s name to be included in the sentence use the \citep{...} command. For
example,
An excellent guide for writing mathematical documents is ‘Handbook of writing for the
mathematical sciences’ \citep{higham:1994}.
produces
An excellent guide for writing mathematical documents is ‘Handbook of writing for the mathematical
sciences’ (Higham, 1994).
5.1.6 Bibliography
In order for sources to be cited in a LATEX document it is necessary to insert a bibliography using the
following commands
\setlength\bibitemsep{2\itemsep}
\printbibliography
These commands will insert a new section titled ‘References’ that contains a list of all of the references
cited in the document using a Harvard style format, for example
References
N.J. Higham. (1994). Handbook of writing for the mathematical sciences. SIAM.
\label{marker}
where marker is a unique text string for the item. Once a numbered item has been marked, cross-
referencing is achieved using the \ref command:
\ref{marker}
This will insert the item number into the document. The page where the item appears in the document
can be determined using the \pageref command:
\pageref{marker}
For example, the source
A useful package for cross-referencing is the cleveref package which can automatically append the
type of item that is being cross-referenced to the item number (e.g., fig., table., section etc.). First include
the package in your preamble using
\usepackage{cleveref}
5.2.1 Markers
There is no restriction to the text used for the marker in the label command as long as it does not
contain special LATEX characters. However, since documents can contain hundreds of markers, it is good
practice to have a marking convention in order to avoid duplication. A common convention is to use a
shorthand prefix, for example
chap: chapter
sec: section
fig: figure
tab: table
eq: equation
itm: enumerated list item
For example, the image of the Mandelbrot set in fig. 1 had the marker fig:mandelbrot set.
References
Higham, N. (1994). Handbook of Writing for the Mathematical Sciences. SIAM.
Unknown (2013). Text formatting with LATEX. [Online; accessed 10 September 2013]. Economics Graduate
Student Association, University of North Carolina. url: http://www.unc.edu/depts/econ/egsa/
LaTeX.pdf.
Wikibooks (2013). LATEX— Wikibooks, The Free Textbook Project. [Online; accessed 10 September 2013].
url: https://en.wikibooks.org/wiki/LaTeX.
A Article template
Below is a template for an article using some common packages. You can copy and paste this into a new
tex file to begin creating a LATEX document.
\documentclass[a4paper,twoside,11pt]{article}
% packages
\usepackage[margin=2.54cm]{geometry}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[backend=bibtex,citestyle=authoryear,bibstyle=authoryear,natbib]{biblatex}
\addbibresource{references}
\usepackage{tikz}
\usetikzlibrary{arrows,patterns,calc,matrix}
\usepackage{caption}
\captionsetup[table]{skip=2pt}
\usepackage{placeins}
\usepackage{graphicx}
\usepackage{listings}
\usepackage[protrusion=true,expansion=true]{microtype}
\usepackage{cleveref}
\usepackage{hyperref}
% top matter
\title{Your title}
\author{Your name}
% document
\begin{document}
% title
\maketitle
% document text
...
% bibliography
\setlength\bibitemsep{2\itemsep}
\printbibliography
\end{document}