Vous êtes sur la page 1sur 222

A Short Course on

Using LaTex
Umair Rafique (Lecturer)
CS Department,
Air University, Islamabad

Why Bother?

Preparing professional quality documents


Changing Text Formatting Style
Bibliography Management
Changing reference style
Availability of LaTex Templates for almost all journals and
conferences.
Very few provide MSWord Templates

Umair Rafique

Course Introduction

Two days course (16th and 17th July)


How to use LaTex for producing professional quality documents

Text Formatting
Tables, Figures, Algorithms
Mathematical Expressions, Theorems
Reference (Bibliography) Management
Much more

Six 90 minute sessions (three on each day)


Will cover all the basics of document formatting using LaTex

Umair Rafique

Bonus

Simplified, ready to use, templates for some major publishers (IEEE,


Elsevier and Springer)
A complete template for MS and PhD thesis
All LaTex related installation setups
Tips on how to benefit from MS Office for use in LaTex

Umair Rafique

Instructor Introduction

Ph.D. (Thesis Accepted), Nanyang Technological University,


Singapore.
Area of Research: Artificial Intelligence
Intelligent Agents
Action Representation for Commonsense Reasoning

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

Terminology: Tex, LaTex, PDF LaTex, MikTex, ProText


Tex Editors

TexStudio
TexWorks
TeXnic Center
TexMakerX

Installing Latex using ProText


Installing the Tex Editor TexMakerX
Creating a Basic Document
What Latex Can Really Do?

Umair Rafique

Tex

Tex is a low-level markup and programming language created by


Donald Knuth in 1977 to typeset documents attractively and
consistently.
The fine control TeX offers makes it very powerful, but also difficult
and time-consuming to use.
TeX is renowned for being extremely stable, for running on many
different kinds of computers, and for being virtually bug free.
However it requires a consistent stage of learning, and additional
times building custom macros for text formatting.

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

LaTeX Distributions: MikTex

LaTeX distribution is a collections of packages and programs


The programs you expect to find in any LaTeX distribution are

latex compiler reads a LaTeX .tex file and creates a .dvi


pdflatex compiler reads a LaTeX .tex file and creates a .pdf
dvi2ps converts the .dvi file to .ps (postscript).
dvi2pdf converts the .dvi file to .pdf
and a lot of packages

MikTex (for Windows)


MiKTeX can update itself by downloading new versions of previously
installed components and packages, and has an easy installation
process. Additionally, it can ask users whether they wish to download
any packages that have not yet been installed but are requested by the
current document.
Umair Rafique

13

LaTex Editors

Provide an easy to use interface for writing and compiling


documents in Latex
Take care of all the document processing connecting to LaTex
engine in the background
It is always good to install LaTex first and then any LaTex Editor

Normally include an integrated viewer so you can write your Latex


document and see the resulting PDF document all using the same
editor
Include many useful features like spell checking, menus for inserting
symbols etc.
Dozens of LaTex editors are available

Umair Rafique

14

Comparison of Four (freely


available) LaTex Editors

Umair Rafique

15

TeXnic Center

Umair Rafique

16

TexMakerX

Umair Rafique

17

TeXStudio

Umair Rafique

18

TeXworks

Umair Rafique

19

Choosing a LaTex Editor

All of them are more or less similar


Simple vs Intelligent
Personal choice

Umair Rafique

20

ProText

A complete package which includes everything you need to run


LaTex

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

Setup: Install MikTex

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

This would have installed MikTex


andTexWorks

Umair Rafique

29

Setup: Install TexMakerX

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

Creating a Basic Document

Umair Rafique

38

A Basic Document
\documentclass[11pt]{article}
\begin{document}
This is my very first document in LaTex.
\end{document}

Format for the first line is \documentclass[options]{class}


Class specifies the type of document
Standard classes available as well as customized classes
article, report, IEEEtran, book

Options specify font size, page size etc.


10pt, 11pt, a4paper, letterpaper, onecolumn, twocolumn, oneside

\begin{} is start of an environment (document environment in this case)


Umair Rafique

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

What Latex can Really Do?

Umair Rafique

41

TOC, Titles, References

Umair Rafique

42

Tables

Umair Rafique

43

Mathematical Expressions

Umair Rafique

44

More Mathematical Expressions

Umair Rafique

45

Theorems

Umair Rafique

46

Will learn all this stuff in


subsequent sessions

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

Titles and Authors


\begin{document}
\title{My Document}
\author{Umair Rafique}
\date{\today}
\maketitle
\end{document}
\author{Umair Rafique,\\
Air University,\\
Islamabad.\\
\texttt{urafique@mail.au.edu.pk}
}
Umair Rafique

50

Multiple Authors

If there are two authors then use the \and command


\author{Umair Rafique \and Coauthor}
Specifying author using standard classes (like article) is straight
forward and quite fixed.
Class files provided by publishers have different ways of specifying
authors and their related information
Will see some examples in Session 6.

Alternatively you can make your own title page (next slide)

Umair Rafique

51

Custom Title Page


\begin{titlepage}
\title{\vspace{-1in}\textbf{GOAL ADOPTION, PREFERENCE GENERATION AND
COMMONSENSE REASONING IN AUTONOMOUS INTELLIGENT AGENTS}}
\author{
\\[2cm]
\bf{UMAIR RAFIQUE}\\[7cm]
School of Computer Engineering\\[2cm]
{\small A thesis submitted to Nanyang Technological University}\\
{\small in partial fulfilment of the requirement for the degree of}\\
{\small Doctor of Philosophy}\\[1cm]
2012\\}
\date{}
\maketitle
\thispagestyle{empty} % no page number on this front cover page
\end{titlepage}
Umair Rafique

52

Umair Rafique

53

Abstract
\begin{document}
\begin{abstract}
Your abstract goes here
\end{abstract}
\end{document}

Only available in article and report class


Default title is Abstract
Can change it using the following command before the abstract
\renewcommand{\abstractname}{New Title for Abstract}

Umair Rafique

54

Sectioning Commands

\begin{document}
\begin{abstract}
\end{abstract}
\section{Section 1}
\end{document}
Umair Rafique

55

Sections and TOC

All the titles of sections will automatically be included in the table of


contents (TOC) as it is
If you want to display different text inside your table of content use
\Section[Title for TOC]{Title for Text}
Section numbering is done automatically
Unnumbered headings can be specified using the starred version
\Section*{Section 1}

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

\label specifies the label for a section


\section{Section Title}\label{section1}
For referring to a certain section use
In Section \ref{section1} we discussed
\ref{section1} will be replaced with the number of the section
accordingly
If sections had been numbered using decimal it will become Section 1
In case of roman numbering it will become Section I

Umair Rafique

57

Table of Contents
All auto-numbered headings get into TOC automatically.
Can insert TOC using the command
\tableofcontents

The default TOC will list headings of level 3 and above.


To change how deep the table of contents displays automatically the
following command can be used (before \begin{document})
\setcounter{tocdepth}{4}

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

Starting New Section on New


Page

New chapters automatically start on a new page.


To start a new section on a new page use the following command
before the new section,
\newpage

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

Header and Footers

\pagestyle command defines the general contents of the headers


and footers
LATEX has four standard pagestyles:
empty: no headers or footers
plain: no header, footer contains page number centered
headings: no footer, header contains name of chapter/section and/or
subsection and page number
myheadings: no footer, header contains page number and user supplied
information
\pagestyle{empty}

Umair Rafique

62

Page Numbering

\pagenumbering defines the format of the page number.

arabic: arabic numerals


roman: lower case roman numerals
Roman: upper case roman numerals
alph: lower case letter
Alph: upper case letter

\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

Some Useful Packages

Fancy Chapter Headings


\usepackage[Conny]{fncychap}
%Sonny, Lenny, Glenn, Conny, Rejne and Bjarne

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

fancyhdr for Double Sided


Printing

\fancyhead{} % clear all header fields


\fancyhead[RO,LE]{\bfseries The performance of new graduates}
\fancyfoot{} % clear all footer fields
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[LO,CE]{From: K. Grant}
\fancyfoot[CO,RE]{To: Dean A. Smith}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

Umair Rafique

68

fancyhdr With Section


Headings: Default

\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}

Umair Rafique

69

fancyhdr With Section


Headings: Customized

\leftmark: current chapter name printed like "CHAPTER 3. THIS IS


THE CHAPTER TITLE"
\rightmark: current section name printed like "1.6. THIS IS THE
SECTION TITLE"
\leftmark contains the left argument of the last \markboth on the
page
\rightmark contains the right argument of the first \markboth or the
only argument of the first \markright on the page.
You can influence how chapter, section, and subsection information
(only two of them!) is displayed by redefining the \chaptermark,
\sectionmark, and \subsectionmark commands

Umair Rafique

70

Umair Rafique

71

fancyhdr With Custom Section


Headings (example)
\pagestyle{fancy}
\fancyhead{}
\addtolength{\headheight}{\baselineskip}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.0pt}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername\ \thechapter. #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\addtolength{\headsep}{-0.1in}
\fancyhead[LE,LO]{\MakeUppercase{\leftmark}\\\rightmark}
\fancyfoot[C]{\thepage}

Umair Rafique

72

Umair Rafique

73

List Environments

By default two types of list environments are available


\begin{enumerate}
\item This is my first numbered item
\end{enumerate}
\begin{itemize}
\item This is my first bullet item
\end{itemize}

These can be nested in any way.

Umair Rafique

74

paralist Package

\begin{itemize}[$\star$] and * will be used instead of the default


symbol.
\begin{compactitem}[$\triangleright$] A compact version of itemized
\begin{compactenum}[$\triangleright$] A compact version of
enumerate
\begin{enumerate}[(i)] produces the labels (i), (ii), (iii) . . .
\begin{enumerate}[{example} a)] produces example a), example b),
example c)
\begin{inparaenum}[(a)] formats an enumerated list inside a
paragraph
\begin{inparaitem}[(a)] formats an itemized list inside a paragraph

Umair Rafique

75

Common Errors

Too Many }s
opening curly brace is missing

Runaway argument
closing curly brace is missing

Undefined Control Sequence


typo in using some latex command like \secton

Missing Package error


package you requested using \package is not available

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

Include it inside your LaTex document

Umair Rafique

82

Figures in LaTex

In order to introduce graphics within documents, LaTeX just creates


a box with the same size as the image you want to include.
It then embeds the picture inside this box.
\begin{figure}[position specifier]
\centering
%here include your figure
\caption[Short Caption for List of Figures]{Figure Caption}
\label{figure_label}
\end{figure}

Umair Rafique

83

Figures in LaTex

position specfier
h (here), t (top), b (bottom), p(page)
Default is tbp

\centering centers the figure


\caption
Caption on top of the figure
\begin{figure}[position specifier]
\caption{}
Caption at the bottom of the figure
\caption{}
\end{figure}

\label
Figure with a label \label{fig11} can be referred to as \ref{fig1}

Umair Rafique

84

Positioning Your Figure

You can force LaTeX to use one given position.


E.g. [!h] forces LaTeX to place it exactly where you place it.
! tells LaTex not to use the best option and h tells it here

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

Including Figures Using


graphicx Package

\includegraphics[width=xx, height=xx, scale=xx]{imagename}


\includegraphics[width=\linewidth]{imagename}
\includegraphics[width=\textwidth]{imagename}
\includegraphics[width=\columnwidth]{imagename}
\includegraphics[height=\textheight]{imagename}

\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

\centering centers the table

Umair Rafique

98

Captions and Labels

\caption
Caption on top of the table
\begin{table}[position specifier]
\caption{}

Caption at the bottom of the table


\caption{}
\end{table}

\label
Table with a label \label{table1} can be referred to as \ref{table1}

Umair Rafique

99

Positioning Your Table

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

Rows and Columns

& is used to separate columns


\\ is used to start a new row
\hline: horizontal line
\newline: start a new line within a cell
cline{i-j}: partial horizontal line beginning in column I
and ending in column j.

Umair Rafique

102

Umair Rafique

103

Umair Rafique

104

Specifying Column Width

\begin{tabular}{ | l | l | l | p{5cm} |}

Umair Rafique

105

Defining Multiple Columns

\begin{tabular}{cccccc}
\begin{tabular}{*{6}{c}}

Umair Rafique

106

Rows Spanning Multiple


Columns

\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

Changing Size of the Text


{ size
\begin{tabular}
\end{tabular}
}

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}

When it is desirable to place the rotated table at the exact location


where it appears in the source (.tex) file, rotfloat package may be
used.
One can use \begin{sidewaystable}[H] just like for normal tables.
The 'H' option can not be used without this package.

Umair Rafique

110

Alternate Row Colors

The xcolor package provides the necessary commands to produce


tables with alternate row colors
The command
\rowcolors{<starting row>}{<odd color>}{<even color>}
has to be specified right before the tabular environment starts.

Umair Rafique

111

Umair Rafique

112

Partial Vertical Lines

Umair Rafique

113

Wide Tables in Two Columns

\begin{table*} instead of \begin{table}


Example

Umair Rafique

114

Booktabs package

Professional Quality Tables


\toprule
\midrule
\bottomrule
\cmidrule(r){i-j}
Examples

Umair Rafique

115

Formating your Table in MS


Excel

Example

Umair Rafique

116

exceltolatex

An add-on for MS Excel


Simple Installation
Generates LaTex code for MS Excel Tables
Example

Umair Rafique

117

Paragraph Formatting

Umair Rafique

118

Paragraph Alignment

flushleft
flushright
center
Use \begin{} and \end{}

Umair Rafique

119

Paragraph Indents

By default, the first paragraph after a heading follows the standard


practice of no indentation.
The size of subsequent paragraph indents are determined by a
parameter called \parindent.
The default length that this constant holds is set by the document
class that you use. It is possible to override using the \setlength
command.
\setlength{\parindent}{1cm}

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..

Once in the verbatim environment, the only command that will be


recognized is \end{verbatim}. Any others will be output.
The font size in the verbatim environment can be adjusted by
placing a font size command (e.g. \normal) before \begin{verbatim}.
You can use the alltt package to still be able to use some basic
commands inside verbatim. Remember to include \package{alltt}.

Umair Rafique

123

Typesetting URLs

One of either the hyperref or url packages provides the \url


command, which properly typesets URLs.
For example:
Go to \url{http://www.google.com}
When using hyperref package, the URL is clickable while in case
of url package, it is not.

Umair Rafique

124

Listing Environment

This is also an extension of the verbatim environment provided by


the moreverb package.
The extra functionality it provides is that it can add line numbers
along side the text.
\begin{listing}[step]{first line}.
The mandatory first line argument is for specifying which line the
numbering shall commence.
The optional step is the step between numbered lines (the default is
1, which means every line will be numbered).
To use this environment, remember to add \usepackage{moreverb}
to the document preamble

Umair Rafique

125

End of Session 3

Umair Rafique

126

Session 4
Bibliography Management,
Theorems,
Formatting your CV using LaTex

Using Built-in Support

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

thebibliography is the environment


9 is telling LaTeX how wide the item label will be when printed.
9 means one character wide (up to 9 references)
\bibitem is representing one reference item
lamport94 is the key used for citing this reference
A good tutorial on LaTex is \cite{lamport94}

You need to type the reference as you want it to be presented.


Umair Rafique

129

BibTex

BibTex is a reference management software for formatting lists of


references.
BibTeX makes it easy to cite sources in a consistent manner, by
separating bibliographic information from the presentation of this
information.

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

The Database File: .bib

Umair Rafique

132

.bib file

A simple text file saved with .bib extension.


Contains reference entries where each reference entry is composed
of some attributes (fields).
Author Name
Year
Title

One reference entry represents one reference


Each reference entry also has a key which will be used to cite this
reference from a LaTex document.

Umair Rafique

133

Example Reference Entry


@article{Laird_Soar_91,
title = {An Analysis of Soar as an Integrated Architecture},
author = {John E. Laird and Michael Hucka and Scott B. Huffman and Paul S.
Rosenbloom},
journal = {SIGART Bulletin},
number = {4},
pages = {98-103},
volume = {2},
year = {1991},
}
Entry_Type{key,
attribute=value,
}
Umair Rafique

134

Reference Entry Attributes


(Fields)

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

Reference Entry Attributes


(Fields) cont..

institution: The institution that was involved in the publishing, but


not necessarily the publisher
journal: The journal or magazine the work was published in
note: Miscellaneous extra information
number: The "(issue) number" of a journal, magazine, or techreport, if applicable. (Most publications have a "volume", but no
"number" field.)
organization: The conference sponsor
pages: Page numbers, separated either by commas or doublehyphens.
publisher: The publisher's name
school: The school where the thesis was written
Umair Rafique

136

Reference Entry Attributes


(Fields) cont..

series: The series of books the book was published in


Lecture Notes in Computer Science

title: The title of the work


url: The WWW address
volume: The volume of a journal or multi-volume book
year: The year of publication (or, if unpublished, the year of
creation)

Umair Rafique

137

Reference Entry Types

article: An article from a journal or magazine.


Required fields: AUTHOR, TITLE, JOURNAL, YEAR
Optional fields: VOLUME, NUMBER, PAGES, MONTH, NOTE

book: A book with an explicit publisher.


Required fields: AUTHOR/EDITOR, TITLE, PUBLISHER, YEAR
Optional fields: VOLUME/NUMBER, SERIES, ADDRESS, EDITION,
MONTH, NOTE

booklet: A work that is printed and bound, but without a named


publisher or sponsoring institution.
Required fields: TITLE
Optional fields: AUTHOR, HOWPUBLISHED, ADDRESS, MONTH,
YEAR, NOTE
Umair Rafique

138

Reference Entry Types cont..


inbook: A part of a book, usually untitled. May be a chapter (or section
or whatever) and/or a range of pages.
Required fields: AUTHOR/EDITOR, TITLE, CHAPTER/PAGES,
PUBLISHER, YEAR
Optional fields: VOLUME/NUMBER, SERIES, TYPE, ADDRESS, EDITION,
MONTH, NOTE

incollection: A part of a book having its own title.


Required fields: AUTHOR, TITLE, BOOKTITLE, PUBLISHER, YEAR
Optional fields: EDITOR, VOLUME/NUMBER, SERIES, TYPE, CHAPTER,
PAGES, ADDRESS, EDITION, MONTH, NOTE

inproceedings: An article in a conference proceedings.


Required fields: AUTHOR, TITLE, BOOKTITLE, YEAR
Optional fields: EDITOR, VOLUME/NUMBER, SERIES, PAGES, ADDRESS,
MONTH, ORGANIZATION, PUBLISHER, NOTE
Umair Rafique

139

Reference Entry Types cont..


manual: Technical documentation.
Required fields: TITLE
Optional fields: AUTHOR, ORGANIZATION, ADDRESS, EDITION,
MONTH, YEAR, NOTE

mastersthesis: A Masters Thesis


Required fields: AUTHOR, TITLE, SCHOOL, YEAR
Optional fields: TYPE, ADDRESS, MONTH, NOTE

misc: For use when nothing else fits.


Required fields: NONE
Optional fields: AUTHOR, TITLE, HOWPUBLISHED, MONTH, YEAR, NOTE

phdthesis: A PhD Thesis.


Required fields: AUTHOR, TITLE, SCHOOL, YEAR
Optional fields: TYPE, ADDRESS, MONTH, NOTE
Umair Rafique

140

Reference Entry Types cont..

proceedings: The proceedings of a conference.


Required fields: TITLE, YEAR
Optional fields: EDITOR, VOLUME/NUMBER, SERIES, ADDRESS,
MONTH, PUBLISHER, ORGANIZATION, NOTE

techreport: A report published by a school or other institution,


usually numbered within a series.
Required fields: AUTHOR, TITLE, INSTITUTION, YEAR
Optional fields: TYPE, NUMBER, ADDRESS, MONTH, NOTE

unpublished: A document having an author and title, but not


formally published.
Required fields: AUTHOR, TITLE, NOTE
Optional fields: MONTH, YEAR
Umair Rafique

141

Example .bib File


BIB_all.bib

Umair Rafique

142

How to Use .bib File from a


LaTex Document?

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

.bst File: Reference Styles

Umair Rafique

144

.bst File

Specifies how references for a given document are styled.


A large number of reference styles are available.
See bibstyles.pdf for the details of following styles.

Umair Rafique

145

Reference Styles: plain

Umair Rafique

146

Reference Styles: abbrv

Umair Rafique

147

Reference Styles: alpha

Umair Rafique

148

.bib style examples

Umair Rafique

149

Theorems Using amsthm


Package

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

Often the counters are determined by section, for example


"Theorem 2.3" refers to the 3rd theorem in the 2nd section of a
document. Can do so using the command.
\newtheorem{name}{Printed output}[numberby]

numberby is the name of the section level (section, subsection).


By default, each theorem uses its own counter.
It is also common for similar types of theorems (e.g. Theorems,
Lemmas and Corollaries) to share a counter. This can be done as
\newtheorem{name}[counter]{Printed output}

counter is the name of the counter to be used. Usually this will be


the name of the master theorem.

Umair Rafique

153

Theorem Numbering

You can also create a theorem environment that is not numbered by


using the newtheorem* command. For instance,
\newtheorem*{mydef}{Definition}

defines the mydef environment, which will generate definitions


without numbering.
Examples

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

It adds the possibility to change the output of the environments


defined by \newtheorem using the \theoremstyle command
\theoremstyle{stylename}
the argument is the style you want to use.
All subsequently defined theorems will use this style.
plain: Used for theorems, lemmas, propositions, etc. (default)
definition: Used for definitions and examples
remark: Used for remarks and notes

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

Algorithms using algorithm2e


package

Umair Rafique

160

Umair Rafique

161

Umair Rafique

162

Some Basics
\usepackage[options ]{algorithm2e}
\begin{algorithm}
\end{algorithm}

Each line of the algorithm should end with \;


\caption{caption of the algorithm} (just like in case of figures)
Algorithms in a document can be displayed as a list using
\listofalgorithms.
\label{algo_label}. can be referenced as \ref{algo_label}
For changing font size use
\begin{algorithm}
{ \scriptsize
}
\end{algorithm}
Umair Rafique

163

Environments Defined in the


Package
algorithm : the main environment
algorithm* : puts the algorithm across the two columns.
procedure : like algorithm environment but:
the caption now writes Procedure name...
the syntax of the \caption command is restricted as follow: you MUST put
a name followed by 2 braces like this Name(). You can put arguments
inside the braces and text after. If no argument is given, the braces will
be removed in the title.
label now puts the name (the text before the braces in the caption) of the
procedure or function as reference

procedure* : puts the procedure across the two columns


function : like procedure but with Function in the title.
function* : puts the function across the two columns.
Umair Rafique

164

Options of the Package

algo2e: changes the name of environment algorithm into


algorithm2e and so allows to use the package with some journal
style which already define an algorithm environment. Changes also
the command name for the list of algorithms, it becomes
\listofalgorithmes
figure: algorithms are put in classical figures and so are numbered
as figures and putted in the \listoffigures.
algopart: algorithms are numbered within part numbers.
algochapter: algorithms are numbered within chapter numbers.
algosection: (default) algorithms are numbered within section
numbers.
boxed: to have algorithms enclosed in a box.
Umair Rafique

165

Options Cont..

boxruled: to have algorithms enclosed in a box


ruled: to have algorithms with a line at the top and the bottom. Note
that the caption is not centered under the algorithm anymore but is
set at the beginning of the algorithm.
algoruled: as above but with extra spaces after the rules.
plain: the default, with no feature.
lined: \SetLine becomes the default
vlined: \SetVline becomes the default
noline: \SetNoline becomes the default
linesnumbered: lines of the algorithms are numbered except for
comments and input/output (KwInput and KwInOut). You must
use \nllabel{label} to label thoses lines.
Umair Rafique

166

SetVLine

Umair Rafique

167

Data, Result, Input and Output

\KwData{input}
\KwResult{output}
\KwIn{input}
\KwOut{output}

Umair Rafique

168

Comments

\tcc{line(s) of comment} (c style)


\tcc*{right justified side comment}
\tcc*[r]{right justified side comment}
\tcc*[l]{left justified side comment}
\tcp*{right justified side comment} (c++ style)

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}

\Repeat{end condition}{text loop}


\lRepeat{end condition}{line text loop}
Umair Rafique

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

$...$

displayed - displayed formulas are separate from the main text.


\begin{displaymath}...\end{displaymath}
\begin{equation*}...\end{equation*}
\[\]

Umair Rafique

179

Spaces

spaces have no significance inside math mode


\,
\;
\:
\quad

Umair Rafique

180

Symbols and Greek Letters

\[
\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

Powers and Indices

\[
k_{n+1} = n^2 + k_n^2 - k_{n-1}
\]

\[
f(n) = n^5 + 4n^2 + 2 _{n=17}
\]

Umair Rafique

183

Fractions and Binomials


\[
\frac{n!}{k!(n-k)!} = \binom{n}{k}
\]
\[
\frac{\frac{1}{x}+\frac{1}{y}}{y-z}
\]

\dfrac, \tfrac (for enforcing display or text style))


\[
^3/_7
\]
Umair Rafique

184

\sfrac using xfrac package

Take \sfrac{1}{2} cup of sugar, \dots


\[
3\times\sfrac{1}{2}=1\sfrac{1}{2}
\]
Take ${}^1/_2$ cup of sugar, \dots
\[
3\times{}^1/_2=1{}^1/_2
\]

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

Sums and Integrals

\[
\sum_{i=1}^{10} t_i
\]

\[
\int_0^\infty e^{-x}\,\mathrm{d}x
\]

Umair Rafique

188

Umair Rafique

189

Brackets, Braces, Delimiter

\[
( 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

Matrices and Arrays

\[
\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

A matrix in text must be set smaller:


$\bigl(\begin{smallmatrix}
a&b\\ c&d
\end{smallmatrix} \bigr)$
to not increase leading in a portion of text.

Umair Rafique

194

Adding Text to Equations


\[
50 apples \times 100 apples = lots of apples^2
\]

\[
50 \text{apples} \times 100 \text{apples}
= \text{lots of apples}^2
\]

Can also use, \textbf and \textit{}


Umair Rafique

195

Formatting Mathematical
Symbols (amsfonts)

Umair Rafique

196

Accents

Umair Rafique

197

Adding Space in Math Mode

\,
\;
\:
\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

Numbering Within Sections

\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

align and align*


The align and align* environments are used for arranging equations of
multiple lines. As with matrices and tables, \\ specifies a line break, and
& is used to indicate the point at which the lines should be aligned.
\begin{align*}
f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
\end{align*}

\begin{align}
f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\
&\qquad {} + 10x + 12
\end{align}
Umair Rafique

212

Braces Spanning multiple lines

\begin{align}
f(x) &= \pi \left\{ x^4 + 7x^3 + 2x^2 \right.\nonumber\\
&\qquad \left. {} + 10x + 12 \right\}
\end{align}

Umair Rafique

213

cases and dcases

\[
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

dcases* for text

\[
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

Vous aimerez peut-être aussi