Introduction to LATEX Basics
Introduction to LATEX Basics
Michael Urban
TRW Software Productivity Project
1 Introduction 1
2 Getting Started 3
2.1 General Operation . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 The Input File . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Running LATEX . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Output Devices . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Typesetting versus Typing . . . . . . . . . . . . . . . . . . . . 8
2.5.1 Quotation Marks . . . . . . . . . . . . . . . . . . . . . 8
2.5.2 Dashes . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5.3 Ligatures . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5.4 Line Breaking . . . . . . . . . . . . . . . . . . . . . . . 9
2.5.5 Other Keyboard Characters . . . . . . . . . . . . . . . 10
2.6 What Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Control Sequences 11
3.1 The Escape Character . . . . . . . . . . . . . . . . . . . . . . 11
3.1.1 Some Control Symbols . . . . . . . . . . . . . . . . . . 12
3.1.2 A Control Word Example . . . . . . . . . . . . . . . . 12
3.1.3 Changing the Type Style . . . . . . . . . . . . . . . . . 13
3.2 Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
i
4.4 The verbatim Environment . . . . . . . . . . . . . . . . . . . 19
4.5 The center Environment . . . . . . . . . . . . . . . . . . . . 20
4.6 flushleft and flushright . . . . . . . . . . . . . . . . . . . 21
4.7 Nesting Environments . . . . . . . . . . . . . . . . . . . . . . 21
4.8 List-Making Environments . . . . . . . . . . . . . . . . . . . . 22
4.8.1 Itemized Lists . . . . . . . . . . . . . . . . . . . . . . . 22
4.8.2 Enumerated Lists . . . . . . . . . . . . . . . . . . . . . 23
4.8.3 Description Lists . . . . . . . . . . . . . . . . . . . . . 23
4.8.4 Nesting Lists . . . . . . . . . . . . . . . . . . . . . . . 24
5 Putting It Together 27
5.1 Numbered Sections . . . . . . . . . . . . . . . . . . . . . . . . 27
5.2 Footnotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5.3 The Table of Contents . . . . . . . . . . . . . . . . . . . . . . 28
5.4 The Title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.5 Page Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.6 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.7 Appendices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.8 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
7 Cross-References 40
8 Equation Formatting 42
8.1 Basic Rules of Math Mode . . . . . . . . . . . . . . . . . . . . 43
8.2 Subscripts and Superscripts . . . . . . . . . . . . . . . . . . . 43
8.3 Mathematical Symbols . . . . . . . . . . . . . . . . . . . . . . 44
8.4 Logs and Sins . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
8.5 Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
ii
8.6 Some Random Examples . . . . . . . . . . . . . . . . . . . . . 45
9 LATEX Errors 49
9.1 Errors from TEX . . . . . . . . . . . . . . . . . . . . . . . . . 49
9.2 Errors from LATEX . . . . . . . . . . . . . . . . . . . . . . . . . 50
9.3 Severe Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
9.3.1 No Document Environment . . . . . . . . . . . . . . . 52
9.3.2 No Document Style . . . . . . . . . . . . . . . . . . . . 52
9.3.3 Invalid Document Style . . . . . . . . . . . . . . . . . . 53
9.3.4 Accidental Mathematics Mode . . . . . . . . . . . . . . 53
9.4 Overfull Hboxes . . . . . . . . . . . . . . . . . . . . . . . . . . 55
9.5 Underfull Vboxes . . . . . . . . . . . . . . . . . . . . . . . . . 56
iii
Chapter 1
Introduction
1
Furthermore, as others have pointed out, word processors tend to be
“what you see is all you’ve got.” With a system like LATEX, a few simple
changes to the input file can turn text originally intended as a short memo
into a journal paper or a business letter.
Because LATEX is a powerful program, it can automatically number sec-
tions of a document, produce indexes and cross-references, and allow you to
specify ‘floating’ portions of a document like tables and figures. It knows
about different document styles so that you can tell LATEX what kind of doc-
ument you’re producing (article, letter, etc.) and LATEX will automatically
set the margins, the heading styles, and so on for you.
When dealing with LATEX (or any computer program, for that matter),
it’s always a good idea to remember that computers are pretty stupid and
usually can’t figure out what you want to do unless you tell them. This means
that there’s a fair amount of detail that has to be mastered in order to get
things to come out the way you want. Fortunately, LATEX minimizes the
number of details you absolutely need to know; simple documents are quite
simple to prepare, while trickier things are more complicated in reasonable
proportion to their complexity.
2
Chapter 2
Getting Started
In which we learn what we need to know to produce our first LATEX document.
1. Prepare an input file in the LATEX notation that describes what your
document is supposed to look like. This description will include the
document style, the contents of the document, changes in type style,
and so on.
2. Process your input file with LATEX. This will produce a so-called dvi
(or device-independent) file as output.
3. Print the dvi file on the high-quality printer (laser printer or photo-
typesetter) that’s available on your system.
3
2.2 The Input File
Your input for LATEX is just a normal text file that you can create with your
favorite text editor. The name of the file should be something.tex, i.e., you
can call it anything you like so long as the name ends with “.tex”. The
examples in this paper will assume the file is named mydoc.tex.
The first line of the input file must be something that looks like this:
\documentstyle{article}
The word article on this line can be replaced by report, letter, or any
other document style available on your system. This line tells LATEX just what
sort of document you’re producing, so that LATEX can set up the margins,
heading style, and so on. article is a good document style for most simple
documents.
The body of the document must be surrounded by two special lines:
\begin{document}
must precede the body of the document, and (as you might have suspected),
\end{document}
is the last line of the document. Thus, a kind of minimalist LATEX input file
might appear as follows:
\documentstyle{article}
\begin{document}
I am a sentence.
\end{document}
In Chapter 4, we’ll see other things that can be done with \begin and
\end.
A simple text document is just a series of words, sentences and para-
graphs. Words are separated by one or more blanks and/or tab characters,
or the end of the input line. In fact, the number of blanks that are used to
separate words is unimportant; ten blanks have exactly the same effect as
one.
Sentences end with punctuation marks like periods and question marks,
and are separated by blanks or the end of the input line. Remember, the
4
number of blanks makes no difference; LATEX knows about periods and adds
the extra space after the period automatically.
LATEX allows you to separate paragraphs easily: just put an extra blank
line between paragraphs. This blank line is, by the way, just about the only
case in which LATEX cares about the end of a line. In all other cases, the
end of a line has exactly the same significance as a space — it just separates
words.
You’re just about ready to produce a simple document. But beware!
There are ten special characters that are part of LATEX’s special notation:
# $ % & { } ~ _ ^ \
The first six of these (which are the most common in regular copy) can be
placed in the document by preceding them with the back-slash character.
For example, the dollar sign can be placed harmlessly in your text by typing
“\$”. Find that back-slash character on your terminal and get used to typing
it; it’s an important part of LATEX’s special notation. The other characters
take a little more work, but we’ll talk about that later.
\documentstyle{article}
\begin{document}
But last week, humor turned to alarm when the group, acting
through a local front called the Age of Enlightenment Foundation,
bought the financially beleaguered University of the East, one of
the largest private universities in Asia with three campuses and
47,000 students.
5
The blank line ends the previous paragraph and starts a new one. To process
this file, we run LATEX. The exact method for doing this will vary from
computer to computer. On UNIX systems, the command would be:
$ latex mydoc
The dollar sign is the prompt for the Shell. Notice that while the file name
is mydoc.tex, we only have to say mydoc here; LATEX infers the .tex part
itself. Now LATEX processes the file and types information on what it’s doing
at your terminal. For this example, you’ll see something like this:
This is TeX, Version 1.1 for Berkeley UNIX (preloaded format=lplain 84.8.13)
(mydoc.tex
LaTeX Version 2.06a - Release 7 July 84
(/usr/lib/tex/macros/article.sty
Document Style ’article’. Version 0.91 - released 25 June 1984
(/usr/lib/tex/macros/art10.sty))
No file mydoc.aux.
[1] (mydoc.aux)
Output written on mydoc.dvi (1 page, 948 bytes).
Transcript written on mydoc.log.
The first line (This is TeX. . .) tells which version of TEX is in use. TEX
is the basic text-processing system which is the basis of LATEX; this distinc-
tion should not concern you now, but helps when you’re trying to decipher
error messages as described in Chapter 9. In the next line, you can see that
mydoc.tex is being read. The next four lines describe the version of LATEX
being used, and the various files being read that comprise the article doc-
ument style.
The message, “No file mydoc.aux” is a harmless warning; it simply
means that you haven’t run this document through LATEX before. LATEX uses
mydoc.aux to retain cross-reference and Table-of-Contents information.
The appearance of “1” in square brackets indicates that Page 1 has been
processed. When the input file has been read, mydoc.aux is re-read to see
if any of the cross-reference or Table of Contents information has changed.
You don’t have to worry about this yet.
The last output lines tell where the results are. The device-independent
representation of your document are kept in a file called mydoc.dvi, which
contains one page of output and occupies 948 bytes of disk space (if you don’t
know about bytes and the like, don’t worry about it). A transcript of all this
6
stuff is kept in a file called mydoc.log in case you want to look at it later.
If you had received a bunch of error messages, you might well want to have
this transcript around.
Finally, when you print the results, you’ll get output much like the
following1 (on its own page, with the page number ‘1’ at the bottom):
But last week, humor turned to alarm when the group, acting through
a local front called the Age of Enlightenment Foundation, bought the finan-
cially beleaguered University of the East, one of the largest private universi-
ties in Asia with three campuses and 47,000 students.
Officials of the Education Ministry then disclosed that the maharishi’s
people had made overtures to buy at least half a dozen other debt-ridden
educational institutions. The deputy minister of education said the group
had brought millions of dollars into the country and was keeping the money
in three local banks.
Actually, the output here has been formatted slightly differently from the way
it would actually appear on a page, and is presented here as a ‘quotation’.
We’ll see how this quotation facility works in Chapter 4.
7
to print dvi files on the Imagen Imprint-10 and Imagen 5/480 laser printers.
In our example, the output might be produced by typing the Shell command:
$ iptex mydoc
Notice that iptex, like LATEX, infers the name mydoc.dvi from the name
mydoc.
You now know enough to produce many kinds of simple documents using
L TEX. However, you’ll soon discover that some of the things you thought you
A
knew all about (like quotation marks) don’t seem to work just the way you’d
expect. This is because a high-quality document has a lot more characters
than you have keys on your keyboard. To find out how to handle some of
these characters, read on . . .
‘this’
You can produce the double-quote marks simply by typing two single-
quote marks. To set “See Spot run” you would use the input:
8
2.5.2 Dashes
Your terminal keyboard has only one kind of dash (or hyphen) on it. But
a well-printed book will use different length symbols for different kinds of
dashes.
The shortest dash is the hyphen, found in compounds like “well-printed”.
To set a hyphen, just use the regular hyphen (dash?) key on your terminal.
The next longer dash is called an “en-dash” because it is just about as
wide as the letter “N”. It is used for number ranges like “pages 13–15” or in
places like “Clause 5.4–3”. Type two hyphens to produce an en-dash.
The longest dash is called an “em-dash” and is about as wide as the letter
“M”—it’s the dash used in English prose. Type three hyphens to produce
an em-dash.
As an example, the sentence, “The en-dash—you know what it is—is
discussed on pages 14–15” is produced by the input,
2.5.3 Ligatures
Actually, you don’t have to worry about ligatures, but it’s nice to know that
LATEX automatically takes care of things like printing the letters f and i in
the word “find” as a single unit, the way the pros do it. These ligatures like
“fi”, “ffi”, “fl” and “ffl” improve the appearance of printed text substantially.
LATEX also causes combinations of letters like A and V to be moved together
to improve their appearance.
Chapter~5
9
2.5.5 Other Keyboard Characters
There are still other non-alphanumeric characters found on the keyboard that
deserve some attention. The characters ‘@’, ‘*’, ‘+’, and ‘=’ are all produced
correctly from the corresponding keyboard keys. On the other hand, the
characters ‘<’, ‘>’, and ‘|’ are not normally used in regular text copy, and
should only be used in mathematics mode (discussed in Chapter 8). If you
use them while producing output in normal text, you’ll get the characters ‘¡’,
‘¿’, and ‘—’ instead. While the first two of these are useful when typesetting
Spanish text, LATEX allows you to type them more conveniently as !‘ and ?‘
respectively.
The further you go, the less easy will it be to withdraw; yet
no oath or bond is laid on you to go further than you will.
10
Chapter 3
Control Sequences
11
3.1.1 Some Control Symbols
We’ve already seen that some control symbols like \% and \& are used to print
characters like ‘%’ and ‘&’ (which would otherwise be significant to LATEX).
Many of LATEX’s control symbols are used to place accents on letters. For
example, to generate the phrase “Piña Colada” I typed “Pi\~na Colada”.
The French word “parlé” is typed as “parl\’e”. When you place an accent
on the letter i or j, you have to get rid of the dots, so the control words \i
and \j, representing dotless ‘ı’ and ‘’, can be used. For example, “manĝâo”
is typed as “man\^ga\^\j o.”
One special control symbol, \\, is used to terminate a line and begin a
new one. For example,
this line was initiated by the input,
For example,\\this line was
Normally, \\ is used in special places like titles.
12
3.1.3 Changing the Type Style
Let’s look at some specific control words for changing the type style. These
are the commands that produce bold or slanted type, or rather small type
where needed.
When you switch from a slanted type face to an unslanted font, you should
add the control symbol \/, which adds a little extra space. Otherwise, you
might set a word like did n’t as didn’t, in which the ‘d’ and ‘n’ are too close
together. One correct form is
\normalsize Further output appears in the normal type size (ten points in
a ten-point paper).
13
\Large Larger still (fourteen points).
\scriptsize Smaller still (seven points). The size normally seen in mathe-
matics superscripts.
3.2 Groups
To see how groups work, and why one wants to use them, consider the sen-
tence, “The bold Roman marched home.” The control word \bf switches
to bold-face type, and \rm switches to the (normal) roman lettering, so you
might type,
14
But there’s an easier way to do it. When any LATEX input is placed between
curly braces, it takes place within its own environment, a kind of world of
its own. Any changes that occur within this environment, such as changes
in type style, are un-done when that environment finishes. So the easier way
to type the example would be
because the change to bold-face type takes place within its own environment
and doesn’t affect what happens outside the curly-brace boundaries.
3.3 Parameters
Newspaper and television writers (and a lot of other people) use the word
“parameter” to mean “limit” or “boundary,” but this usage is incorrect.
The actual meaning is “variable,” i.e., something that changes from one
occurrence to the next. Many LATEX control words require that a parameter
be supplied. For example, as we’ve seen, the \documentstyle control word
requires that you specify just which document style you’re selecting. In
LATEX, such parameters are placed between curly braces—a second use for
the braces besides grouping.
We’ll see later that some LATEX commands also have optional parameters
supplied (if at all) inside square brackets. But before we delve further into
the details of LATEX’s grammar, we’ll turn to a pair of control words (which
require parameters) that you’ll be using a lot.
15
Chapter 4
Back in Section 2.3, we displayed the sample LATEX output using narrower
margins. This was a quotation and was accomplished by establishing an en-
vironment in which several aspects of page layout (left margin, right margin,
paragraph indentation) were changed. Remember, these environments are
just like the ones you get when you put something inside the curly braces;
any changes in the style or size of type that happen inside the environment
are undone when the environment finishes up.
You can make such an environment by using a command like
\begin{quotation}
and ending it with \end{quotation}. You’ve seen these \begin and \end
commands before; your entire LATEX document occurs within an environment
called document. In other words, the environment to be bounded by the
\begin and \end ‘brackets’ is supplied as a parameter when those control
words are used. Notice that you will get an error if you try to do something
like:
\begin{something}
\begin{somethingelse}
\end{something}
\end{somethingelse}
16
That is, environments cannot overlap; you must \end the ‘inner’ environment
before you end the ‘outer’ one. Many people, while editing the input file, will
type both the \begin and \end commands first, and then back up and insert
whatever they are doing in between the two lines in order to guarantee that
everything matches up OK.
Let’s look at some of the more useful formatting environments.
\begin{quote}
\small
Cardinal Jaime L. Sin, the Roman Catholic archbishop of Manila,
warned that the group had the trappings of a cult, and convened a
conference of educators, scientists, theologians and philosophers
to study its potential impact.
\end{quote}
would produce:
17
Long quotes sometimes look better in a smaller size of type, as in this
example. It’s a good idea to end the last paragraph of a quote with a blank
line before leaving the quote environment, especially if you’ve changed the
type size within the quote environment1 . Notice the extra spacing before
and after the quote. Also notice that the change to the \small size type is
un-done when the quote environment ends.
\begin{verse}
There once was a poet named Dan,\\
Whose limericks never would scan.\\
When told this was so,\\
He said, ‘‘Yes, I know,\\
1
This is because otherwise the paragraph isn’t divided into lines until after the envi-
ronment closes, and by then the change in line spacing has been un-done. A little weird,
admittedly, but just follow the rule and you won’t go wrong.
18
It’s because I try to put every syllable into the
last line that I possibly can.’’
\end{verse}
\begin{verbatim}
\small\it This is \today.
\end{verbatim}
produces:
19
word becomes the bracketing character for the parameter, and all subsequent
characters (including the escape character) are output in typewriter font until
the bracketing character is seen again.
For example, I typed “\verb” by typing
\verb|\verb|
using the vertical bar as my bracketing character, although any other char-
acter would have worked just as well.
\begin{center}
{\bf Bitts and Bights}\\
{\small\it A Novel}
\end{center}
Note that the line separation has to be explicitly typed (as \\). If you
have a lot of text input without line breaks, you’ll end up with a paragraph,
each line of which is centered:
20
4.6 flushleft and flushright
Sometimes you need to produce something like this:
Samuel Klugarsh
666 Clarkle Street
Hogboro, NJ 11780
in which each line is flush right with the right-hand margin. The flushright
environment allows this. The input in this case was:
\begin{flushright}
Samuel Klugarsh\\
666 Clarkle Street\\
Hogboro, NJ 11780
\end{flushright}
Once again, each line break should be explicitly specified, or you’ll get a
paragraph in which each line has a “ragged” left margin.
On the other hand, sometimes a “ragged” right margin is desirable. In
this case, the flushleft environment can be used. For example,
“Honestly,” said Helena Benitez, the independent assemblywoman
who spearheaded the legislative investigation, “it looks to me like
either an invasion from Mars or an invasion of locusts that have
come to feast on the grain when it’s ripe.”
\end{flushleft}
\end{quote}
21
The extra indentation of the flushleft environment in the input file doesn’t
affect the output at all, since LATEX ignores extra blanks; it’s just there to
improve the readability of the input.
This is a good place to stop and get in some practice. Put together a
short LATEX document with a centered, large, boldface title, a ragged-
right paragraph, and a small italic quotation, just to get the feel of all
this stuff. When you’re pretty confident with all these curly braces and
begins and ends, go on to the next section.
• Description lists, in which each item has a text label instead of a bullet
or number.
The alert reader has noticed that each item in the list is preceded by the
control word \item. In particular, the first thing in an itemize (or other
list-making) environment must be an \item or an error will result. There
are no other restrictions. For example a new paragraph will be indented at
the same level as the “bulleted” paragraphs, so everything will look neat.
22
Because LATEX ignores extra spaces, the input can be formatted neatly, as
in this example, to improve readability. This sort of thing helps a lot when
you’re fixing things later.
3. Description lists, in which each item has a text label instead of a bullet
or number.
Ada The Countess of Lovelace. She was the paramour of Charles Babbage,
and was probably the world’s first programmer.
Euclid Greek mathematician who gave his name to the geometry of the
plane, one of the few branches of ancient science still taught.
23
The first \item command must specify “Ada” as the hanging tag. So
“\tt Ada” will be the parameter for \item and in this case the \item com-
mand will be
\item [\tt Ada]
Note that the change to typewriter font happens inside a separate environ-
ment because a parameter (optional or mandatory) is automatically placed
inside its own group. Here’s the entire specification:
\begin{description}
\item[\tt Ada] The Countess of Lovelace. She was the paramour
of Charles Babbage, and was probably the world’s first
programmer.
\item[\tt Pascal] Mathematician and philosopher, Blaise Pascal
is probably best known for his triangular
representation of binomial expansion.
\item[\tt Euclid] Greek mathematician who gave his name to
the geometry of the plane, one of the few branches
of ancient science still taught.
\end{description}
24
1. Commands
2. System calls
3. Subroutines
is quite a complex-looking list, but is really just a series of enumerate envi-
ronments, nested inside one another:
\begin{enumerate}
\item Commands
\begin{enumerate}
\item Editor commands
\begin{enumerate}
\item Reading
\item Writing
\item Cursor movement
\end{enumerate}
\item Mail commands
\begin{enumerate}
\item Composing mail
\begin{enumerate}
\item Editing the file
\item Mail headers
\end{enumerate}
\item Delivering mail
\end{enumerate}
\item Communication commands
\end{enumerate}
25
\item System calls
\item Subroutines
\end{enumerate}
Note that the different numbering styles associated with the different levels
are handled automatically by LATEX. Similarly, nested itemize environments
produce different hanging tags (bullets at level 1, dashes at level two, and so
on) and indentation levels for the different nesting levels.
So far, you know how to prepare LATEX documents with different sizes
and styles of type, quotations, verbatim input, ragged left and/or right
margins, centered material, and lists with different kinds of tags. This
is another good place to stop and practice. The next chapter will de-
scribe how to put a complete paper together with numbered sections,
footnotes, title page, and table of contents.
26
Chapter 5
Putting It Together
In which we learn how to put together a beautifully formatted paper like this one.
27
Later on, we’ll see how a Table of Contents is created, but for now we’ll
observe that you sometimes want to have a chapter or section heading that
is different from what appears in the Table of Contents entry. In this case,
an optional parameter for the sectioning command gives the data that will
appear in the Table of Contents. For example, if I began a sub-subsection
with the line,
then the heading of the sub-subsection would read “Spiders and Crabs”, but
the Table of Contents entry for it would read simply “Crustaceans”.
5.2 Footnotes
There has probably never been a technical paper of any kind that appeared
without footnotes, so LATEX was designed to make footnoting easy. By now,
you’re pretty familiar with the way LATEX does things, so it should come as
no surprise that this footnote1 was produced by the following input line:
28
know which page a given section or sub-section begins on until that page
has actually been printed. So it retains this information in a file with a
name like mydoc.aux and rereads this file on the next run when it sees the
\tableofcontents command.
\title{Advanced Lessons\\
in\\
Hyperstellar Archaeology}
\author{S. Klugarsh}
\date{July 4, 2000 AD}
These commands actually don’t output anything! But they provide infor-
mation that is used by yet another LATEX control word, \maketitle. In the
article document style, the title block (automatically centered and spaced)
goes on the first page of the document; in the report style it goes on a sepa-
rate page, and a new page immediately follows, so you can immediately follow
a \maketitle with a \tableofcontents if you like. If you use \maketitle,
you must provide a title and author, even if you only say
\author{}
29
which changes the style in which the current (and subsequent) pages are to
be numbered, resetting the page number to ‘1’ as it does so. It takes a
single parameter which describes the numbering style; roman causes lower-
case roman numerals to appear, and arabic uses normal arabic numerals.
5.6 An Example
The first several lines of the input file that produced this document were:
\documentstyle[12pt,twoside]{report}
\title{\Large\bf An Introduction to \LaTeX}
\author{\normalsize Michael Urban\\
\it TRW Software Productivity Project}
\date{\normalsize\today}
\begin{document}
\maketitle
\pagenumbering{roman}
\tableofcontents
\chapter{Introduction}
\pagenumbering{arabic}
This paper is intended to introduce you to the \LaTeX\footnote
{Pronounced either ‘‘lah-{\it tekh\/}’’ or ‘‘{\it lay\/}-tekh’’}
document preparation
The only new item here is the twoside option which sets the margins cor-
rectly for two-sided copying. A complete list of the various options which can
be used to set various aspects of the document’s appearance appears in the
LATEX documentation but is beyond the scope of this introduction. A sup-
plementary volume describing the different ways to control the appearance
of LATEX output is planned.
5.7 Appendices
Rather than providing a separate series of sectioning commands for appen-
dices and their subsections, LATEX lets you start a set of appendices with the
control word \appendix. The effect of \appendix is to cause subsequent
major headings (i.e., Chapters if you’re doing a report and Sections for an
30
article) to refer to numbered appendices rather than major sections. For
example, if I were to say,
\appendix
\chapter{List of Fonts}
\chapter{Names of Suppliers}
then the two new chapters would be headed as “Appendix A” and “Ap-
pendix B” and would be so listed in the Table of Contents. Once you’ve
done this, of course, it’s not particularly easy to go back to regular chapter
numbering (and how you can do it is beyond the scope of this paper).
5.8 Comments
Sometimes you want to add a comment to the input file for your own benefit
while you’re working on the document, but not to be output by LATEX. In
this case, you can precede the comment with a percent sign (%). Anything
that follows a percent sign on a line of input, up to and including the end
of the line, is ignored by LATEX. So your input file might include something
like:
As everyone knows, there are ?? % LOOK THIS UP!
artificial elements in the periodic table.
You now know how to produce a complete paper using LATEX. The
remaining material in this paper is “optional” stuff that talks about
things like figures and tables, tabular output, and mathematics. If you
can get along without these things for a while, stop reading now and
start producing beautifully formatted papers.
31
Chapter 6
In which we learn how to set up tabular information and let it “float” to a conve-
nient spot.
Table 6.1 was actually typed in between the figure ‘6.1’ and the word “was”
in this sentence. It managed to migrate to the bottom of the page because
the specification for the table was enclosed in a special environment (the
table environment) which automatically moves to a convenient place where
there is room.
Letter Meaning
h here
b bottom
t top
p float page
32
6.1.1 The table Environment
The table environment causes its contents to be placed in a separate area
from the surrounding text. The exact location in which the table ends up
depends on the amount of space currently available on a page, and on an
optional parameter supplied with the \begin{table} control sequence. Ta-
ble 6.1 shows the options available.
h Causes the table to be placed at the current line if possible.
b Causes the table to be placed at the bottom of a page of text, if possible.
t Causes the table to be placed at the top of a page of text, if possible.
p Causes the table to appear on a separate “float” page reserved for it.
Several of these placement options can appear at once. For example,
\begin{table}[hb]
specifies that the table may appear at the current text position or at the
bottom of a text page (possibly the next page if there isn’t room for it on
the current page).
If you don’t specify a destination for the table, the document style de-
termines the placement. Both the report and article document styles use
a standard specification of “tbp” for tables. This is usually sufficient, but
can cause a problem when a table appears at the beginning of a chapter or
a document, since you don’t want it to appear before the chapter heading at
the top of the page. Table 6.1 was explicitly specified as a “[b]” table for
this reason.
Tables are automatically numbered (throughout the document in an article,
and within each chapter in a report). To place a caption with the table
number in it, you simply use the \caption control word. For example, the
caption on Table 6.1 was specified with
\caption{Figure/Table Destinations}
LATEX also provides a means for producing a List of Tables (and List of
Figures) at the beginning of the document, similar to the Table of Contents
facility. For example, you can place the control word \listoftables in your
document after the \tableofcontents command. \listoffigures works
similarly.
33
Sequence Description
\> Tab to next tab stop.
\< Tab backwards to previous tab stop.
\’ Right-justify current entry against previous tab stop.
\‘ Push following text to right margin.
\+ Set left margin right one tab stop.
\- Set left margin left one tab stop.
\= Set a tab stop at current position on a line
\kill Start a new line without outputting the current line (but keep any
tab stops that were set).
\pushtabs Save tab settings.
\poptabs Restore saved tab settings.
34
Year Simple Interest Annual Comp. Daily Comp. Continuous
0 5000.00 5000.00 5000.00 5000.00
1 5400.00 5400.00 5416.37 5416.44
2 5800.00 5832.00 5867.42 5867.55
3 6200.00 6298.56 6356.02 6356.26
\begin{tabbing}
\sl Year \= \sl Simple Interest \=
\sl Annual Comp. \= \sl Daily Comp. \= \sl Continuous\\
0 \> 5000.00 \> 5000.00 \> 5000.00 \> 5000.00\\
1 \> 5400.00 \> 5400.00 \> 5416.37 \> 5416.44\\
2 \> 5800.00 \> 5832.00 \> 5867.42 \> 5867.55\\
3 \> 6200.00 \> 6298.56 \> 6356.02 \> 6356.26
\end{tabbing}
The \> control sequence behaves much like the tab key on a typewriter.
The \’ sequence is similar, but right-justifies the current entry against the
previous tab stop (with a little extra space) and returns to the beginning of
the current column. In other words, \’ sort of “pushes” text to the left, as if
you had a key on the typewriter that caused output to go to the left without
moving the carriage.
Since there’s no tab stop at the right margin, the separate control symbol
\‘ pushes all the text that follows it up against the right margin in a similar
way.
The \= control symbol is much like the “tab set” key on a typewriter
keyboard, and sets a tab stop at the current line position. Frequently, you
will want to set up tab stops by typing the widest entries that will appear
in each column (and hitting the “tab set” control symbol \= after each one),
but won’t want to actually output a line with all those entries at once (and
certainly not as the first thing in the table). In this case, don’t end the line
with the usual \\, but use \kill instead; this retains the tab set-ups, but
throws away the current line without outputting anything.
Finally, \pushtabs and \poptabs are sort of like LATEX environments:
\pushtabs saves all the current tab stops; \poptabs restores the last set of
stops that were thus saved.
35
There are some more oddities concerning tab stops. Consider the follow-
ing input:
\begin{tabbing}
Short \= A bit Longer \= Negligible\\
A longer entry \> \> But not too long\\
Now something ugly \> But consistent
\end{tabbing}
The result of this is:
Why does this happen? Because what \> does is to go to the next nu-
merical tab stop, i.e. column #1, column #2, etc. This happens even if
we’ve gone past that tab stop, and LATEX will move backwards, if necessary,
to accomplish it. Thus, you don’t actually have to know whether or not a
long tabbed entry is going to slop over into the next column or not; you can
just count tab stops to get you to the right place.
cm Centimeters
em A “printer’s em,” the width of the current typeface. For example, it’s
twelve points most of the time in this document.
pc Pica (12pt)
36
To specify a dimension like three inches, you say “3in” with no space between
the number and the unit. You can use decimal fractions if you like, so
“2.732in” is a legal dimension, even if your particular printer doesn’t have
a resolution of one-thousandth of an inch.
Now that you know about dimensions, it’s easy to tell LATEX to skip a little
space: just say \hspace{dimen}, where dimen is the size of the horizontal
space you want to leave. So, to set up an output line with with four one-inch
columns, you might simply say:
\begin{tabbing}
\hspace{1in}\=\hspace{1in}\=\hspace{1in}\=\kill
One \> Two \> \> Four\\
\> \> Three\\
This \> All \> Lines \> Up\\
Into \> Neat \>\>Columns
\end{tabbing}
37
\begin{center}
\begin{tabular}{|l|l|}
\hline
\sl Letter & \sl Meaning\\
\hline
h & here \\
b & bottom \\
t & top \\
p & float page\\
\hline
\end{tabular}
\end{center}
The center environment is used in the usual way to center the table.
The tabular environment requires a second mandatory parameter which
describes how each line of the table will look. Here, we’ve specified that
each row will have a vertical line, a left-justified entry, another line, another
left-justified entry, and a final vertical line. Other options include c and r
for centered and right-justified entries. Also, you can have more than one
vertical bar at a time, causing multiple vertical lines to appear in the output
to separate the columns.
The \hline directive causes a horizontal line to be drawn the width of
the table.
Each line in the table must be explicitly ended with \\ and each entry
in the line is followed by the special LATEX tab character, the ampersand
(one of those special characters we talked about back in Section 2.2). Also
notice that each tabbed entry occurs in its own environment, so the switch
to slanted font in the header line must happen separately for each entry.
You can also set up a “paragraph” entry in a table by specifying a format
of p{dimen}, where dimen specifies the width of the paragraph. For example:
\begin{tabular}{|l|p{1.5in}|}
\hline
First Item: & This is a short paragraph\\
\hline
Second Item: & This is a somewhat longer paragraph\\
\hline
\end{tabular}
38
First Item: This is a short
paragraph
Second Item: This is a somewhat
longer paragraph
6.4 Figures
Of course, anything can go in a figure environment (or a table environment,
for that matter): program text, tabular tables, descriptions, quotations
. . . The LATEX documentation describes how to produce box-and-arrow-type
drawings using the picture environment, but that’s somewhat outside the
scope of this document. However, you should know how to produce some
extra vertical space so that a drawing can be pasted in later; simply put the
control sequence \vspace{dimen}, where dimen is a dimension like “1.5in”
(for one and a half inches of vertical space). Note that \vspace should occur
between paragraphs and that odd things will happen if you use it inside a
paragraph.
39
Chapter 7
Cross-References
This paper would have been particularly hard to write if it were necessary
to know about section numbering in advance, since it refers frequently to
things like “Table 6.1 on page 32.” LATEX allows one to assign labels to
various numbered entities (chapters, sections, tables and figures) and refer
to those labels elsewhere in the document. The important thing is that these
cross-references are made by name (referring to the label) rather than by
number, and obviate the need for the document composer to know about the
actual numbers.
As a specific example, Section 2.2 discusses input files. Shortly after the
\section control word that began that section, I included the line
\label{SIMPLE}
Note the use of the tie character (~) to prevent line breaking between the
word “Section” and the section number.
Labels also keep track of the current page, so I can mention that Sec-
tion 2.2 is on page 4 by saying
is on page~\pageref{SIMPLE} by saying
40
Cross-references always refer to the smallest division of the document
(section, sub-section, sub-paragraph) in which they occur, or to the figure or
table in which they are used. Warning: if you \label a figure or table, you
must not do so until after the \caption control word.
Although overuse of cross-references can detract from the readability of
a document, sometimes a large number of cross-references can accumulate in
a document, and the writer is responsible for keeping track of the names of
all the \labels. A block of comment lines can be used to keep the list of
labels and their significance on-line within the document. If things get out
of control unexpectedly, you can figure out what’s what by taking advantage
of the fact that all the labels and their values and page references are output
by LATEX into the .aux file. Scanning this file for all the lines containing the
word “newlabel” will produce a list of all the labels you’ve defined in the
document.
Cross-references can refer to labels that occur later in the document, but
the document must be run through LATEX twice. The first time through,
LATEX will complain about undefined labels when the corresponding \ref is
encountered, and if you print the resulting document, the reference is printed
as “??”.
41
Chapter 8
Equation Formatting
√
−b ± b2 − 4ac
x=
2a
Needless to say, this sort of thing is normally pretty hard to type so that it
looks good, even on a typewriter that has all the special symbols.
LATEX has three environments which constitute “mathematics mode.” In
mathematics mode, LATEX’s rules and control sequences are changed some-
what so as to facilitate the formatting of equations. These environments are
math (for formulas that appear in the text, like “x2 + y 2 = 0”), displaymath
(for formulas that are displayed separately from the text, like the quadratic
formula that introduced this section), and equation (which is just like dis-
play math, except that it adds an equation number at the right margin).
To make all this easier to type, LATEX lets you type \( and \) instead of
\begin{math} and \end{math}, and \[ and \] instead of \begin{displaymath}
and \end{displaymath}. Even easier, you can type a dollar sign instead of
\( and \), so you can type $x+y=0$ to produce “x + y = 0.”
The TEXbook contains no fewer than four complete chapters on setting
equations in mathematics mode, so it’s safe to conclude that it’s a pretty large
subject. However, this introductory paper isn’t concerned with the complete
details of mathematics mode, so we’ll only include a few outstanding rules.
42
8.1 Basic Rules of Math Mode
Mathematics mode differs from LATEX’s usual operation in a number of im-
portant ways.
1. All spaces are ignored. This includes new lines and tab characters.
LATEX is assumed to know all about spacing equations, and has a series
of control characters for inserting spaces “manually”, like “\ ” (that’s
a control symbol consisting of a backslash followed by a space).
2. Alphabetic characters are in a special font, math italic. Math italic is
a lot like text italic, but the character spacing is slightly different. You
can switch to roman (or other) fonts via \rm and the like, but spaces
are still ignored between the words.
3. Many control sequences, like the ones that produce Greek letters (α–ω),
only work in mathematics mode and produce errors at other times.
4. You can’t start a new paragraph inside a formula, so don’t leave a blank
line in any math environment. If you do, LATEX will think that you’re
trying to leave mathematics mode, and an error will result.
43
8.3 Mathematical Symbols
LATEX has a dazzling array of mathematical symbols, all of which are only
available in math mode. To produce Greek letters like “ω” (omega) you type
$\omega$. The capital omega (Ω) is pronounced $\Omega$. You can also
get boldface versions of these characters. Some of the other mathematical
glyphs that are recognizable by non-mathematicians are \times (seen in
“5 × 5 = 31eight ”), \infty (∞) and the popular \bullet (•). A complete list
of symbols can be found in Appendix F of The TEXbook on pages 434–439. A
quick perusal of the complete list will show that many characters are named
according to their mathematical significance, so that mathematical input is
“pronounced” in a manner similar to the terminology of mathematics.
produces
lim xn = 0
n→∞
with the subscript appearing below the word, which is the usual mathemat-
ical appearance of “lim”.
8.5 Fractions
While you can say things like $x/y$ most of the time to produce “x/y”, some-
times you need to do things like “ 21 ”. For this, the control sequence \frac is
available. It takes two parameters: the numerator and the denominator. For
example, “ 12 ” can be typed as “$\frac{1}{2}$” and the quadratic formula
which began this chapter can be typed as
44
\[ x = \frac{-b \pm \sqrt{b^{2}-4ac} }
{2a} \]
Since it’s possible to produce fairly ugly output using \frac, it is recom-
mended that you consider carefully whether or not a simple slash will do.
This example also demonstrated the \sqrt control word. You can also get
other roots√by supplying an optional parameter. For example $\sqrt[3]{x^{3}}$
3
produces x3 .
\[ \sum_{n=1}^{m} n = 5 \]
produces:
m
X
n=5
n=1
\begin{equation}
\int_{-1}^{-1}x\,dx
\end{equation}
produces: Z −1
x dx (8.1)
−1
The \, control sequence produces a “thin space.” LATEX actually has four
different math-mode spacing control characters:
Sequence Name Example
\; Thick space xx
\: Medium space xx
\, Thin space xx
(no space) xx
\! Negative space xx
45
Negative space is rarely used.
produces:
â ≡ ñ + ̄
There are ten different accent marks that can be placed above mathematical
symbols, and the control words used for them are different from the control
symbols used for accenting normal text characters. See page 135 of The
TEXbook for a complete list.
produces:
eiθ = cos θ + i sin θ
produces:
xmax − xmin
\[ \sqrt{1+\sqrt{1+\sqrt{1+x}}} \]
produces: r q √
1+ 1+ 1+x
46
produces:
x+y
z+2
Various kinds of brackets, parentheses, and the like can be used with \left
and \right. These produce brackets big enough to surround whatever is
between them. However, for every \left, there must be a \right, although
they need not be the same type of bracket.
\[ \left\langle
\frac {2(x+y)}
{|z|}
\right\rangle
\]
produces: * +
2(x + y)
|z|
\[ \left |
\begin{array}{ccc}
42 & 39 & 11 \\
23 & 5 & 9 \\
7 & 17 & 27
\end{array}
\right | = -18602
\]
produces:
42 39 11
23 5 9 = −18602
7 17 27
The array environment is a math-mode version of the tabular environment.
\[ \delta_{ij} = \left\{
\begin{array}{ll}
y & {\rm if\ } y>0\\
z+y & {\rm otherwise}
\end{array}
\right. \]
47
produces: (
y if y > 0
δij =
z + y otherwise
The \right. closes the \left\{ without printing anything. Since spaces
are ignored in mathematics mode, the control character \ (i.e., a backslash
followed by a space) is necessary to insert a space after “if”. You aren’t
necessarily expected to understand all this.
48
Chapter 9
LATEX Errors
LATEX error messages (and, for that matter, its purely informative mes-
sages) tend to be verbose, and can overwhelm the victim with spurious infor-
mation. This is largely due to the way that LATEX is layered on top of another
system, TEX, which has its own error messages. When LATEX encounters an
error, what often “really” happens is that TEX perceives this as an error in
the LATEX software itself, and displays a lot of information relating to the
internal structure of that software. Most of the time, you can ignore the
large majority of what gets typed out when there’s an error.
49
and leave your cursor after the question mark. The error message in this
case is straightforward. The notation “l.3” is very important, since it tells
you which line of the file (line 3) the error is on. The message also shows you
how much of the input line has been read by the time the error was noticed.
In this case, the control word \invalid was the last thing that was read,
and this caused the error.
If you type a question mark, you get a “menu” of options:
Type <return> to proceed, S to scroll future error messages,
R to run without stopping, Q to run quietly,
I to insert something, E to edit your file,
1 or ... or 9 to ignore the next 1 to 9 tokens of input,
H for help, X to quit.
?
If you type “H” you get an explanation of the error message. Sometimes it’s
helpful, not at other times. In this case, the message is:
The control sequence at the end of the top line
of your error message was never \def’ed. If you have
misspelled it (e.g., ‘\hobx’), type ‘I’ and the correct
spelling (e.g., ‘I\hbox’). Otherwise just continue,
and I’ll forget about whatever was undefined.
In other words, you can insert a substitute control word using the “I” option,
or just hit a Return and ignore the bogus control sequence. This sort of error
is very common, and fortunately easy to correct.
50
In this case, LATEX produces the following verbose display:
LaTeX error. See LaTeX manual for explanation.
Type H <return> for immediate help.
! Environment quoth undefined.
\@latexerr ...for immediate help.}\errmessage {#1}
\endgroup
l.3 \begin{quoth}
Here, the error originates from LATEX and not TEX. The error message is still
comprehensible: there’s no such thing as a quoth environment. However, the
display with @latexerr and all that stuff is not very helpful and should be
ignored.
On the other hand, the input line that triggered the error (once again,
line 3) is displayed. If we type “H” in this case, we see:
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
?
51
9.3.1 No Document Environment
If you try to run LATEX on a file containing the single line,
In this case, LATEX decided something was wrong as soon as it read the very
first character of the file. At this point, discretion is the better part of valor,
and you should ‘X’ out of LATEX. If you do continue, LATEX will try to read
additional input from the terminal once it finishes reading the file, and your
only escape will be to hit your interrupt key.
52
This is completely mysterious to the untrained eye, since it refers to a par-
ticular internal control variable that LATEX is trying to use to set up the
document environment. Alas, this variable hasn’t been defined by a docu-
ment style description file. Complete chaos. Once again, it’s best to ‘X’ out
of a situation like this. Attempts to continue will just produce additional
cryptic error reports.
\documentstyle{nosuchthing}
In this case, you’ll get a message from TEX explaining that it can’t find the
file you asked for. TEX then asks for the name of a different file to read. For
example,
! I can’t find file ‘nosuchthing.sty’.
<to be read again>
\relax
\@documentstyle ...ionfiles {}\input #2.sty\relax
\let \@elt \input \@option...
l.1 \documentstyle{nosuchthing}
In this example, you can recover by noticing that LATEX was looking for the
file named ‘nosuchthing.sty.’ Logically, then, you can make it use the
‘report’ document style by typing the file name ‘report.sty’, as we did
in this case (in response to “Please type another input file name:”).
Sure enough, things proceed from there in a normal way.
53
This line is over $5.00 in price and also refers
to the variable foo_bar which will cause a problem.
When LATEX reads the first dollar sign, it will switch to mathematics mode.
When it comes to the end of the paragraph, or of the document, it will
conclude that a closing dollar sign was somehow left out, and print an error
message like:
! Missing $ inserted.
<inserted text>
$
<to be read again>
\par
inpriceandalsoreferstothevariablefoo
as a single variable. Quite a mess. Suppose, now that we ‘correct’ the file
by putting a back-slash in front of the dollar sign. Everything goes fine until
the underscore is encountered, at which point LATEX thinks that math mode
is intended, and says,
! Missing $ inserted.
<inserted text>
$
<to be read again>
_
l.4 to the variable foo_
bar which will cause a problem.
? h
I’ve inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
54
9.4 Overfull Hboxes
It is impossible to completely cover the techniques of LATEX error recovery
in an introductory document, but there are a couple of very common errors
that sound terrible yet are in reality fairly benign.
The first of these is “Overfull \hbox (xx pt too wide)” This is fol-
lowed by a display of an output line that shows the font and the possible
hyphenation points. This is just a warning, and can be fairly harmless.
What it means is simply that LATEX couldn’t find a good place to break a
line without producing an unacceptable amount of space between the words.
So it gave up, and set the line with some amount of text sticking into the
right margin. The exact amount of text sticking into the margin is displayed
in printer’s points (72.27 of them to the inch, if you recall).
Since this is a warning, you don’t get a chance to do anything about it
while LATEX is running. If the amount of right-margin slop is unacceptable,
you can either change the prose in the paragraph in which the line appears, or
place the entire paragraph (including the blank line that ends the paragraph)
inside an environment called sloppypar; this will allow LATEX to set lines with
somewhat unaesthetic amounts of space between words. In any case, you’ll
have to run the document again to get it right.
If you see that LATEX has failed to notice a good hyphenation spot near
the end of the line, you can salvage the situation by telling LATEX about
how to hyphenate that word. For example, LATEX doesn’t know how to
hyphenate the word “hello”. You can add the following command before the
\begin{document}:
\hyphenation{hel-lo}
which will tell LATEX how to do it. This may enable it to reset the offending
line in an acceptable way on the next run. You can also insert “discretionary
hyphens” in a word to indicate a possible hyphenation point on a one-time-
only basis. For example,
55
9.5 Underfull Vboxes
Almost as often, the message, “Underfull \vbox (badness ...” appears.
This just means that LATEX can’t find a really good place to break the cur-
rent page, so it’s “stretching” the page more than it would like (producing
extra white space in the page). Once again, the results may or may not be
acceptable; you can always insert \newpage to force a page break.
In summary, while LATEX errors may sometimes be cryptic or even fright-
ening, they can usually be deciphered by the patient user. Since the line
number of the input which caused the error is printed, one can always deter-
mine where the error is, even if it isn’t immediately obvious what the error
might be. There’s no substitute for experience, though, so don’t be intimi-
dated by error or warning messages. Producing high-quality documents with
LATEX is, errors notwithstanding, a highly rewarding experience.
56