Main Latex
Main Latex
LATEX DOCUMENTATION
1. INTRODUCTION
If we are used to producing documents with Microsoft Word, we will find that LATEX is a very
different style of working. Microsoft Word is ‘What You See Is What You Get’ (WYSIWYG),
this means that you see how the final document will look as you are typing. When working in
this way you will probably make changes to the document’s appearance (such as line spacing,
headings, page breaks) as you type. With LATEX you do not see how the final document will
look while you are typing it — this allows you to concentrate on the content rather than
appearance.
A LATEX document is a plain text file with a .tex file extension. It can be typed in a simple text
editor such as Notepad, but most people find it is easier to use a dedicated LATEX editor. As we
type you mark the document structure (title, chapters, subheadings, lists etc.) with tags. When the
document is finished you compile it — this means converting it into another format. Several
different output formats are available, but probably the most useful 1 is Portable Document
Format (PDF), which appears as it will be printed and can be transferred easily between
computers
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}
The \documentclass command must appear at the start of every LATEX document.
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.
Name your document Doc1 and save it as a TeX document in this folder. It is a good idea
to keep each of your LATEX documents in a separate folder as the compiling process
creates multiple files.
Make sure the typeset menu is set to pdfLaTeX.
Click on the Typeset button. There will be a pause while your document is being
converted to a PDF file. When the compiling is complete TeXworks’ PDF viewer will
open and display your document. The PDF file is automatically saved in the same folder
as the .tex file.
The \maketitle command creates a title. You need to specify the title of the document. If the
date is not specified today’s date is used. Author is optional.
\author{My Name}
\date{\today}
\maketitle
Points to note:
• Article documents start the text immediately below the title on the same page. Reports put the
title on a separate page.
2.3 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 {...}.
2.4 Labelling:
We 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.
\documentclass[a4paper, 12pt]{article}
\begin{document}
\author{My Name}
\date{\today}
\maketitle
\section{Introduction}
\section{Methods}
\subsection{Stage 1}
\subsection{Stage 2}
\section{Results}
Type \tableofcontents where you want the table of contents to appear in your document — often
directly after the title page.
We may also want to change the page numbering so that roman numerals (i, ii, iii) are used for
pages before the main document starts. This will also ensure that the main document starts on
page.
Page numbering can be switched between Arabic and roman using \pagenumbering {...}.
\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.
3. TYPESETTING TEXT
To put coloured text in your document you need to use a package. There are many
packages that can be used with LATEX to enhance its functionality. Packages are included in the
preamble (i.e. before the \begin{document} command).
Packages are activated using the \usepackage[options]{package} command, where package is the
name of the package and options is an optional list of keywords that trigger special features in
the package.
Where colour_name is the name of the colour you want, and text is the text you want to be
coloured.
\xdefinecolor{lavendar}{rgb}{0.8,0.6,1}
\colorlet{mygreen}{green!60!gray}
which means 60% green + 20% gray.
When a color is needed, you may use directly the method above (e.g., blue!70 for having
a 70% blue).
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].
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 typographical points (pt). For example,
\vspace{12pt} will add space equivalent to the height of a 12pt font.
The following symbols are reserved characters which have a special meaning in LATEX:
#$%^&_{}~\
All of these apart from the backslash \ can be inserted as characters in your document by adding
a prefix backslash:
Note that you need to type a pair of curly brackets {} after the hat ^ and tilde ~, otherwise these
will appear as accents over the following character. For example, “\^ e” produces “ˆe”. The
above code will produce:
#$%ˆ&{}˜
The backslash character \ cannot be entered by adding a prefix backslash, \\, as this is used for
line breaking. Use the \textbackslash command instead.
4. TABLES
The tabular command is used to typeset tables. By default, LATEX tables are drawn
without horizontal and vertical lines — you need to specify if you want lines drawn. LATEX
determines the width of the columns automatically. This code starts a table: \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 command \end{tabular} finishes the table. Examples of tabular code and the resulting
tables:
Eg: \begin{tabular}{|l|l|}
Apples & Green \\
Strawberries & Red \\
Oranges & Orange \\
\end{tabular}
Eg: \begin{tabular}{rc}
Apples & Green \\
5. FIGURES
SKIT, Dept of CSE Page 11
LATEX DOCUMENTATION
Images should be PDF, PNG, JPEG or GIF files. The following code will insert an image
called myimage:
\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).
We 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.
\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).
We could also use [scale=0.5] which scales the image by the desired factor, in this case reducing
by half.
\caption {...} defines a caption for the figure. If this is used LATEX will add “Figure” and a
number before the caption.
If we use captions, we can use \listoffigures to create a table of figures in a similar way to the
table of contents.
\label {...} creates a label to allow you to refer to the table or figure in your text.
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’.
We can enter math mode with an opening and closing dollar sign $. This can be used to
write mathematical symbols within a sentence — for example,
Typing $1+2=3$
Produces 1 + 2 = 3.
If we want a “displayed” equation on its own line use $$...$$. For example, $$1+2=3$$
produces: 1 + 2 = 3
1+2=3
The number 6 refers to the chapter number; this will only appear if you are using a document
class with chapters, such as report.
\begin{eqnarray}
a & = & b + c \\
&=&y-z
\end{eqnarray}
Produces:
a=b+c
=y−z
For unnumbered equations add the star symbol * after the equation or eqnarray
Command (i.e. use {equation*} or {eqnarray*}).
This section is a very brief introduction to using LATEX to produce mathematical symbols
— the Mathematics chapter in the LATEX Wikibook is an excellent tutorial on mathematical
symbol commands, which you should refer to if we want to learn more. If you want to find the
command for a specific symbol try Detexify1, which can recognise hand drawn symbols.
For example,
$2_a$ produces 2a. If the power or index includes more than one character, group them using
curly brackets {...}, e.g. $b_{a-2} $ producesb a−2.
6.2.2 Fractions:
$$\frac{a}{3} $$ produces:
a
b
y
3
+b
x
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: √ y2
$$\sqrt[x]{y^2}$$ produces: √x y 2
6.2.4 Sums & Integrals:
The command \sum inserts a sum symbol; \int inserts an integral. For both functions, the
upper limit is specified by a hat ˆ and the lower by an underscore _
∑ yz
x=1
∫ f (x)
a
Greek letters can be typed in math mode using the name of the letter preceded by a
backslash \. Many Greek capital letters are used in the Latin 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$ = θ, Θ
$\sigma, \Sigma$ = σ, Σ
$\phi, \Phi$ = φ, Φ
$\psi, \Psi$ = ψ, Ψ
$\omega, \Omega$ = ω, Ω
7. INSERTING REFERENCES
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.
The BibTeX file contains all the references you want to cite in your document. It has the
file extension .bib. It should be given the same name as and kept in the same folder as our .tex
file.
The .bib file is plain text - it can be edited using Notepad or your LATEX editor (e.g.
TeXworks). You should enter each of your references in the BibTeX file in the following format:
@article {
Birdetal2001,
Volume = {50},
Pages = {9-19},
Year = {2001}}
Each reference starts with the reference type (@article in the example above).
@book, @incollection for a chapter in an edited book and @inproceedings for papers presented
at conferences.
The reference type declaration is followed by a curly bracket, then the citation 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 is probably easiest to keep track of.
The remaining lines contain the reference information in the format Field name =
{field contents}, we need to include LaTeX commands in your BibTeX file for any special text
formatting - e.g.
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 contemporary 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”.
We can type the BibTeX file yourself, or you can use reference management software such as
EndNote to create it.
Type the following where you want the bibliography to appear in your document
(usually at the end):
\bibliographystyle{plain}
\bibliography{Doc1}
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
\notice {citationkey}.
To include a page number in your in-text citation put it in square brackets before the citation
key:
SKIT, Dept of CSE Page 17
LATEX DOCUMENTATION
To cite multiple references include all the citation keys within the curly brackets separated by
commas: \cite{citation01, citation02, citation03}.
7.4 Styles:
LATEX comes with several styles with numerical in-text citations, these include:
Plain:
The citation is a number in square brackets (e.g. [1]). The bibliography 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.
Alpha:
The same as plain except the citation is an alphanumeric abbreviation based on the
author(s) surname(s) and year of publication, surrounded by square brackets (e.g. [Kop10]).
Use the natbib package if you want to include author-date citations. Natbib uses the
command \citep {...} for a citation in brackets (e.g. [Koppe, 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.
These format the bibliography in the same way as the plain, abbrv and unsrt styles, respectively.
If we want to use a different style (e.g. one provided by the journal you are submitting an
article to) you should save the style file (.bst file) in the same folder as your .tex and .bib files.
Include the name of the .bst file in the \bibliographystyle {...} commmand.
8. CONCLUSION
LaTeX gives us flexibility that we don’t need to see how the final document will look
while you are typing it — this allows you to concentrate on the content rather than appearance.
LaTeX allows you to make more consistent, and more easily changeable, documents. LaTex is a
typesetting and document preparation system that “includes features designed for the production
of technical and scientific documentation.” It means that you can use LaTex to create documents
with text and formatting that would be difficult in a standard word processor.
9. REFERENCES
[1] http://www.latex-project.org/
[2] http://ctan.tug.org/tex-archive/info/lshort/english/lshort.pdf
[3] https://en.wikibooks.org/wiki/LaTeX
[4] http://edin.ac/17EQPM1
[5] http://en.wikipedia.org/wiki/Comparison_of_TeX_editors