LaTeX Tips: Figures and tables
Figures in LaTeX: An example
Here is an example that illustrates how to incorporate figures
into LaTeX. It assumes that you have eps and pdf files,
"sineplot.eps" and "sineplot.pdf", of the picture that
you want to incorporate
into your tex document, and placed these files into the same directory as
your tex source file. Picture files are usually created with an
external application, such as Mathematica or xfig (a Unix drawing
program); how this is done depends on the particular application and
cannot be discussed here.
\documentclass{amsart}
\usepackage{graphicx}
[...]
\begin{document}
[...]
\begin{figure}
\centering
\includegraphics{sineplot}
\caption{Plot of the function $\sin x$}
\label{sineplot}
\end{figure}
Figure \ref{sineplot} shows a plot of the sine function.
[...]
\end{document}
The main ingredients in this example are the following:
-
The "graphicx" package.
This is the standard mechanism of incorporating externally produced
graphics into a tex document.
(Note that the package name "graphicx" ends with an "x", but the
"\includegraphics" command ends with an "s".)
-
The "\includegraphics" command. This command, provided by the
graphicx package, does the work of importing a graphics file into a tex
document. Its argument is the name of the graphics file one wants to
include. The extension of the filename is optional, and it is best to
leave it out; i.e., use "sineplot" rather than "sineplot.eps" or
"sineplot.pdf". (See below for more on this.)
-
The "\centering" command:
This ensures that the figure is centered horizontally, much like a
displayed equation.
-
The "figure" environment:
This takes care of the placement of the figure,
and the automatic numbering of the figures.
-
The "\caption" command:
This command generates a caption to the figure, along with a label
of the form "Figure 1". In the above example, the caption
generated would be "Figure 1: Plot of the function $\sin x$".
-
The "\label" command.
This allows one to refer back to the figure in the usual way, with
\ref{...}, as illustrated in the last sentence in above
example.
Figures in LaTeX: Additional tips
- Placement of \usepackage{graphicx}:
The graphicx package may interfere with other packages or override the
behavior of other packages. To minimize such problems, it is
best to load this package before any other packages are loaded;
i.e., the "\usepackage{graphicx}" instruction should come right after
the "\documentclass{...}".
-
Graphics file types:
In order to create dvi or ps output of the TeX document (via latex/dvips),
LaTeX needs an eps version of the graphics file, but to
create pdf output (via pdflatex), it needs a pdf version.
By not including either of the two file extensions, LaTeX picks whichever
file it needs (assuming both are available),
and one can use the same source file to generate both
dvi/ps and pdf versions of the document.
This is the reason why it is best to specify the graphics file
only in terms of its root name (e.g., "sineplot"), rather than
with the extension (e.g., "sineplot.eps").
-
Scaling:
Figures can be scaled using the "[width=...]" option of the
\includegraphics command. For example:
\includegraphics[width=0.8\textwidth]{sineplot}
While one could provide absolute dimensions (e.g., "width=3in"),
it is preferable to specify the width in terms of the "\textwidth"
parameter; that way, the size of the figure is proportional to the
size of the surrounding text.
-
Numbering of figures:
The numbering of figures works in much the same way as the numbering of
theorems or equations. By default, figures are numbered consecutively, in a single
sequence: Figure 1, Figure 2, etc. In longer documents it may be
better to number figures by section, e.g.: Figure 1.1, Figure 1.2, etc.
This can be achieved in the same way as for equations
with the "\numberwithin" command:
\numberwithin{figure}{section}
This command requires the amsmath package, so make sure to load this
package with "\usepackage{amsmath}" or use documentclass "amsart" or
"amsbook".
-
Figures without captions:
The "\caption" and "\label" commands are optional. Without these
commands, the figures will appear without caption. This may be
desirable, for example, in a document with only one figure whose
meaning is clear from the context.
-
Using graphicx under the draft mode.
When the "draft" option in the "\documentclass" command
is turned on, the graphics files are not displayed;
only an
outline of the rectangular boxes containing the graphics is shown.
This is useful in order to determine the proper sizing
and placement of the graphics. To override this behavior,
use the option "final" when loading the package:
\usepackage[final]{graphicx}
With this option, all graphics will be displayed, regardless of
whether or not the draft option is turned on.
-
Documentation of the graphicx package.
The main source of documentation is the
document "Using imported graphics in LaTeX2e", which, despite its
hefty size (86 pages), provides an excellent introduction to the
"graphicx" package, and
to the issues involved in using graphical material in TeX files.
The name of this document is "epslatex", and you can usually access it on your
local installation with the command "texdoc epslatex".
The "graphicx" package is also documented in the
"LaTeX Graphics Companion".
Tables in LaTeX: An example
Here is a simple example that illustrates the basic structure of a table
in LaTeX.
\begin{table}
\centering
\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}
\caption{Math 500 Grades}
\label{math500grades}
\end{table}
The main ingredients in this example are the following:
-
The "tabular" environment. This is the environment that creates
the table. Here are the main features of this environment:
- Argument to "\begin{tabular}": This argument
("|l||r|r|r|c|" in the above example) is mandatory and
represents a template of a table row.
Here l/r/c refers to the
alignment (left/right/center), a bar (|) indicates a vertical
line, a double bar (||) a double vertical line.
The bars at the beginning and the end of the template
create the vertical parts of the "frame" surrounding the table.
- Table rows:
Rows are separated by a double backslash (\\).
- Table entries:
Within each row, table entries are separated by ampersand symbols.
Blank spaces before and after the ampersand symbol are ignored.
A common source of errors is a mismatch between the number of ampersands
and the number of alignment symbols in the template (see above): Each
alignment symbol represents a table entry, and since an ampersand
separates two adjacent table entries, the number of ampersands must be
one less than the number of alignment symbols.
- Horizontal lines:
The command "\hline" creates
a horizontal line extending through the width of
the table; "\hline\hline" creates a double horizontal line.
Note that these commands should be placed right after the double slash
line separator, or the \begin{tabular}{...} command.
The first and last \hline commands above
create the horizontal parts of the "frame" surrounding the table.
-
The "table" environment:
This is analogous to the "figure" environment. It takes care of the
placement of the table, and the automatic numbering.
-
The "\centering" command:
This ensures that the table is centered horizontally.
-
The "\caption" commands:
This generates the table caption.
-
The "\label" commands:
This creates a label that allows back references to the table.
Tables in LaTeX: Additional tips
-
Adjusting the vertical spacing:
The default spacing between rows is rather tight, but it
can be increased
with the "\arraystretch" parameter: For example, the command
\renewcommand{\arraystretch}{1.5}
placed right after "\begin{table}" increases the default vertical
spacing by a factor 1.5.
-
Adjusting the intercolumn spacing:
The spacing between table columns can be changed with the "\tabcolsep"
parameter. Rather than specifying an absolute value for
this parameter (e.g., "5pt"), it is best to increase or decrease
the current value with the "\addtolength" command. For example,
\addtolength{\tabcolsep}{-2pt}
placed after "\begin{table}",
reduces the column spacing by an amount of 2pt. If a table is a bit too
wide for the page, a small adustment like this may be enough to make the
table fit on the page.
-
Reducing the size of a table.
Another way to make an overwide table fit a page is to
shrink the entire table using the sizing commands "\small", etc. For
example:
\begin{table}
\small
[...]
\end{table}
reduces the size of the table by one notch. The available sizing commands,
in decreasing order, are "\normalsize" (the default), "\small",
"\footnotesize", "\scriptsize", "\tiny". However, I would not recommend
using anything smaller than "\footnotesize".
Placement of figures and tables
The placement of figures and tables can be a source of much frustration;
sometimes, no matter what one does, TeX doesn't seem to want to place a
figure or table in a particular location. The reason for this is almost
always that there is no "legal" way (e.g., in the sense of not
overrunning the page) for TeX to produce the desired placement.
Back to the LaTeX Tips Page
Last modified: Tue 09 Oct 2007 10:51:56 AM CDT
A.J. Hildebrand