Académique Documents
Professionnel Documents
Culture Documents
Using LaTex
Umair Rafique (Lecturer)
CS Department,
Air University, Islamabad
Why Bother?
Umair Rafique
Course Introduction
Text Formatting
Tables, Figures, Algorithms
Mathematical Expressions, Theorems
Reference (Bibliography) Management
Much more
Umair Rafique
Bonus
Umair Rafique
Instructor Introduction
LaTex?
Homepage: http://www.ntu.edu.sg/home5/umai0001/
Google: Umair Rafique
Umair Rafique
Course Syllabus
16th July
Session 1 (9:00 to 10:30) Introduction to Latex, Installation, Creating a
Simple Document
Session 2 (11:00 to 12:30) Authors and Titles, Chapters, Sections, Table of
Contents, Labels, Cross-Referencing, Page Styles, Lists
Session 3 (12:45 to 2:15) Figures, Graphs, Tables, and Paragraph
Formatting
17th July
Session 1 (9:00 to 10:30) Bibliography Management using BibTex,
Theorems, Formatting your CV using LaTex.
Session 2 (11:00 to 12:30) Writing Algorithms, Mathematical Expressions
and Equations
Session 3 (12:45 to 2:15) Using Well known Latex Templates (IEEE,
Elsevier, Springer)
Umair Rafique
Session 1
Introduction to LaTex, Installation,
Creating a Simple Document
Outline
TexStudio
TexWorks
TeXnic Center
TexMakerX
Umair Rafique
Tex
Umair Rafique
LaTex I
LaTex is a document preparation systems based on TeX and includes prebuilt macros.
LaTeX provides a high-level language that accesses the power of TeX.
LaTeX essentially comprises a collection of TeX macros and a program to
process LaTeX documents.
Because the TeX formatting commands are very low-level, it is usually much
simpler for end-users to use LaTeX.
It was created by Leslie Lamport in early 1980s.
Its purpose is to simplify TeX typesetting, especially for documents
containing mathematical formulae.
Many authors have contributed extensions, called packages or styles
Some of these are bundled with most TeX/LaTeX software distributions.
More can be found in the Comprehensive TeX Archive Network ( CTAN).
Umair Rafique
10
LaTex II
Using LaTex
You can concentrate purely on the structure and contents of the document
and not worry about layout issues.
You don't need to manually adjust fonts, text sizes etc as LaTeX takes care
of them automatically.
The layout, fonts, tables etc. are consistent throughout the document.
Mathematical formulae can be easily typeset.
Indexes, footnotes, citations and references are generated easily.
You dont see what the final version will look like while typing. Instead you
see the logical structure of the document.
The LaTeX document is a plain text file containing the content of the
document, with additional markup.
Umair Rafique
11
PDFLaTex
An extension to LaTex
The main difference between LaTeX and PDFLaTeX is that whereas
LaTeX outputs DVI (DeVice Independent) files, PDFLaTeX can
output PDF files directly.
This allows tight integration of PDF features such as hypertext links
and tables of contents.
Umair Rafique
12
13
LaTex Editors
Umair Rafique
14
Umair Rafique
15
TeXnic Center
Umair Rafique
16
TexMakerX
Umair Rafique
17
TeXStudio
Umair Rafique
18
TeXworks
Umair Rafique
19
Umair Rafique
20
ProText
Latest MikTex
TexWorks
TexMakerX
A huge collection of LaTex Packages
Umair Rafique
21
Installation Procedure
Using ProText
Umair Rafique
22
ProText Folder
Umair Rafique
23
Umair Rafique
24
MikTex Installation 1
Umair Rafique
25
MikTex Installation 2
Umair Rafique
26
MikTex Installation 3
Umair Rafique
27
MikTex Installation 4
Umair Rafique
28
Umair Rafique
29
Umair Rafique
30
TexMakerX Installation 1
Umair Rafique
31
TexMakerX Installation 2
Umair Rafique
32
TexMakerX Installation 3
Umair Rafique
33
TexMakerX Installation 4
Umair Rafique
34
TexMakerX Installation 5
Umair Rafique
35
TexMakerX Installation 6
Umair Rafique
36
TexMakerX Installation 7
Umair Rafique
37
Umair Rafique
38
A Basic Document
\documentclass[11pt]{article}
\begin{document}
This is my very first document in LaTex.
\end{document}
39
Some Basics
Spaces
Several whitespace characters are treated as one space
An empty line is considered end of a paragraph.
Several empty lines are treated as a single empty line.
Comments
% is symbol is used to denote comments
Any text written after the % symbol will not be shown in the final
document
Newline
\\
Umair Rafique
40
Umair Rafique
41
Umair Rafique
42
Tables
Umair Rafique
43
Mathematical Expressions
Umair Rafique
44
Umair Rafique
45
Theorems
Umair Rafique
46
Umair Rafique
47
End of Session 1
Umair Rafique
48
Session 2
Titles and Authors, Chapters,
Sections, Table of Contents,
Labels, List Environments and
more
50
Multiple Authors
Alternatively you can make your own title page (next slide)
Umair Rafique
51
52
Umair Rafique
53
Abstract
\begin{document}
\begin{abstract}
Your abstract goes here
\end{abstract}
\end{document}
Umair Rafique
54
Sectioning Commands
\begin{document}
\begin{abstract}
\end{abstract}
\section{Section 1}
\end{document}
Umair Rafique
55
This will not go in TOC. To add an entry into TOC you can use
\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}
Umair Rafique
56
Section Labels
Umair Rafique
57
Table of Contents
All auto-numbered headings get into TOC automatically.
Can insert TOC using the command
\tableofcontents
List of figures and list of tables can also be added using similar
command \listoffigures, \listoftables
To add an entry into TOC you can use (as done before)
\addcontentsline{toc}{section}{Introduction}
Replace toc with lof or lot for figures and tables respectively
Umair Rafique
58
Umair Rafique
59
Appendices
In report or book
\appendix
\chapter{first appendix}
In article
\appendix
\section
Umair Rafique
60
If you want to start a new chapter or section from the right page (in
case of double sided printing), you can use the following command
before the new section or chapter heading
\cleardoublepage
Umair Rafique
61
Umair Rafique
62
Page Numbering
\pagenumbering {arabic}
It resets the page number to 1.
Umair Rafique
63
Using Packages
The preamble is everything from the start of the LaTeX source file
until the \begin{document} command
It normally includes commands which affect the whole document
If you want to use external packages, the \usepackage command is
also put in the preamble.
\usepackage{packagename}
Umair Rafique
64
Page Margins
\usepackage[top=3.5cm, bottom=3.5cm,
left=3.5cm,right=2.75cm]{geometry}
Fancy Headers
\usepackage{fancyhdr}
Umair Rafique
65
Umair Rafique
66
fancyhdr example
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{\bfseries The performance of new graduates}
\lfoot{From: K. Grant}
\cfoot{To: Dean A. Smith}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
Umair Rafique
67
Umair Rafique
68
\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
Umair Rafique
69
Umair Rafique
70
Umair Rafique
71
Umair Rafique
72
Umair Rafique
73
List Environments
Umair Rafique
74
paralist Package
Umair Rafique
75
Common Errors
Too Many }s
opening curly brace is missing
Runaway argument
closing curly brace is missing
Umair Rafique
76
Common Warnings
Underful \hbox
This is a warning that LaTeX cannot stretch the line wide enough to fit,
without making the spacing bigger than its currently permitted maximum
Overful \hbox
An overfull \hbox means that there is a hyphenation or justification
problem: moving the last word on the line to the next line would make
the spaces in the line wider than the current limit; keeping the word on
the line would make the spaces smaller than the current limit, so the
word is left on the line, but with the minimum allowed space between
words, and which makes the line go over the edge
Umair Rafique
77
Footnotes
\footnote{}
Umair Rafique
78
End of Session 2
Umair Rafique
79
Session 3
Figures, Graphs, Tables, and
Paragraph Formatting
Figures
Umair Rafique
81
Preparing Figures
Powerpoint
Example Figures
Print the figure to PDF printer
Try to avoid using jpg images
Example
Umair Rafique
82
Figures in LaTex
Umair Rafique
83
Figures in LaTex
position specfier
h (here), t (top), b (bottom), p(page)
Default is tbp
\label
Figure with a label \label{fig11} can be referred to as \ref{fig1}
Umair Rafique
84
It wont happen when it's really impossible, e.g you place a table
here and this place would be the last line on a page.
Alternatively you can use [H] which is provided from package
rotfloat.
Examples
Umair Rafique
85
\begin{figure}[ht]
\centering
\includegraphics[width=.9\columnwidth]{fig_example.pdf}
\caption{Figure Caption}
\end{figure}
Umair Rafique
86
Sideways Figures
\begin{sidewaysfigure}[H]
\centerline{\includegraphics[width=8.5in]{figurename}}
\caption{Figure Caption}
\label{label}
\end{sidewaysfigure}
Umair Rafique
87
Including Graphs
Umair Rafique
88
GNU Plot
http://www.gnuplot.info/
http://t16web.lanl.gov/Kawano/gnuplot/index-e.html
Save the graph as a PDF file.
Include it into your LaTex document as a Figure
Umair Rafique
89
Umair Rafique
90
Umair Rafique
91
Umair Rafique
92
Umair Rafique
93
Umair Rafique
94
MS Excel
Simple Graphs
Demo
Print it to PDF and include it as a Figure
Umair Rafique
95
Umair Rafique
96
Tables
Umair Rafique
97
Creating Tables
\begin{table}[position specifier]
\centering
\begin{tabular}{|l|}
... your table ...
\end{tabular}
\caption[Short Caption for List of Tables]{This table shows some data}
\label{tab:myfirsttable}
\end{table}
position specfier
h (here), t (top), b (bottom), p(page)
Default is tbp
Umair Rafique
98
\caption
Caption on top of the table
\begin{table}[position specifier]
\caption{}
\label
Table with a label \label{table1} can be referred to as \ref{table1}
Umair Rafique
99
You can force LaTeX to use one given position. E.g. [!h] forces
LaTeX to place it exactly where you place it.
It wont happen when it's really impossible, e.g you place a table
here and this place would be the last line on a page.
Alternatively you can use [H] which is provided from package
rotfloat.
Examples
Umair Rafique
100
tabular Environment
The tabular environment can be used to typeset tables with optional horizontal
and vertical lines.
LaTeX determines the width of the columns automatically.
The first line of the environment has the form
\begin{tabular}{table spec}
The table spec argument tells LaTeX the alignment to be used in each column
and the vertical lines to insert.
The number of columns does not need to be specified as it is inferred by
looking at the number of arguments provided.
It is also possible to add vertical lines between the columns here.
|, ||,
l, c, r
\begin{tabular}{cccc}
\begin{tabular}{|c|c|c|c|}
Umair Rafique
101
Umair Rafique
102
Umair Rafique
103
Umair Rafique
104
\begin{tabular}{ | l | l | l | p{5cm} |}
Umair Rafique
105
\begin{tabular}{cccccc}
\begin{tabular}{*{6}{c}}
Umair Rafique
106
\multicolumn{num_cols}{alignment}{contents}
num_cols is the number of subsequent columns to merge
alignment is either l, c, r, or to have text wrapping specify a width
like p{5.0cm} .
contents is simply the actual data you want to be contained within
that cell.
Umair Rafique
107
Umair Rafique
108
size can be
\tiny
\scriptsize
\footnotesize
\small
\normalsize
\large
\Large
\LARGE
\huge
\Huge
Umair Rafique
109
Sideways Tables
\usepackage{rotating}
\begin{sidewaystable}
\begin{tabular}...
\end{tabular}
\end{sidewaystable}
Umair Rafique
110
Umair Rafique
111
Umair Rafique
112
Umair Rafique
113
Umair Rafique
114
Booktabs package
Umair Rafique
115
Example
Umair Rafique
116
exceltolatex
Umair Rafique
117
Paragraph Formatting
Umair Rafique
118
Paragraph Alignment
flushleft
flushright
center
Use \begin{} and \end{}
Umair Rafique
119
Paragraph Indents
Umair Rafique
120
Line Spacing
\usepackage{setspace}
\doublespace
\onehalfspace
\singlespace
These will specify the line spacing for all sections and paragraphs
until another command is used.
Umair Rafique
121
Verbatim Text
Umair Rafique
122
Verbatim Cont..
Umair Rafique
123
Typesetting URLs
Umair Rafique
124
Listing Environment
Umair Rafique
125
End of Session 3
Umair Rafique
126
Session 4
Bibliography Management,
Theorems,
Formatting your CV using LaTex
If you are writing only one or two documents and aren't planning on
writing more on the same subject for a long time, maybe you don't
want to waste time creating a database of references you are never
going to use.
In this case you should consider using the basic and simple
bibliography support that is embedded within LaTeX.
LaTeX provides an environment called thebibliography that you
have to use where you want the bibliography (usually at the very
end of your document, just before the \end{document} command.)
Umair Rafique
128
Example
129
BibTex
Umair Rafique
130
How it Works?
It takes as input
an .aux file produced by LaTeX on an earlier run;
a .bst file (the style file), which specifies the general reference-list style
and specifies how to format individual entries
.bib file constituting a database of all reference-list entries the user
might ever hope to use.
BibTeX chooses from the .bib file only those entries specified by
the .aux file (which would be those given by LaTeX's \cite
commands).
It creates as output a .bbl file containing these entries together with
the formatting commands specified by the .bst file
LaTeX will use the .bbl file to produce the reference list
Umair Rafique
131
Umair Rafique
132
.bib file
Umair Rafique
133
134
address: Publisher's address (usually just the city, but can be the
full address for lesser-known publishers)
author: The name(s) of the author(s) (in the case of more than one
author, separated by and)
booktitle: The title of the book, if only part of it is being cited
chapter: The chapter number
edition: The edition of a book, long form (such as "first" or
"second")
editor: The name(s) of the editor(s)
Umair Rafique
135
136
Umair Rafique
137
138
139
140
141
Umair Rafique
142
Include the following command at the place where you want your
Reference section to appear
\bibliography{BIB_all}
\bibliographystyle{abbrv}
Here BIB_all is the name of the .bib file while abbrv is the
bibliography style.
Cite a reference using the \cite command.
Compile the LaTex document at least three times to get all the
references right.
Examples.
Umair Rafique
143
Umair Rafique
144
.bst File
Umair Rafique
145
Umair Rafique
146
Umair Rafique
147
Umair Rafique
148
Umair Rafique
149
Umair Rafique
150
Theorem
With theorem we can mean any kind of labelled part that we want to
look separated from the rest of the text and with sequential numbers
next to it.
This approach is commonly used for theorems in mathematics, but
can be used for anything.
Examples
Definitions
\package{amsthm}
Umair Rafique
151
Basic Theorems
\newtheorem{name}{Printed output}
The first argument is the name you will use to reference it, the
second argument is the output LaTeX will print whenever you use it.
\newtheorem{mydef}{Definition} will define the mydef
environment; if you use it like this
\begin{mydef}
Here is a new definition
\end{mydef}
Umair Rafique
152
Theorem Numbering
Umair Rafique
153
Theorem Numbering
Umair Rafique
154
Proof Environment
\begin{proof}
Here is my proof
\end{proof}
If you would like to manually name the proof, include the name in
square brackets
\begin{proof}[Proof of important theorem]
Here is my important proof
\end{proof}
\qedhere command
Examples
Umair Rafique
155
Theorem Styles
Example
Umair Rafique
156
Formatting your CV
Example
Umair Rafique
157
End of Session 4
Umair Rafique
158
Session 5
Writing Algorithms, Mathematical
Expressions and Equations
Umair Rafique
160
Umair Rafique
161
Umair Rafique
162
Some Basics
\usepackage[options ]{algorithm2e}
\begin{algorithm}
\end{algorithm}
163
164
165
Options Cont..
166
SetVLine
Umair Rafique
167
\KwData{input}
\KwResult{output}
\KwIn{input}
\KwOut{output}
Umair Rafique
168
Comments
Umair Rafique
169
if-then-else commands
\If{condition}{then block}
\If(then comment){condition}{then block}
\lIf{condition}{thens line text}
\ElseIf{elseif block}
\lElseIf{elseifs line text}
\Else{else block}
\lElse{elses line text}
\eIf{condition}{then block}{else block}
Umair Rafique
170
Loops
\For{condition}{text loop}
\lFor{condition}{line text loop}
\While{condition}{text loop}
\lWhile{condition}{line text loop}
\ForEach{condition}{text loop}
\lForEach{condition}{line text loop}
\ForAll{condition}{text loop}
\lForAll{condition}{line text loop}
171
Examples
Umair Rafique
172
Examples cont..
Umair Rafique
173
Examples cont..
Umair Rafique
174
Examples cont..
Umair Rafique
175
Umair Rafique
176
Example
Umair Rafique
177
Mathematical Expressions
and Equations using amsmath
Source: LaTex wikibook (Ch 16
and 17)
Umair Rafique
178
Some Basics
\usepackage{amsmath}
or
\usepackage{mathtools}
text - text formulas are displayed in-line, that is, within the body of
text where it is declared. e.g., I can say that a + a = 2a within this
sentence.
\begin{math}...\end{math} or
\(...\) or
$...$
Umair Rafique
179
Spaces
Umair Rafique
180
\[
\forall x \in X, \quad \exists y \leq \epsilon
\]
\[
\alpha, \Alpha, \beta, \Beta, \gamma, \Gamma,
\pi, \Pi, \phi, \varphi, \Phi
\]
Umair Rafique
181
Operators
\[
\cos (2\theta) = \cos^2 \theta - \sin^2 \theta
\]
\[
\lim_{x \to \infty} \exp(-x) = 0
\]
Umair Rafique
182
\[
k_{n+1} = n^2 + k_n^2 - k_{n-1}
\]
\[
f(n) = n^5 + 4n^2 + 2 _{n=17}
\]
Umair Rafique
183
184
Umair Rafique
185
Continued Fractions
\begin{equation}
x = a_0 + \cfrac{1}{a_1
+ \cfrac{1}{a_2
+ \cfrac{1}{a_3 + a_4}}}
\end{equation}
Umair Rafique
186
Roots
\[
\sqrt{\frac{a}{b}}
\]
\[
\sqrt[n]{1+x+x^2+x^3+\ldots}
\]
Umair Rafique
187
\[
\sum_{i=1}^{10} t_i
\]
\[
\int_0^\infty e^{-x}\,\mathrm{d}x
\]
Umair Rafique
188
Umair Rafique
189
\[
( a ), [ b ], \{ c \}, | d |, \| e \|,
\langle f \rangle, \lfloor g \rfloor,
\lceil h \rceil
\]
\[
\left(\frac{x^2}{y^3}\right)
\]
\[
\left\{\frac{x^2}{y^3}\right\}
\]
Umair Rafique
190
\[
\left.\frac{x^3}{3}\right_0^1
\]
\[
( \big( \Big( \bigg( \Bigg(
\]
Umair Rafique
191
\[
\begin{matrix}
a & b & c \\
d & e & f \\
g&h&i
\end{matrix}
\]
pmatrix ()
bmatrix []
Bmatrix{}
vmatrix | |
Vmatrix || ||
Umair Rafique
192
\[
A_{m,n} =
\begin{pmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
\vdots & \vdots & \ddots & \vdots \\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{pmatrix}
\]
Umair Rafique
193
Umair Rafique
194
\[
50 \text{apples} \times 100 \text{apples}
= \text{lots of apples}^2
\]
195
Formatting Mathematical
Symbols (amsfonts)
Umair Rafique
196
Accents
Umair Rafique
197
\,
\;
\:
\quad
\qquad
Umair Rafique
198
dots
\dots
\ldots
\cdots
\vdots
\ddots
Umair Rafique
199
Math Symbols
Umair Rafique
200
Umair Rafique
201
Umair Rafique
202
Umair Rafique
203
Umair Rafique
204
Umair Rafique
205
Equations
\begin{equation}
f(x)=(x+a)(x+b)
\end{equation}
\begin{equation} \label{eq:someequation}
5^2 - 5 = 20
\end{equation}
this references the equation \ref{eq:someequation}.
Umair Rafique
206
Referencing Equations
\begin{equation} \label{eq:erl}
a = bq + r
\end{equation}
where \eqref{eq:erl} is true if $a$ and $b$ are integers with $b \neq
c$.
Umair Rafique
207
\documentclass[12pt]{article}
\usepackage{amsmath}
\numberwithin{equation}{subsection}
\begin{document}
\section{First Section}
\subsection{A subsection}
\begin{equation}
L = {L}{\sqrt{1-\frac{v^2}{c^2}}}
\end{equation}
\end{document}
Umair Rafique
208
Sub-equations
\begin{subequations}
Maxwells equations:
\begin{align}
B&=-\nabla \times E,\\
E&=\nabla \times B - 4\pi j,
\end{align}
\end{subequations}
Umair Rafique
209
Arrows
\[
A \xleftarrow{\text{this way}} B
\xrightarrow[\text{or that way}]{} C
\]
Umair Rafique
210
More Arrows
\[
a \xleftrightarrow[under]{over} b\\
%
A \xLeftarrow[under]{over} B\\
%
B \xRightarrow[under]{over} C\\
%
C \xLeftrightarrow[under]{over} D\\
%
D \xhookleftarrow[under]{over} E\\
%
E \xhookrightarrow[under]{over} F\\
%
F \xmapsto[under]{over} G\\
\]
Umair Rafique
211
\begin{align}
f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\
&\qquad {} + 10x + 12
\end{align}
Umair Rafique
212
\begin{align}
f(x) &= \pi \left\{ x^4 + 7x^3 + 2x^2 \right.\nonumber\\
&\qquad \left. {} + 10x + 12 \right\}
\end{align}
Umair Rafique
213
\[
u(x) =
\begin{cases}
\exp{x} & \text{if } x \geq 0 \\
1 & \text{if } x < 0
\end{cases}
\]
\[
a=
\begin{dcases}
\int x\, dx\\
b^2
\end{dcases}
\]
Umair Rafique
214
\[
f(x) = \begin{dcases*}
x & when $x$ is even\\
-x & when $x$ is odd
\end{dcases*}
\]
Umair Rafique
215
\begin{equation}
\left.\begin{aligned}
B&=-\partial \times E,\\
E&=\partial \times B - 4\pi j,
\end{aligned}
\right\}
\qquad \text{Maxwells equations}
\end{equation}
Umair Rafique
216
split
\begin{equation}\label{xx}
\begin{split}
a& =b+c-d\\
& \quad +e-f\\
& =g+h\\
& =i
\end{split}
\end{equation}
Umair Rafique
217
End of Session 5
Umair Rafique
218
Session 6
Working With Complete Latex
Templates
Outline
Elsevier
IEEE
Springer
Thesis
Umair Rafique
220
End of Session 6
And the end of our course as well
Umair Rafique
221
Thank You
Good Luck
Umair Rafique
222