. 1( : 23) . . >>
L TEX Tutorials
A
A PRIMER
Indian TEX Users Group
Trivandrum, India
2003 September
A
L TEX TUTORIALS ” A PRIMER
Indian TEX Users Group

EDITOR: E. Krishnan
COVER: G. S. Krishna

Copyright c 2002, 2003 Indian TEX Users Group
Floor III, SJP Buildings, Cotton Hills
Trivandrum 695014, India
http://www.tug.org.in

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU
Free Documentation License, version 1.2, with no invariant sections, no front-cover texts, and no
back-cover texts. A copy of the license is included in the end.

This document is distributed in the hope that it will be useful, but without any warranty; without
even the implied warranty of merchantability or ¬tness for a particular purpose.

Online versions of this tutorials are available at:
http://www.tug.org.in/tutorials.html
PREFACE

The ideal situation occurs when
the things that we regard as beau-
tiful are also regarded by other
people as useful.
” Donald Knuth

For us who wrote the following pages, TEX is something beautiful and also useful. We
enjoy TEX, sharing the delights of newly discovered secrets amongst ourselves and won-
dering ever a new at the in¬nite variety of the program and the ingenuity of its creator.
We also lend a helping hand to the new initiates to this art. Then we thought of extend-
ing this help to a wider group and The Net being the new medium, we started an online
tutorial. This was well received and now the Free Software Foundation has decided to
publish these lessons as a book. It is a ¬tting gesture that the organization which upholds
the rights of the user to study and modify a software publish a book on one of the earliest
programs which allows this right.

The TUGIndia Tutorial Team

3
4
CONTENTS

The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
I.
What is L TEX? “ 7 • I.2 Simple typesetting “ 8 • I.3 Fonts “ 13 • I.4 Type size “ 15
A
I.1

The Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
II.
II.1 Document class “ 17 • II.2 Page style “ 18 • II.3 Page numbering “ 19 • II.4 Formatting
lengths “ 20 • II.5 Parts of a document “ 20 • II.6 Dividing the document “ 21 • II.7 What next?
“ 23

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
III.
Introduction “ 27 • III.2 natbib “ 28
III.1

Bibliographic Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
IV.
IV.1 The BIBT X program “ 33 • IV.2 BIBT X style ¬les “ 33 • IV.3 Creating a bibliographic
E E
database “ 34

Table of contents, Index and Glossary . . . . . . . . . . . . . . . . . . . . . . 39
V.
V.1

Displayed Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
VI.
Borrowed words “ 47 • VI.2 Poetry in typesetting “ 48 • VI.3 Making lists “ 48 • VI.4 When
VI.1
order matters “ 51 • VI.5 Descriptions and de¬nitions “ 54

Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
VII.
Keeping tabs “ 57 • VII.2 Tables “ 62
VII.1

Typesetting Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
VIII.
The basics “ 77 • VIII.2 Custom commands “ 81 • VIII.3 More on mathematics “ 82 •
VIII.1
Mathematics miscellany “ 89 • VIII.5 New operators “ 101 • VIII.6 The many faces of
VIII.4
mathematics “ 102 • VIII.7 And that is not all! “ 103 • VIII.8 Symbols “ 103

Typesetting Theorems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
IX.
Theorems in L TEX “ 109 • IX.2 Designer theorems”The amsthm package “ 111 • IX.3
A
IX.1
Housekeeping “ 118

Several Kinds of Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
X.
X.1 LR boxes “ 119 • X.2 Paragraph boxes “ 121 • X.3 Paragraph boxes with speci¬c height “
122 • X.4 Nested boxes “ 123 • X.5 Rule boxes “ 123

Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
XI.
The figure environment “ 125 • XI.2 The table environment “ 130
XI.1

5
6 CONTENTS

Cross References in LTEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
A
XII.
Why cross references? “ 135 • XII.2 Let L TEX do it “ 135 • XII.3 Pointing to a page”the
A
XII.1
package varioref “ 138 • XII.4 Pointing outside”the package xr “ 140 • XII.5 Lost the keys? Use
lablst.tex “ 140

Footnotes, Marginpars, and Endnotes . . . . . . . . . . . . . . . . . . . . . . 143
XIII.
Footnotes “ 143 • XIII.2 Marginal notes “ 147 • XIII.3 Endnotes “ 148
XIII.1
TUTORIAL I

THE BASICS

A
WHAT IS L TEX?
I.1.

The short and simple answer is that LTEX is a typesetting program and is an extension
A

of the original program TEX written by Donald Knuth. But then what is a typesetting
program?
To answer this, let us look at the various stages in the preparation of a document
using computers.

The text is entered into the computer.
1.
The input text is formatted into lines, paragraphs and pages.
2.
The output text is displayed on the computer screen.
3.
The ¬nal output is printed.
4.

In most word processors all these operations are integrated into a single application
package. But a typesetting program like TEX is concerned only with the second stage
above. So to typeset a document using TEX, we type the text of the document and the
necessary formatting commands in a text editor (such as Emacs in GNU/Linux) and then
compile it. After that the document can be viewed using a previewer or printed using a
printer driver.
TEX is also a programming language, so that by learning this language, people can
write code for additional features. In fact LTEX itself is such a (large) collection of extra
A

features. And the collective effort is continuing, with more and more people writing extra
packages.

A small example
I.1.1.

Let us see LTEX in action by typesetting a short (really short) document. Start your
A

favorite text editor and type in the lines below exactly as shown
\documentclass{article}
\begin{document}
This is my \emph{first} document prepared in \LaTeX.
\end{document}

Be especially careful with the \ character (called the backslash) and note that this is
different from the more familiar / (the slash) in and/or and save the ¬le onto the hard
disk as myfile.tex. (Instead of myfile you can use any name you wish, but be sure to
have .tex at the end as the extension.) The process of compiling this and viewing the
output depends on your operating system. We describe below the process of doing this
in GNU/Linux.

7
8 THE BASICS
I.

At the shell prompt type

latex myfile

You will see a number of lines of text scroll by in the screen and then you get the prompt
back. To view the output in screen, you must have the X Window running. So, start X if
you have not done so, and in a terminal window, type

xdvi myfile

A window comes up showing the output below

This is my ¬rst document prepared in LTEX.
A

Now let us take a closer look at the source ¬le (that is, the ¬le you have typed).
The ¬rst line \documentclass{article} tells LTEX that what we want to produce is an
A

article. If you want to write a book, this must be changed to \documentclass{book}.
The whole document we want to typeset should be included between \begin{document}
and \end{document}. In our example, this is just one line. Now compare this line in the
source and the output. The ¬rst three words are produced as typed. Then \emph{first},
becomes ¬rst in the output (as you have probably noticed, it is a common practice to
emphasize words in print using italic letters). Thus \emph is a command to LTEX to A

typeset the text within the braces in italic1 . Again, the next three words come out without
any change in the output. Finally, the input \LaTeX comes out in the output as LTEX.
A

Thus our source is a mixture of text to be typeset and a couple of LTEX commands
A

\emph and \LaTeX. The ¬rst command changes the input text in a certain way and the
second one generates new text. Now call up the ¬le again and add one more sentence
given below.
This is my \emph{first} document prepared in \LaTeX. I typed it
on \today.

What do you get in the output? What new text does the command \today generate?

Why LTEX?
A
I.1.2.

So, why all this trouble? Why not simply use a word processor? The answer lies in the
motivation behind TEX. Donald Knuth says that his aim in creating TEX is to beautifully
typeset technical documents especially those containing a lot of Mathematics. It is very
dif¬cult (sometimes even impossible) to produce complex mathematical formulas using a
word processor. Again, even for ordinary text, if you want your document to look really
beautiful then LTEX is the natural choice.
A

SIMPLE
I.2. TYPESETTING

We have seen that to typeset something in LTEX, we type in the text to be typeset together
A

with some LTEX commands. Words must be separated by spaces (does not matter how
A

many) and lines maybe broken arbitrarily.
The end of a paragraph is speci¬ed by a blank line in the input. In other words,
whenever you want to start a new paragraph, just leave a blank line and proceed. For
example, the ¬rst two paragraphs above were produced by the input
1 This is not really true. For the real story of the command, see the section on fonts.
9
SIMPLE
I.2. TYPESETTING

We have seen that to typeset something in \LaTeX, we type in the
text to be typeset together with some \LaTeX\ commands.
Words must be separated by spaces (does not matter how many)
and lines maybe broken arbitrarily.

The end of a paragraph is specified by a \emph{blank line}
in the input. In other words, whenever you want to start a new
paragraph, just leave a blank line and proceed.

Note that the ¬rst line of each paragraph starts with an indentation from the left
margin of the text. If you do not want this indentation, just type \noindent at the start
of each paragraph for example, in the above input, \noindent We have seen ... and
\noindent The end of ... (come on, try it!) There is an easier way to suppress para-
graph indentation for all paragraphs of the document in one go, but such tricks can wait.

Spaces
I.2.1.

You might have noticed that even though the length of the lines of text we type in a
paragraph are different, in the output, all lines are of equal length, aligned perfectly on
the right and left. TEX does this by adjusting the space between the words.
In traditional typesetting, a little extra space is added to periods which end sentences
and TEX also follows this custom. But how does TEX know whether a period ends a
sentence or not? It assumes that every period not following an upper case letter ends a
sentence. But this does not always work, for there are instances where a sentence does
end in an upper case letter. For example, consider the following

Carrots are good for your eyes, since they contain Vitamin A. Have you ever seen a rabbit
wearing glasses?

The right input to produce this is
Carrots are good for your eyes, since they contain Vitamin A\@. Have
you ever seen a rabbit wearing glasses?

Note the use of the command \@ before the period to produce the extra space after the
period. (Remove this from the input and see the difference in the output.)
On the other hand, there are instances where a period following a lowercase letter
does not end a sentence. For example

The numbers 1, 2, 3, etc. are called natural numbers. According to Kronecker, they were made
by God; all else being the work of Man.

To produce this (without extra space after etc.) the input should be
The numbers 1, 2, 3, etc.\ are called natural numbers. According to
Kronecker, they were made by God;all else being the works of Man.

Here, we use the command \ (that is, a backslash and a space”here and elsewhere, we
sometimes use to denote a space in the input, especially when we draw attention to the
space).
There are other situations where the command \ (which always produce a space in
the output) is useful. For example, type the following line and compile it.
I think \LaTeX is fun.
10 THE BASICS
I.

You get

I think LTEXis fun.
A

What happened to the space you typed between \LaTeX and is? You see, TEX gobbles up
all spaces after a command. To get the required sequence in the output, change the input
as
I think \LaTeX\ is fun.

Again, the command \ comes to the rescue.

Quotes
I.2.2.

Have you noticed that in typesetting, opening quotes are different from closing quotes?
Look at the TEX output below

Note the difference in right and left quotes in ˜single quotes™ and “double quotes”.

This is produced by the input
Note the difference in right and left quotes in ˜single quotes™
and ˜double quotes™™.

Modern computer keyboards have a key to type the symbol ` which produces a left quote
in TEX. (In our simulated inputs, we show this symbol as ˜.) Also, the key ™ (the usual
˜typewriter™ quote key, which also doubles as the apostrophe key) produces a left quote
in TEX. Double quotes are produced by typing the corresponding single quote twice. The
˜usual™ double quote key " can also be used to produce a closing double quote in TEX.
If your keyboard does not have a left quote key, you can use \lq command to produce
it. The corresponding command \rq produces a right quote. Thus the output above can
also be produced by
Note the difference in right and left quotes in \lq single
quotes\rq\ and \lq\lq double quotes\rq\rq.

(Why the command \ after the ¬rst \rq?)

Dashes
I.2.3.

In text, dashes are used for various purposes and they are distinguished in typesetting by
their lengths; thus short dashes are used for hyphens, slightly longer dashes are used to
indicate number ranges and still longer dashes used for parenthetical comments. Look at
the following TEX output

X-rays are discussed in pages 221“225 of Volume 3”the volume on electromagnetic waves.

This is produced from the input
 . 1( : 23) . . >>