Lintro
Lintro
Lintro
LT
A
EX
Version 1.02 July 2015
1
http://en.wikibooks.org/wiki/LaTeX
2
http://ctan.tug.org/tex-archive/info/lshort/english/lshort.pdf
iv Thank you!
Preface
This short introduction describes LATEX and should be sufficient for most
applications of LATEX. Refer to [1], [3], [6] for a complete description of the
LATEX system.
Chapter 2 presents our first document in LATEX and its compilation pro-
cess. It also discusses about the syntax of LATEX.
It is important to read the chapters in order—the book is not that big, after
all. Be sure to carefully read the examples, because a lot of the information
is in the examples placed throughout the book.
If you need to get hold of any LATEX related material, have a look at one of
the Comprehensive TEX Archive Network (CTAN) sites. The homepage is
at http://www.ctan.org.
Professor
Computer Science and Engineering
Shahjalal University of Science and Technology
Table of Contents
Preface v
1 Introduction 1
1.1 What is LATEX? . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why LATEX? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Installing LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 What to Install . . . . . . . . . . . . . . . . . . . . . 2
1.3.2 Cross Platform Editor . . . . . . . . . . . . . . . . . . 3
1.3.3 TEX on Windows . . . . . . . . . . . . . . . . . . . . . 3
1.3.4 TEX on Linux . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.5 Online Solutions . . . . . . . . . . . . . . . . . . . . . 5
4 Typesetting Text 31
4.1 Page styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.1.1 Standard page styles . . . . . . . . . . . . . . . . . . . 31
4.2 Text Formatting . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.2.1 Spacing . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4.2.2 Fonts and Sizes . . . . . . . . . . . . . . . . . . . . . . 33
4.2.3 Text mode superscript and subscript . . . . . . . . . . 36
4.2.4 Dashes and hyphens . . . . . . . . . . . . . . . . . . . 36
4.2.5 Ellipsis (. . . ) . . . . . . . . . . . . . . . . . . . . . . . 37
4.3 Paragraph Formatting . . . . . . . . . . . . . . . . . . . . . . 37
4.3.1 Paragraph alignment . . . . . . . . . . . . . . . . . . . 37
4.3.2 Paragraph indent and break . . . . . . . . . . . . . . . 38
4.4 Printing Verbatim . . . . . . . . . . . . . . . . . . . . . . . . 39
4.5 List structures . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.5.1 Nested lists . . . . . . . . . . . . . . . . . . . . . . . . 41
4.6 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.6.1 The tabular environment . . . . . . . . . . . . . . . . 42
4.6.2 Spanning . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.7 Importing Graphics . . . . . . . . . . . . . . . . . . . . . . . . 50
4.7.1 The graphicx package . . . . . . . . . . . . . . . . . . 50
TABLE OF CONTENTS ix
5 Typesetting Mathematics 57
5.1 The amsmath package . . . . . . . . . . . . . . . . . . . . . . 57
5.2 Mathematics environments . . . . . . . . . . . . . . . . . . . 57
5.3 Building Blocks of a Mathematical Formula . . . . . . . . . . 58
5.3.1 Greek letters . . . . . . . . . . . . . . . . . . . . . . . 59
5.3.2 Powers and indices . . . . . . . . . . . . . . . . . . . . 59
5.3.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.3.4 Fractions and Binomials . . . . . . . . . . . . . . . . . 60
5.3.5 Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.3.6 Sums and integrals . . . . . . . . . . . . . . . . . . . . 61
5.3.7 Brackets, braces and delimiters . . . . . . . . . . . . . 62
5.3.8 Matrices and arrays . . . . . . . . . . . . . . . . . . . 64
5.3.9 Adding text to equations . . . . . . . . . . . . . . . . 66
5.3.10 Controlling horizontal spacing . . . . . . . . . . . . . . 66
5.4 Single Equation . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.5 Multiple Equations . . . . . . . . . . . . . . . . . . . . . . . . 70
5.6 Equation numbering . . . . . . . . . . . . . . . . . . . . . . . 71
5.7 List of Mathematical Symbols . . . . . . . . . . . . . . . . . . 72
A Further Reading 73
Bibliography 75
Index 77
x TABLE OF CONTENTS
List of Tables
Introduction
• Free add-on packages exist for many typographical tasks not directly
supported by basic LATEX. For example, packages are available to in-
clude PostScript graphics or to typeset bibliographies conforming to
exact standards. Many of these add-on packages are described in The
LATEX Companion[3].
1. The TEX/LATEX program for processing your LATEX source files into
typeset PDF or DVI documents.
2. A text editor for editing your LATEX source files. Some products even
let you start the LATEX program from within the editor.
4. A program to handle PostScript files and images for inclusion into your
documents.
For every platforms there are several programs that fit the requirements
above. Here we just tell about the ones we know, like and have some expe-
rience with.
1.3 Installing LATEX 3
A LATEX editor
If you are not happy with our crossplatform suggestion Texmaker (section
1.3.2).
TeXnicCenter uses many concepts from the programming-world to pro-
vide a nice and efficient LATEX writing environment in Windows. Get your
copy from http://www.texniccenter.org/. TeXnicCenter integrates nicely
with MiKTEX.
Recent MiKTEX distributions contain the TEXworks editor http://
texworks.org/. It supports Unicode and requires at least Windows XP.
Document Preview
For PDF you may want to look at any PDF viewer like Foxit reader, Adobe
Acrobat reader or Sumatra PDF 2 . I mention Sumatra PDF because it lets
2
http://blog.kowalczyk.info/software/sumatrapdf/
4 Introduction
you jump from any position in the pdf document back into corresponding
position in your source document.
If you are looking for a more windows like graphical editing environment,
check out TeXmaker 3 . Apart from being very a useful editor with integrated
pdf-preview and syntax highlighting, it has the advantage of running on
Windows, Mac and Unix/Linux equally well.
Most Linux distros insist on splitting up their TEX environments into a
large number of optional packages, so if something is missing after your first
install, go check again.
3
http://www.xm1math.net/texmaker
1.3 Installing LATEX 5
4
http://docs.latexlab.org
5
https://www.overleaf.com
6 Introduction
Chapter 2
When picking a name for your file, make sure it bears a .tex extension.
2.2 Compilation
2.2.1 Compilation process
The general concept is to transform a plain text document into a publishable
format, mostly a DVI, PS or PDF file. This process is called compilation,
which is done by an executable file called a compiler. There are two exe-
cutables:
8 Our First Document
Hello World!
along with the page number at the bottom, which is added automatically in
the article class.
2.2.3 Troubleshooting
If there is an error in your document and compiler cannot create the PDF,
Build Output at the bottom of the screen will stay open. If this happens:
• Read the Build Output - the last line will probably include a line
number and the command that caused the error.
2.3.1 Spaces
Whitespace characters, such as blank or tab, are treated uniformly as ”space”
by LATEX. Several consecutive ”spaces” are treated as one, ”space” opening
a line is generally ignored, and a single line break also yields ”space”.
A double line break (an empty line), however, defines the end of a para-
graph; multiple empty lines are treated the same as one empty line. The
text below is an example. On the above is the text from the input file, and
in the box below is the formatted output.
It does not matter whether you enter one or several spaces after a word.
An empty line starts a new paragraph.
#$%ˆ& {}˜\
As you will see, these characters can be used in your documents all the
same by adding a prefix backslash:
#$%ˆ& {}˜\
{
\bf This is bold.
}
This is no longer bold.
\commandname[option1,option2,...]{argument1}{argument2}...
\begin{environmentname}
text to be influenced
\end{environmentname}
Between the \begin and the \end you can put other commands and
nested environments. The internal mechanism of environments defines a
group, which makes its usage safe (no influence on the other parts of the
document). In general, environments can accept arguments as well, but this
feature is not commonly used and so it will be discussed in more advanced
parts of the document.
Anything in LaTeX can be expressed in terms of commands and envi-
ronments.
2.3.6 Comments
When LATEX encounters a % character while processing an input file, it
ignores the rest of the current line, the line break, and all whitespace at the
beginning of the next line. This can be used to write notes into the input
file, which will not show up in the printed version.
This is an % stupid
% Better: instructive <----
example: Supercal%
ifragilist%
icexpialidocious
12 Our First Document
Note that the % character can be used to split long input lines that do
not allow whitespace or line breaks, as with Supercalifragilisticexpialidocious
above.
Chapter 3
\documentclass{...}
\begin{document}
...
\end{document}
\begin{document}
...
\end{document}
When processing an input file, LATEX needs to know the type of document
the author wants to create. This is specified with the \documentclass com-
mand. It is recommended to put this declaration at the very beginning.
\documentclass[options]{class}
Here, class specifies the type of document to be created. Table 3.1 lists
the document classes explained in this introduction. The options parameter
customizes the behavior of the document class. The options have to be
separated by commas. The most common options for the standard document
classes are listed in Table 3.2.
Example: an input file for a LATEX document could start with the line
\documentclass[11pt,twoside,a4paper]{article}
which instructs LATEX to typeset the document as an article with a base font
size of 11 points, and to produce a layout suitable for double sided printing
on A4 paper.
3.2.2 Packages
While writing your document, you will probably find that there are some
areas where basic LATEX cannot solve your problem. If you want to include
graphics, colored text or source code from a file into your document, you
need to enhance the capabilities of LATEX. Such enhancements are called
packages. Some packages come with the LATEX base distribution. Others
are provided separately. Modern TEX distributions come with a large num-
ber of packages pre-installed. Packages are activated with the
\usepackage[options]{package}
command, where package is the name of the package and options is a list of
keywords that trigger special features in the package. For example, to use
the color package, which lets you typeset in colors, you would type:
\documentclass[11pt,a4paper,oneside]{report}
\usepackage{color}
\begin{document}
...
\end{document}
\usepackage{package1,package2,package3}
and you can have more than one \usepackage command. Some packages
allow optional settings in square brackets. If you use these, you must give
the package its own separate \usepackage command.
Many packages can have additional formatting specifications in optional
arguments in square brackets. Read the documentation for the package con-
cerned to find out what can be done. You can pass several options together
separated by a comma:
\usepackage[option1,option2,option3]{’’package_name’’}
16 Basic Document Structure
\documentclass[11pt,a4paper]{report}
\begin{document}
\title{How to Structure a LaTeX Document}
\author{John Doe\\ Magic Department, Richard Miles University
\and Richard Row, \LaTeX\ Academy}
\date{December 2004}
\maketitle
\end{document}
Commonly the date is excluded from the title page by using \date{}.
It defaults to \today if not in the source file. The double backslash (\\) is
the LATEX command for forced linebreak.
To form a title page, use
\maketitle
3.3.2 Abstract
As most research papers have an abstract, there are predefined commands
for telling LATEX which part of the content makes up the abstract. This
should appear in its logical order, therefore, after the title creation, but
before the main sections of the body. This command is available for the
document classes article and report, but not book.
\documentclass{article}
\begin{document}
\begin{abstract}
3.3 The document environment 17
By default, LATEX will use the word ”Abstract” as a title for your ab-
stract.
3.3.3 Sections
The commands for inserting sections are fairly intuitive. Of course, certain
commands are appropriate to different document classes. For example, a
book has chapters but an article doesn’t. Here are some of the structure
commands:
\chapter{Introduction}
This chapter’s content...
\section{Structure}
This section’s content...
\subsection{Top Matter}
This subsection’s content...
\subsubsection{Article Information}
This subsubsection’s content...
Notice that you do not need to specify section numbers; LATEX will sort
that out for you. The spacing between sections, the numbering and the font
size of the titles will be set automatically by LATEX.
LATEX provides 7 levels of depth for defining sections (see table 3.3).
Each section in this table is a subsection of the one above it.
All the titles of the sections are added automatically to the Table of Con-
tents (if you decide to insert one). To get an unnumbered section heading
which does not go into the Table of Contents, follow the command name
with an asterisk before the opening curly brace:
\subsection*{Introduction}
18 Basic Document Structure
\tableofcontents
at the point where you want it printed (usually after the Abstract or Sum-
mary).
Entries for the ToC are recorded each time you process your document,
and reproduced the next time you process it, so you need to re-run LATEX
one extra time to ensure that all ToC page number references are correctly
calculated.
The commands \listoffigures and \listoftables work in exactly
the same way as \tableofcontents to automatically list all your tables
and figures. If you use them, they normally go after the \tableofcontents
command.
\begin{document}
3.4 Book structure 19
\frontmatter
\maketitle
% Introductory chapters
\chapter{Preface}
% ...
\mainmatter
\chapter{First chapter}
% ...
\appendix
\chapter{First Appendix}
\backmatter
\chapter{Last note}
\end{document}
• The frontmatter chapters will not be numbered. Page numbers will
be printed in roman numerals. Frontmatter is not supposed to have
sections, since they will be number 0.n because there is no chapter
numbering.
• The mainmatter chapters works as usual. The command resets the
page numbering. Page numbers will be printed in arabic numerals.
• The \appendix macro can be used to indicate that following sections
or chapters are to be numbered as appendices. Appendices can be
used for the article class too:
\appendix
\section{First Appendix}
Frontmatter
1. Half-title
2. Empty
3. Title page
6. Table of contents
8. Preface chapter
Mainmatter
1. Main topic
Appendix
1. Some subordinate chapters
Backmatter
1. Bibliography
2. Glossary / Index
3.5.1 Bibliography
If you are writing only one or two documents and aren’t planning on writing
more on the same subject for a long time, you might not 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; that usually means at the very end
of your document, just before the \end{document} command. Each entry
starts with
\bibitem[label]{marker}
The marker is then used to cite the book, article or paper within the
document.
\cite{marker}
If you do not use the label option, the entries will get enumerated auto-
matically. Here is a practical example:
...
\begin{thebibliography}{56}
\bibitem{lamport94}
Leslie Lamport,
LaTeX: a document preparation system,
Addison Wesley, Massachusetts,
2nd edition,
1994.
\end{thebibliography}
...
Bibliography
[1] Leslie Lamport. LaTeX: A Document Preparation System. Ad-
dison Wesley, Massachusetts, 2nd edition, 1994.
3.5.2 Indexing
To enable the indexing feature of LATEX, the makeidx package must be loaded
in the preamble with:
\usepackage{makeidx}
and the special indexing commands must be enabled by putting the
\makeindex
command into the input file preamble.
The content of the index is specified with
\index{key@formatted_entry}
commands, where formatted entry will appear in the index and key will be
used for sorting. The formatted entry is optional. If it is missing the key
will be used. You enter the index commands at the points in the text that
you want the final index entries to point to. For example, the text
\index{Fourier Series}
composed of multiples of sine and cosine functions.
to create an entry called ’Fourier Series’ with a reference to the target page.
Multiple uses of \index with the same key on different pages will add those
target pages to the same index entry. Table 3.4 explains the syntax with
several examples.
To show the index within the document, merely use the command
\printindex
Compiling indices
When the input file is processed with LATEX, each \index command writes
an appropriate index entry, together with the current page number, to a spe-
cial file. The file has the same name as the LATEX input file, but a different
extension (.idx). This .idx file can then be processed with the makeindex
program:
makeindex filename
The makeindex program generates a sorted index with the same base
file name, but this time with the extension .ind. If now the LATEX input
file is processed again, this sorted index gets included into the document at
the point where LATEX finds \printindex.
1. create a directory only for the project. We’ll refer to that in the
following parts as the root directory
2. create two other directories inside the root, one for LATEX documents,
the other one for images. Since you’ll have to write their name quite
often, choose short names. A suggestion would be simply tex and img.
3. create your document (we’ll call it document.tex, but you can use the
name you prefer) and your own package (for example mystyle.sty);
this second file will help you to keep the code cleaner.
If you followed all those steps, these files should be in your root directory,
using "/" for each directory:
./document.tex
./mystyle.sty
./tex/
./img/
nothing else.
\input{filename}
and
\include{filename}
\documentclass{article}
\begin{document}
%\input{Section_1}
%\input{Section_2}
%\input{Section_3}
\input{Section_4}
%\input{Section_5}
\end{document}
This code will process your base file with the article conventions but only
the material in the file Section 4.tex will be processed. If that was, say,
the last thing you needed to check before sending off to that major journal,
you would then simply remove all the percentage signs and rerun LATEX,
repeating the compiling process as necessary to resolve all references, page
numbers and so on.
Standalone
The standalone package provides a means for importing the preamble of
child documents into the main document, allowing for a flexible way to
include text or images in multiple documents (e.g. an article and a
presentation).
In the main document, the package must be loaded as:
\usepackage{standalone}
26 Basic Document Structure
\documentclass{standalone}
% Load any packages needed for this document
\begin{document}
% Your document or picture
\end{document}
\usepackage{standalone}
in your document, keeping your code much cleaner. Moreover, all the info
about your style will be within one file, so when you will start another
document you’ll just have to copy that file and include it properly, so you’ll
have exactly the same style you have used.
Creating your own style is very simple: create a file called mystyle.sty
(you could name it as you wish, but it has to end with ”.sty”). Write at the
beginning:
\ProvidesPackage{mystyle}
Then add all the packages you want with the standard command \usepackage{...}
as you would do normally, change the value of all the variables you want,
etc. It will work like the code you put here would be copied and pasted
within your document.
\documentclass[12pt,a4paper]{report}
\usepackage{mystyle}
3.6 Big Projects 27
\begin{document}
\input{./tex/title.tex}
%\maketitle
\tableofcontents
\listoffigures
\listoftables
\input{./tex/intro.tex}
\input{./tex/main_part.tex}
\input{./tex/conclusions.tex}
\appendix
\input{./tex/myappendix.tex}
% Bibliography:
\input{./tex/mybibliography.tex}
\end{document}
28 Basic Document Structure
Typesetting Text
\pagestyle{‘‘style’’}
will apply the specified style to the current and all subsequent pages, and
\thispagestyle{‘‘style’’}
will only affect the current page. The possible styles are shown in Table 4.1.
\linespread{factor}
command into the preamble of your document. Use \linespread{1.3}
for ”one and a half” line spacing, and \linespread{1.6} for ”double” line
spacing. Normally the lines are not spread, so the default line spread factor
is 1.
The setspace package allows more fine-grained control over line spacing.
To set ”one and a half” line spacing document-wide, but not where it is
usually unnecessary (e.g. footnotes, captions):
\usepackage{setspace}
%\singlespacing
\onehalfspacing
%\doublespacing
%\setstretch{1.1}
To change line spacing within the document, the setspace package
provides the environments singlespace, onehalfspace, doublespace and
spacing:
This paragraph has \\ default \\ line spacing.
\begin{doublespace}
This paragraph has \\ double \\ line spacing.
\end{doublespace}
\begin{spacing}{2.5}
This paragraph has \\ huge gaps \\ between lines.
\end{spacing}
4.2 Text Formatting 33
Horizontal Space
LaTeX determines the spaces between words and sentences automatically.
To add horizontal space, use:
\hspace{length}
If such a space should be kept even if it falls at the end or the start of a
line, use \hspace* instead of \hspace.
This\hspace{1.5cm}is a space
of 1.5 cm.
Vertical Space
The space between paragraphs, sections, subsections, . . . is determined au-
tomatically by LATEX. If necessary, additional vertical space between two
paragraphs can be added with the command:
\vspace{length}
This command should normally be used between two empty lines. If the
space should be preserved at the top or at the bottom of a page, use the
starred version of the command, \vspace*, instead of \vspace.
Additional space between two lines of the same paragraph or within a
table is specified with the
\\[length]
command.
With \bigskip and \smallskip you can skip a predefined amount of
vertical space without having to worry about exact numbers.
Tables 4.2 and 4.3. The actual size of each font is a design issue and depends
on the document class and its options. Table 4.4 shows the absolute point
size for these commands as implemented in the standard document classes.
The small and bold Romans ruled all of great big Italy.
In math mode use the font changing commands to temporarily exit math
mode and enter some normal text. If you want to switch to another font for
math typesetting you need another special set of commands; refer to Table
4.5.
In connection with the font size commands, curly braces play a significant
role. They are used to build groups. Groups limit the scope of most LATEX
commands.
\begin{Large}
This is not true.
But then again, what is these
days \ldots
\end{Large}
4.2.5 Ellipsis (. . . )
A sequence of three dots is known as an ellipsis, which is commonly used
to indicate omitted text. There is a special command for these dots. It is
called \ldots:
All text between the \begin and \end of the specified environment will
be justified appropriately. The commands listed are for use within other
environments. For example, p (paragraph) columns in tabular.
There is no way (in standard LATEX) to set full justification explicitly. It
means that if you do not enclose the previous 3 commands into a group, the
rest of the document will be affected.
38 Typesetting Text
Whitespace in LATEX can also be made flexible (what Lamport calls ”rub-
ber” lengths). This means that values such as extra vertical space inserted
before a paragraph \parskip can have a default dimension plus an amount
of expansion minus an amount of contraction. This is useful on pages in
complex documents where not every page may be an exact number of fixed-
height lines long, so some give-and-take in vertical space is useful. You
specify this in a \setlength command like this:
If you want to indent a paragraph that is not indented, you can use
\indent
at the beginning of the paragraph. Obviously, this will only have an effect
when \parindent is not set to zero. If you want to indent the beginning
of every section, you can use the indentfirst package: once loaded, the
beginning of any chapter/section is indented by the usual paragraph inden-
tation.
To create a non-indented paragraph, you can use
\noindent
4.4 Printing Verbatim 39
as the first command of the paragraph. This might come in handy when
you start a document with body text and not with a sectioning command.
\verb+text+
\begin{verbatim}
10 PRINT "HELLO WORLD ";
20 GOTO 10
\end{verbatim}
The verbatim environment and the \verb command may not be used
within parameters of other commands.
\begin{list_type}
\end{list_type}
All three of these types of lists can have multiple paragraphs per item:
just type the additional paragraphs in the normal way, with a blank line
between each. So long as they are still contained within the enclosing en-
vironment, they will automatically be indented to follow underneath their
item.
Itemize
This environment is for your standard bulleted list of items.
\begin{itemize}
\item The first item
\item The second item
\item The third etc \ldots
\end{itemize}
Enumerate
The enumerate environment is for ordered lists, where by default, each item
is numbered sequentially.
\begin{enumerate}
\item The first item
\item The second item
\item The third etc \ldots
\end{enumerate}
4.5 List structures 41
Description
The description environment is slightly different. You can specify the item
label by passing it as an optional argument (although optional, it would
look odd if you didn’t include it!). Ideal for a series of definitions, such as a
glossary.
\begin{description}
\item[First] The first item
\item[Second] The second item
\item[Third] The third etc \ldots
\end{description}
\begin{enumerate}
\item The first item
\begin{enumerate}
\item Nested item 1
42 Typesetting Text
4.6 Tables
Tables are a common feature in academic writing, often used to summarize
research results. Mastering the art of table construction in LATEX is therefore
necessary to produce quality papers and with sufficient practice one can print
beautiful tables of any kind.
Once you are comfortable with basic LATEX tables, you might have a look
at more advanced packages or the export options of your favorite spread-
sheet.
\begin{tabular}[pos]{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
4.6 Tables 43
vertical lines between the columns here. The following symbols are available
to describe the table columns (some of them require that the package array
has been loaded):
l left-justified column
c centered column
r right-justified column
p{’width’} paragraph column with text vertically aligned at the
top
m{’width’} paragraph column with text vertically aligned in the
middle (requires array package)
b{’width’} paragraph column with text vertically aligned at the
bottom (requires array package)
| vertical line
|| double vertical line
By default, if the text in a column is too wide for the page, LATEX won‘t
automatically wrap it. Using p{’width’} you can define a special type of
column which will wrap-around the text as in a normal paragraph. You can
pass the width using any unit supported by LATEX, such as ‘pt’ and ‘cm’, or
command lengths, such as \textwidth.
The optional parameter pos can be used to specify the vertical position
of the table relative to the baseline of the surrounding text. You can use
the following letters:
b bottom
c center (default)
t top
In the first line you have pointed out how many columns you want,
their alignment and the vertical lines to separate them. Once in the envi-
ronment, you have to introduce the text you want, separating between cells
and introducing new lines. The commands you have to use are the following:
44 Typesetting Text
Note, any white space inserted between these commands is purely down
to ones’ preferences. I personally add spaces between to make it easier to
read.
Basic examples
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
1 2 3
4 5 6
7 8 9
\begin{tabular}{ l | c || r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
4.6 Tables 45
1 2 3
4 5 6
7 8 9
To add horizontal lines to the very top and bottom edges of the table:
\begin{tabular}{ l | c || r }
\hline
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\hline
\end{tabular}
1 2 3
4 5 6
7 8 9
\begin{center}
\begin{tabular}{ l | c || r }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{center}
1 2 3
4 5 6
7 8 9
46 Typesetting Text
\begin{center}
\begin{tabular}{ | l || c ||| r }
\hline
1 & 2 & 3 \\ \hline
4 & 5 & 6 \\ \hline \hline
7 & 8 & 9 \\
\hline
\end{tabular}
\end{center}
1 2 3
4 5 6
7 8 9
\begin{tabular}{|r|l|}
\hline
7C0 & hexadecimal \\
3700 & octal \\ \cline{2-2}
11111000000 & binary \\
\hline \hline
1984 & decimal \\
\hline
\end{tabular}
7C0 hexadecimal
3700 octal
11111000000 binary
1984 decimal
4.6.2 Spanning
Rows spanning multiple columns
The command for this looks like this:
4.6 Tables 47
\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 p{5.0cm} . And
contents is simply the actual data you want to be contained within that
cell. A simple example:
\begin{tabular}{ |l|l| }
\hline
\multicolumn{2}{|c|}{Team sheet} \\
\hline
GK & Paul Robinson \\
LB & Lucus Radebe \\
DC & Michael Duberry \\
DC & Dominic Matteo \\
\hline
\end{tabular}
Team sheet
GK Paul Robinson
LB Lucus Radebe
DC Michael Duberry
DC Dominic Matteo
\usepackage{multirow}
to the preamble. This then provides the command needed for spanning rows:
\multirow{’’num_rows’’}{’’width’’}{’’contents’’}.
The arguments are pretty simple to deduce (* for the width means the
content’s natural width).
...
\usepackage{multirow}
48 Typesetting Text
...
\begin{tabular}{ |l|l|l| }
\hline
\multicolumn{3}{ |c| }{Team sheet} \\
\hline
Goalkeeper & GK & Paul Robinson \\ \hline
\multirow{4}{*}{Defenders} & LB & Lucus Radebe \\
& DC & Michael Duburry \\
& DC & Dominic Matteo \\
& RB & Didier Domi \\ \hline
\multirow{3}{*}{Midfielders} & MC & David Batty \\
& MC & Eirik Bakke \\
& MC & Jody Morris \\ \hline
Forward & FW & Jamie McMaster \\ \hline
\multirow{2}{*}{Strikers} & ST & Alan Smith \\
& ST & Mark Viduka \\
\hline
\end{tabular}
Team sheet
Goalkeeper GK Paul Robinson
LB Lucus Radebe
DC Michael Duburry
Defenders
DC Dominic Matteo
RB Didier Domi
MC David Batty
Midfielders MC Eirik Bakke
MC Jody Morris
Forward FW Jamie McMaster
ST Alan Smith
Strikers
ST Mark Viduka
The main thing to note when using \multirow is that a blank entry must
be inserted for each appropriate cell in each subsequent row to be spanned.
4.6 Tables 49
\usepackage{multirow}
\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 & \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 & \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}
Primes
2 3 5 7
504 3 2 0 1
Powers
540 2 3 1 0
gcd 2 2 0 0 min
Powers
lcm 3 3 1 1 max
\begin{tabular}{ r|c|c| }
50 Typesetting Text
\multicolumn{1}{r}{}
& \multicolumn{1}{c}{noninteractive}
& \multicolumn{1}{c}{interactive} \\
\cline{2-3}
massively multiple & Library & University \\
\cline{2-3}
one-to-one & Book & Tutor \\
\cline{2-3}
\end{tabular}
noninteractive interactive
massively multiple Library University
one-to-one Book Tutor
\usepackage{graphicx}
The argument in the curly braces is the name of the image. Write
it without the extension. This way the LATEX compiler will look for any
supported image format in that directory and will take the best one (EPS
if the output is DVI; JPEG, PNG or PDF if the output is PDF).
The optional parameter accepts a comma separated list of keys and
associated values. The keys can be used to alter the width, height and
rotation of the included graphic. Table 4.6 lists the most important keys.
The following example code may help to clarify things:
\includegraphics[scale=0.1]{./img/tulips}
4.8 Floats
Floats are containers for things in a document that cannot be broken over a
page. LATEX by default recognizes ”table” and ”figure” floats, but you can
52 Typesetting Text
define new ones of your own. Floats are there to deal with the problem of
the object that won’t fit on the present page, and to help when you really
don’t want the object here just now.
\begin{figure}[placement specifier]
\centering
... figure contents ...
\caption{this is a figure}
\label{fig:myfirstfigure}
\end{figure}
Floats are used to allow LATEX to handle figures and add a label and
caption to the figure. The placement specifier parameter (Table 4.8) gives
the author a greater degree of control over where certain floats are placed.
Here is an example that shows the figure in section 4.7.3 becomes float:
\begin{figure}
\centering
\fbox{\includegraphics[scale=0.1]{./img/tulips}}
\caption{Tulips}
\label{fig:tulips}
\end{figure}
Please understand: you do not have to use floating tables. If you want
to place your tables where they lie in your source code and you do not need
any label, do not use table at all! This is a very common misunderstanding
among newcomers.
The environment names may now seem quite confusing. Let’s sum it up:
• table is for the location of the table on the document, plus caption
and label support.
\begin{table}[placement specifier]
\centering
\begin{tabular}{|l|}
... your table ...
\end{tabular}
\caption{This table shows some data}
\label{tab:myfirsttable}
\end{table}
In the table, we used a label, so now we can refer to it just like any other
reference:
\ref{tab:myfirsttable}
Centering the table horizontally works like everything else, using the
\centering command just after opening the table environment, or by en-
closing it with a center environment. Here is an example that shows a table
in section 4.6.1 becomes float:
\begin{table}
\caption{A simple table}
\centering
\begin{tabular}{ l c r }
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{tabular}
\label{tab:simple_table}
\end{table}
54 Typesetting Text
You can set the optional parameter placement specifier to define the
position of the floats (table, figure), where it should be placed. The
characters shown in table 4.8 are all possible placements. Using sequences
of it define your ”wishlist” to LATEX.
Default is tbp, which means that it is by default placed on the top of the
page. If that’s not possible, it’s placed at the bottom if possible, or finally
with other floating environments on an extra page.
4.8.3 Captions
It is always good practice to add a caption to any figure or table. Fortu-
nately, this is very simple in LATEX. All you need to do is use the \caption{’’text’’}
command within the float environment. LATEX will automatically keep track
of the numbering of figures and tables, so you do not need to include this
within the caption text.
\label{marker}
give the object you want to reference a marker, you can see it like a name.
\ref{marker}
can reference the object you have marked before. This prints the number
that was assigned to the object.
\pageref{marker}
will print the number of the page where the object is.
For example, you could write something like:
LATEX will calculate the right numbering for the objects in the document;
the marker you have used to label the object will not be shown anywhere
in the document. Then LATEX will replace the string "\ref{marker}" with
the right number that was assigned to the object. When you use references,
you have to compile your document twice to see the proper output.
4.10 Footnotes
The footnote facility is easy to use. The command you need is:
\footnote{text}
Do not leave a space between the command and the word where you
wish the footnote marker to appear, otherwise LATEX will process that space
56 Typesetting Text
LATEX will obviously take care of typesetting the footnote at the bottom
of the page. Each footnote is numbered sequentially - a process that is
automatically done for you.
Chapter 5
Typesetting Mathematics
\usepackage{amsmath}
in the preamble of the document.
• text — text formulas are displayed inline, that is, within the body of
text where it is declared, for example, I can say that a + a = 2a within
this sentence.
1. Most spaces and line breaks do not have any significance, as all spaces
are either derived logically from the mathematical expressions, or have
to be specified with special commands such as \,, \quad or \qquad
(we’ll get back to that later, in section 5.3.10)
2. Empty lines are not allowed. Only one paragraph per formula.
α, β, γ, Γ, π, Π, φ, ϕ, Φ
For powers with more than one digit, surround the power with {}.
n^{22}
n22
5.3.3 Operators
An operator is a function that is written as a word: e.g. trigonometric
functions (sin, cos, tan), logarithms and exponentials (log, exp). LATEX has
many of these defined as commands:
lim exp(−x) = 0
x→∞
For the modular operator there are two commands: \bmod and \pmod:
a \bmod b
a mod b
x \equiv a \pmod b
x ≡ a (mod b)
\frac{n!}{k!(n-k)!} = \binom{n}{k}
n! n
=
k!(n − k)! k
You can embed fractions within fractions:
5.3 Building Blocks of a Mathematical Formula 61
\frac{\frac{1}{x}+\frac{1}{y}}{y-z}
1 1
x + y
y−z
Note that when appearing inside another fraction, or in inline text ab , a
fraction is noticeably smaller than in displayed mathematics. The \tfrac
and \dfrac commands force the use of the respective styles, \textstyle and
\displaystyle. Similarly, the \tbinom and \dbinom commands typeset the
binomial coefficient.
5.3.5 Roots
The \sqrt command creates a square root surrounding an expression. It
accepts an optional argument specified in square brackets ([ and ]) to change
magnitude:
\sqrt{\frac{a}{b}}
r
a
b
\sqrt[n]{1+x+x^2+x^3+\ldots}
p
n
1 + x + x2 + x3 + . . .
\sum_{i=1}^{10} t_i
10
X
ti
i=1
62 Typesetting Mathematics
The limits for the integrals follow the same notation. It’s also important
to represent the integration variables with an upright d, which in math
mode is obtained through the \mathrm{} command, and with a small space
separating it from the integrand, which is attained with the \, command.
\int_0^\infty \mathrm{e}^{-x}\,\mathrm{d}x
Z ∞
e−x dx
0
The \substack command allows the use of \\ to write the limits over
multiple lines:
\sum_{\substack{
0<i<m \\
0<j<n
}}
P(i,j)
X
P (i, j)
0<i<m
0<j<n
If you want the limits of an integral to be specified above and below the
symbol (like the sum), use the \limits command:
\int\limits_a^b
Zb
Automatic sizing
Very often mathematical features will differ in size, in which case the delim-
iters surrounding the expression should vary accordingly. This can be done
automatically using the \left, \right, and \middle commands. Any of
the previous delimiters may be used in combination with these:
\left(\frac{x^2}{y^3}\right)
x2
y3
P\left(A=2\middle|\frac{A^2}{B}>4\right)
2
A
P A = 2
>4
B
\left\{\frac{x^2}{y^3}\right\}
x2
y3
\left.\frac{x^3}{3}\right|_0^1
1
x3
3 0
64 Typesetting Mathematics
Manual sizing
In certain cases, the sizing produced by the \left and \right commands
may not be desirable, or you may simply want finer control over the delimiter
sizes. In this case, the \big, \Big, \bigg and \Bigg modifier commands
may be used:
(
These commands are primarily useful when dealing with nested delim-
iters.
d
kg(x)
dx
\begin{matrix}
a & b & c \\
d & e & f \\
g & h & i
\end{matrix}
a b c
d e f
g h i
\begin{matrix}
-1 & 3 \\
2 & -4
\end{matrix}
=
\begin{matrix*}[r]
-1 & 3 \\
2 & -4
\end{matrix*}
−1 3 −1 3
=
2 −4 2 −4
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}
a1,1 a1,2 ··· a1,n
a2,1 a2,2 ··· a2,n
Am,n = .
.. .. ..
.. . . .
am,1 am,2 · · · am,n
66 Typesetting Mathematics
In some cases you may want to have finer control of the alignment within
each column, or want to insert lines between columns or rows. This can be
achieved using the array environment, which is essentially a math-mode
version of the tabular environment, which requires that the columns be
pre-specified:
\begin{array}{c|c}
1 & 2 \\
\hline
3 & 4
\end{array}
1 2
3 4
Z
y dx
Z
y dx
Z
y dx
Add a squared and b squared to get c squared. Or, using a more math-
ematical approach: a2 + b2 = c2
\TeX{} is pronounced as
$\tau\epsilon\chi$\\[5pt]
100~m$^{3}$ of water\\[5pt]
This comes from my $\heartsuit$
68 Typesetting Mathematics
TEX is pronounced as τ χ
100 m3 of water
This comes from my ♥
If you want your larger equations to be set apart from the rest of the
paragraph, it is preferable to display them rather than to break the para-
graph apart. To do this, you enclose them between \begin{equation} and
\end{equation}.
\begin{equation}
a^2 + b^2 = c^2
\end{equation}
Add a squared and b squared to get c squared. Or, using a more math-
ematical approach:
a2 + b2 = c2 (5.1)
If you dont want LATEX to number the equations, use the starred version
of equation using an asterisk, equation*, or even easier, enclose the equation
in \[ and \]:
Add a squared and b squared to get c squared. Or, using a more math-
ematical approach
a2 + b2 = c2
a2 + b2 = c2
While \[ is short and sweet, it does not allow switching between num-
bered and not numbered style as easily as equation and equation*.
Note the difference in typesetting style between text style and display
style equations:
Pn 1 π2
This is text style: limn→∞ k=1 k2 = 6 .
\begin{align}
f(x) &= (x+a)(x+b) \\
&= x^2 + (a+b)x + ab
\end{align}
Note that the align environment must not be nested inside an equation
(or similar) environment. Instead, align is a replacement for such environ-
ments; the contents inside an align are automatically placed in math mode.
align* suppresses numbering. To force numbering on a specific line, use
the \tag{...} command before the linebreak.
align is similar, but automatically numbers each line like the equation
environment. Individual lines may be referred to by placing a \label{...}
before the linebreak. The \nonumber or \notag command can be used to
suppress the number for a given line:
\begin{align}
f(x) &= x^4 + 7x^3 + 2x^2 \nonumber \\
&\qquad {} + 10x + 12
\end{align}
Notice that we’ve added some indenting on the second line. Also, we
need to insert the double braces ({}) before the + sign, otherwise latex
won’t create the correct spacing after the + sign. The reason for this is that
without the braces, latex interprets the + sign as a unary operator, instead
of the binary operator that it really is.
More complicated alignments are possible, with additional &’s on a single
line specifying multiple ”equation columns”, each of which is aligned. The
following example illustrates the alignment rule of align*:
\begin{align*}
f(x) &= a x^2+b x +c & g(x) &= d x^3 \\
f’(x) &= 2 a x +b & g’(x) &= 3 d x^2
\end{align*}
\begin{equation}
f(x)=(x+a)(x+b)
\end{equation}
You can also use the \label and \ref (or \eqref from the amsmath pack-
age) commands to label and reference equations, respectively. For equation
number 1, \ref results in 1 and \eqref results in (1) :
72 Typesetting Mathematics
\begin{equation} \label{eq:someequation}
5^2 - 5 = 20
\end{equation}
52 − 5 = 20 (5.9)
this references the equation 5.9.
\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$.
a = bq + r (5.10)
where (5.10) is true if a and b are integers with b 6= c.
Further Reading
LATEX Project
http://www.latex-project.org/
Official website - has links to documentation, information about installing
LATEX on your own computer, and information about where to look for help.
LATEX Wikibook
http://en.wikibooks.org/wiki/LaTeX/
Comprehensive and clearly written. A downloadable PDF is also available.
[4] Michel Goossens, Sebastian Rahtz and Frank Mittelbach. The LATEX
Graphics Companion. Addison-Wesley, Reading, Massachusetts, 1997,
ISBN 0-201-85469-4.
.dvi, 8
.pdf, 8
.tex, 7
TEXworks, 3
commands, 10
compilation, 7
distribution, 3
eps, 4
group, 10
LaTeX, 1
latex, 8
PDF viewer, 3
pdflatex, 8
Sumatra PDF, 3
WYSIWYG, 1