# 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}

\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.
• The "figure" and "table" environments: Contrary to what one might expect, these environments do not generate the figures or tables; in fact, they are not necessary to produce figures or tables and can be left out (though this has serious drawbacks - see below). What these environments do is (i) place the enclosed objects (figures or tables) at an appropriate location in the document chosen according to the "floating principle" (see below); (ii) take care of the numbering and labeling of the figures and tables and allow backreferences with the \ref{...} command; (iii) produce a caption through the \caption command. As far as TeX is concerned, the "figure" and "table" environments are equivalent; the only visible difference is in the printed caption: a "table" caption is of the form "Table 1: ...", while a "figure" caption is of the form "Figure 1: ...".
• The "floating" principle: The "figure" and "table" environments ensure that the enclosed objects (i.e., figures or tables) are treated as "floating" objects, in the sense that they are (usually) not typeset at the location specified in the source file, but rather at a a convenient place on the same page, or on a following page. This is usually the desired behavior since figures and tables represent unbreakable "chunks" and placing them exactly where they are specified in the source file risks creating overfull or underful vboxes. Moreover, a common convention in typesetting is to have figures and tables appear only at the top or bottom of a page. The default behavior of TeX is to follow this convention and seek the most convenient spot at the top or bottom of the current or a subsequent page for the placement of a figure. For a document with many figures, however, this may be unnecessarily restrictive and result in poor placement of figures (e.g., in the midst of a bibliography). See the next item for ways to fix such problems.
• Modifying the default float placement: The placement algorithm of TeX can be modified with an optional argument to the figure environment that indicates positioning preferences. The following is an example; this is what I usually use when I run into problems with the default settings. (The same can be used with the "table" environment.)
\begin{figure}[htb!]
[...]

\end{figure}

Here "h" stands for "here", "t" for the top of the page, "b" for the bottom of the page, and the preferences are evaluated in the order given (i.e., "here" first, followed by "top" and "bottom"). The exclamation mark is optional, but it encourages TeX to try harder, even if it requires overriding some default settings.
• Setting figures and tables without the "figure" or "table" environments. The only guaranteed way to force objects likes figures and tables to appear at a particular spot is to leave out the "figure" or "table" environments entirely; i.e., simply use a tabular environment or "\includegraphics{...}", surrounded by "\begin{center}", "\end{center}", and (possibly) some vertical spacing. These environments are not needed to typeset a figure or a table (the "\includegraphics" command and "tabular" environments do this), and by leaving them out one forces the objects to appear in the output at exactly the spot where they appear in the source file. This, however, should only be used as a last resort, as it has some serious drawbacks: First, such a forced placement may cause overfull or underfull vboxes. Second, the spacing before and after the object is likely not optimal and may need to be adjusted manually. Finally, one cannot use the captioning and numbering mechanism provided by the figure and table environments.
• More about "floats": The placement of floats is one of the most complex issues in TeX, and there is much more to it than can be described here. For more information about floats see Section 6.4 of Kopka/Daly, "A guide to LaTeX", or Chapter 6 of "The LaTeX Companion" by Mittelbach/Goosens.

Back to the LaTeX Tips Page