Introduction to LaTeX: 3. Miscellany
Tables
While generating tables in Plain TeX (and AmSTeX) is difficult
and can be a very frustrating experience, this is not the case with
LaTeX. LaTeX has simple, intuitive, and user-friendly macros for
tables, and
typesetting good looking tables in LaTeX is a breeze, and can be fun.
The idea is to think of the table as a matrix, consisting of rows and
columns. The data is entered one row at a time, with double
backslashes (\\) separating the rows, and ampersands (&)
separating the entries within each row. A surrounding pair
\begin{tabular}{...}
... \end{tabular}
then does all the work.
Here the argument of the first brace {...} consists of a "template"
that specifies the positioning of the table entries: l, r, or c, for
left, right, or center.
Here is a very simple example:
\begin{tabular}{l r r r c}
Name&Exam1&Exam2&Exam3&Grade\\
John&19& 28&33&C \\
Jane&49& 35&60&B \\
Jim&76& 38&59&A
\end{tabular}
Note that TeX
automatically chooses the width of the columns and the heights of the
rows so that all table entries fit, with some room to spare.
Now let's spruce up this table,
by surrounding it with a frame,
and separating columns and rows by "rules" (vertical and horizontal
lines).
This is easy to do:
To get horizontal lines
add "\hline" at each linebreak and at the beginning and end of the
table.
To get vertical lines (and also specify the positioning of the
data entries - left, center, or right), add vertical bar characters
("|") to the template, e.g.: { | l | r | r | r | r }
If you use a double bar symbol "||" instead of a single bar,
or use a double "\hline", the separating lines get "doubled".
This provides an easy way to separate the headers from the table
contents.
Here is the above table with these enhancements.
\begin{tabular}{| l || r | r | r | c |}
\hline
Name&Exam1&Exam2&Exam3&Grade\\
\hline\hline
John&19& 28&33&C \\
\hline
Jane&49& 35&60&B \\
\hline
Jim&76& 38&59&A \\
\hline
\end{tabular}
Exercise 3.1: Build a table like the one above (or paste the
above code into a LaTeX article template), compile the TeX file,
and look at the typeset table.
There are two additional enhancements you might want to make with
the above table. The first is to center it, by surrounding the entire
construct with a \begin{center} ... \end{center} pair.
The second enhancement is to surround everything by a \begin{table} ...
\end{table} pair. This makes the table a
"floating" table, meaning that TeX chooses an appropriate placement,
so as to avoid bad page breaks. For example, if at the current
location the table would run over the bottom of the page, TeX will
fill the page with text, and
place the table at the top of the following page.
The "table" environment also enables one to add a "caption".
The result is something like the following:
\begin{table}
\begin{center}
\begin{tabular}{ .....}
......
\end{tabular}
\caption{Math 361 Grades}
\end{center}
\end{table}
Exercise 3.2: Enhance your table in this manner, by centering
it and adding a caption. Note that LaTeX automatically gives the table
a label of the form "Table 1: ....". If you had a second table, it
would be called "Table 2: ...". Try to typeset several (possibly identical)
tables, with a few paragraphs of text in between, and see how TeX
chooses the placement of the tables. (Note: The positioning
doesn't always come out where you'd expect or want it. See the Gratzer
book for ways to get around such problems.)
Mathematical material in tables:
If the table entries involve mathematical notation, they should be
placed inside single dollar signs: For example, x2 has be
entered as $x^2$.
Lists
LaTeX has two basic list environments: The "itemize" environment which
produces a "bullet list", and the "enumerate" environment which
generates a numbered list. These enviroments are similar to the
<ul> and <ol> environments in html:
The list items are introduced with the command "\item", which is
analogous to the html code <li> . Here is an
example of the first type of list:
\smallskip
\textbf{Some special characters in TeX:}
\begin{itemize}
\item Accents
\item Braces
\item Dollar signs
\end{itemize}
Replacing "itemize" by "enumerate" will create a numbered list
(without any further changes - the numbers are generated
automatically!).
Lists can be nested. In numbered lists (i.e., those generated by
"enumerate"), the numbering style changes from one level to the next.
The items at the top level are numbered 1., 2., etc., those at the next
level are numbered alphabetically by (a), (b), etc.
Note that the numbers are generated automatically - you don't have
to enter them!
Exercise 3.3:
Typeset the following set of exercises using correct TeX constructs:
1. Differentiate the following functions:
(a) f(x) = x2
(b) f(x) = log x
2. Integrate the following functions:
(a) f(x) = square root of x
(b) f(x) = sin x cos x
Fonts and spacing commands
The most important rule about font changing and spacing commands is
to avoid using them!
You should normally leave font and spacing
matters up to TeX. If you don't like the way a document looks, try
another documentclass (e.g., article instead of amsart), add an
option to the documentclass, or add a global instruction to the
preamble of the document, before \begin{document}.
Options are specified in square brackets
before the name of the class (which is enclosed in braces).
Here are few such changes you might want to make:
- Changing page dimensions:
To change the width and height of the printed text, add
instructions such as the following to the preamble (i.e., after
\documentclass and \usepackage, but before \begin{document}:
\setlength{\textwidth}{5in} \setlength{\textheight}{7in}.
Here "in" stands for inches, and is one of several units that TeX
knows about. Other units include cm (centimeters), mm (millimeters),
and pt (points - see below).
- Changing the font size:
The default font size for ordinary text is 10pt;
this means 10 "points", where a point equals 1/72 of an inch. To
get a larger font size, e.g., 12pt, in the "amsart" documentclass,
use \documentclass[12pt]{amsart}. The same works for most document
classes, and with 11pt instead of 12pt. (You could specify 10pt, but
that's the default anyway so it will have no effect.)
Note: The Plain TeX "\magnification" command does not work under
LaTeX. To magnify a document,
change the font size and the page dimensions as indicated above.
- Equation numbers on left (or right): In the amsart
document class, equation numbers appear by default on the left, whereas
in the article and book classes, they appear on the right. To change
the positioning of equation numbers, add an option "leqno" or "reqno"
to the documentclass, e.g.: \documentclass[reqno,11pt]{amsart}.
(This example also illustrates how to specify several options,
by separating
them with commas.)
- Paragraph indentations: By default (in most document
classes), all paragraphs except for the first paragraph in a section
are indented. To disable the indentation, set the "parindent" parameter
to zero, by adding this to the preamble:
\setlength{\parindent}{0in}
To add additional spacing between paragraphs, set the "parskip"
parameter to an appropriate value. For example:
\setlength{\parskip}{10pt}
Explicit font and spacing instructions: As mentioned,
it is generally a bad idea to "hand-code" font and spacing
instructions. (In fact, I have a program that counts such hand-coded
items in a TeX file and assigns a "badness" score depending on how many
there are, with zero being a perfect score.)
That said, there are
situations where manually coded items are appropriate, so here is a
quick rundown on the most important of these:
- Font changes: \textbf{....} puts the material enclosed in
braces in boldface; \textit{...} puts it in italic font.
Note that, as the name indicates, these commands are for text mode
only. To change the font in math mode (something that is rarely
necessary), use the analogous commands \mathbf{...}, \mathit{...}.
- Changing font size:
To change the size of a font manually, use a command such as
\large (one step up from normal), \Larger (two steps up from normal
size), \normalsize (normal size), or
\small (one step down from normal size). These commands stay in
effect until the next size changing command. The \normalsize command
returns the font size back to normal. Again, this works for text
mode only. For math mode there are similar commands; of these, there is
only one, "\displaystyle", that you might want to use on occasion.
"\displaystyle" has the effect of enlarging a font that would otherwise
be too small to be legible. The latter might occur in stacked
subscripts, superscripts, fractions, or limits to sums or integrals.
For example, in the expression $\frac{e^{x^2+1}}{x}$ (set "inline")
the exponent $2$ is likely to be too small to be legible. To remedy
this, and increase the size of the expression by one notch, precede
"\frac" by "\displaystyle" (within the $ ... $ pair).
Exercise 3.4 Type a few sentences containing the above
expression, (a) "inline" without \displaystyle, (b) inline, but with
"\displaystyle", (c) as a displayed formula, enclosed by a bracket
pair (without \displaystyle), and (d) inline, but with the fraction
written in slash notation (i.e., $e^{x^2+1}/x$). (The latter is
probably the best of the three inline versions; even better would be
to use "exp" in place of "e" ( $(1/x)\exp\left{x^2+1
\right}$), since then only one level of super/sub-scripting is
involved.) Observe how TeX sizes
the various components in this expression, depending on whether the
formula is inline, or displayed, and whether or not it contains the
\displaystyle instruction.
- Indentations: Paragraphs are normally indented,
except for the first paragraph in a section. To prevent this
indentation, use \noindent right before the paragraph.
The opposite command, \indent, causes TeX to indent material that
would normally not be indented,
These commands
should be used very sparingly; if you want to disable indentation
completely, set the "parindent" parameter to zero inches, as shown
above.
A common mistake is to leave blank lines before or after
displays. Blank lines are interpreted by TeX as paragraph breaks, so
the text following such a blank line is being indented.
I have seen many papers where authors make the mistake of surrounding
every display by blank lines, then realize that this causes unwanted
indentations, and try to undo the damage by adding \noindent's
every time. Here is an extreme
example of this,
a small excerpt from a paper whose author shall remain anonymous.
The example is instructive, because it shows many other bad habits.
As a result, the author probably spent twice as much time typing the
document than he would have needed otherwise, and the technical editor
(which, in this case, was me) had to waste hours to undo the damage.
- Line and page breaks: To force a line break at a particular
spot, say \newline; similarly, to force a page break, use \newpage.
- Vertical spacing:
You can specify an arbitrary amount of vertical space by the
\vspace{...} command, e.g., \vspace{1.5in} generates 1.5 inches of
space. However,
it is usually better to just use one of the following
three standard commands for creating a small amount of vertical space:
\smallskip, \medskip, \bigskip.
\smallskip is appropriate, for
instance, before and after a "manually" coded theorem. (If you use the
\begin{theorem}... \end{theorem} construct (see below), the spacing
will be generated automatically.)
- Horizontal spacing:
To explicitly specify a horizontal space in text mode, you can use
\hspace{...}, with ... being the amount of space needed. Creating
horizontal space in this manner is very rarely appropriate and not
recommended.
In math mode, however, there are situations where you
want to add a small amount of horizontal space, e.g., to separate two
equations on a single line. In most of these cases, specifying "\quad"
gives you the right amount of space. Other spacing commands
in math mode include "\," (a tiny amount of space), "\!" (a tiny amount
of "negative" space), "\qquad" (twice the amount of space generated by
a \quad).
Error messages
As you begin doing more complex things, it is more likely that you
will encounter error messages while compiling the file.
Often, these error messages are cryptic, and it may be hard
to figure out what is wrong. Here are some typical situations:
- Undefined control sequence: This (usually) means what it
says: TeX encountered a command that it didn't know about.
The offending command is shown in the error message (look for items
beginning with a backslash). Often, this
is due to a simple typo. Sometimes, it's a command that is defined in
one of the AMS extension packages, some of which have to be explicitly
loaded. This problem can be prevented by adding the
line
\usepackage{amsmath, amsthm, amssymb,amsfonts}
after \documentclass{...}, to load the main ams
packages. Depending on the documentclass, some of these packages may
get loaded automatically, but it doesn't hurt to load them anyway.
The impact on performance is minimal.
- Overfull/underfull
vboxes/hboxes: These are warning messages that indicate
that TeX wasn't able to fill lines or pages to its rather strict
specifications. If you look at the output, you may see lines sticking
out to the right. For drafts, simply ignore these messages, especially
if the excess is small. If a line is significantly overlong, which
occurs mainly in displayed equations, break the display into several
lines using the "align" or "align*" environment.
- Missing $ inserted: This is usually an indication that
TeX found material that needs to be set in math mode.
For example, the expression "x^2" should be enclosed in dollar signs.
It could also be a sign that a math mode instruction didn't
have a corresponding "end math mode" instruction.
- Extra }, or forgotten $: An indication that there is a
mismatch of opening and closing braces, or opening and closing math
mode instructions. Often the result of a forgotten brace in
complex fractions (an example being
\frac{e^{x^2+2}+2}{\log(x^2+2)+e^{x^{2n}} - can you spot the missing
brace?). Those types of errors are hard to diagnose.
The best strategy in most cases is to note the line number displayed
in the error message, exit the compile phase by typing "x",
and look at the document at or near this line
number, in the hopes of spotting the error. If you don't succeed,
ask someone. Some errors can be extremely frustrating and hard to
locate.
Odds and ends
Here is a partial list of topics that we haven't covered here. Learn
these as needed, using Gratzer's book as a reference.
How not to typeset LaTeX
Here are some examples of awfully bad TeX typing; they are more common
than you think.
All cases involve extensive use of hand-coded formatting.
The TeX files compile okay, but the output doesn't look
good, and fixing up these papers (so as to meet
publisher's specifications) is an arduous and time-consuming task.
Exercise 3.5:
Take a look at one of the above source files, and try to spots errors
and bad habits. What would you do to fix up the file?
Resources
Online resources
Books
One could certainly learn TeX/LaTeX entirely from online materials,
but anyone serious about learning TeX should buy a book.
It is a worthwhile investment that will likely be useful for a long
time. Unlike books about commercial software which become obsolete
as soon as a new version comes out (who would want to buy a book on
Windows 95 today?), TeX is a stable system, and books on TeX
have a long half-life. There are dozens of books on TeX, but there is
only one you really need: Gratzer's "Math into LaTeX".
Most
of the books on the market are either inappropriate for beginners,
or do not adequately cover mathematical typesetting.
- A must-have: George Gratzer, "Math into LaTeX".
This is the standard reference on LaTeX with the AMS flavor, and a must
(IMHO) for anyone planning to write mathematical papers in LaTeX.
None of the other beginner-level books on the market
covers the AMS packages that
are essential for any serious mathematical typesetting
(amsmath, amsthm, amsfonts, etc.) in any detail. Even if you have
already another introductory book, it's worth buying this one.
The first chapter serves as a good stand-alone introduction to LaTeX;
this chapter is also available online at
http://www.ctan.org/tex-archive/info/mil/mil.pdf.
- A bare bones choice: George Gratzer, "First steps in LaTeX".
A slimmer version of the above book - about 100 pages versus 450 pages
for the full version,
Good as an introductory text or for a crash course, but you will likely need
to buy a more comprehensive text or reference, once you have outgrown
this book. If you have the other book (Math into LaTeX), you won't
need this one.
For most people, the Gratzer book is all they need. For those past
the beginner level who really want to delve deeper into LaTeX, here
are two additional recommendations, both at the intermediate-advanced
level:
- H. Kopka, P. Daly, "A guide to LaTeX":
Probably the best and most comprehensive reference on LaTeX proper
(as opposed to add-on packages which are discussed in the next item).
- M.
Goossens, F. Mittelbach, A. Samarin, "The LaTeX Companion"; Goossens,
S. Rahtz, F. Mittelbach, "The LaTeX Graphics Companion":
These two books cover many of the "packages" and add-ons
available for LaTeX and are of interest for those wanting to use some
of these packages.
How to get the TeX software/program
If you do all your TeX work on the Math Department's
Unix system (which is what I do - using ssh if I am at my home
computer - and what I would strongly recommend),
you don't have to worry about this. The TeX system we have is
reasonably current, actively maintained, and probably more
comprehensive than any system you might install on your own.
If you use the departmental TeX installation, you can get help in case
of problems through the help and texhelp email addresses, but
if you work on your home computer, you are on your own.
Another advantage to doing all your work on your Unix account is that
your files will all be in one place, and you don't have to transfer
files back and forth from your home machine to your Unix account.
If you really still want to have a local copy of the TeX software,
here are the recommended options, depending on the OS you are
using.
- Linux: In that case there is nothing to install, since all
major Linux distributions include a full TeX installation. The Linux
versions are very similar to that on our departmental Unix network, so
it should be easy to find your way around, and to switch from working
on a local Linux machine to working on the departmental network.
- Windows:
MikTeX, is a free and well
supported TeX distribution for Windows,
arguably better than any commercially available system (for which
you'd have to shell out a few hundred dollars).
- MacOS: OzTeX
is a good shareware system.
Back to Table of Contents
Last modified: Fri 24 Aug 2007 12:33:42 PM CDT
ajh@uiuc.edu