Skip to main content

Floats, figures and captions

by Andrew Roberts

In the previous tutorial, the importing of graphics was introduced. However, just having a picture stuck in-between paragraphs does not look professional. For starters, we want a way of adding captions, and to be able to cross-reference. What we need is a way of defining figures. It would also be good if LaTeX could apply similar principles to when it arranges text to look its best, to arranging pictures too. This is where floats come into play.

Floats

Floating elements refer to anything within a document that cannot be broken over a page. This basically boils down to tables and figures. They require special treatment, and the concept of floating was the solution for a robust method for dealing with such elements, whilst keeping the document presentation as `nice' as possible.

The most common problem is that there is not enough space on the remainder of a given page to fit the specified figure. To overcome this, LaTeX will float this over to the next page, whilst filling the current page with body text. Contrast this to what happens in MS Word, for example. If a image is added but is too large to fit on the current page, it will position it on the next page, but, will leave a large gap, instead of rearranging subsequent text to fill the space. It requires a lot of manual tweaking to rectify, whereas LaTeX takes care of all of this automatically.

Figures

To create a figure, you must use the figure environment (tricky, eh?!).

\begin{figure}[placement specifier]
... figure contents ...
\end{figure}

In the previous section, I was saying how floats are used to allow LaTeX to handle figures, whilst maintaining the best possible presentation. However, there may be times when you disagree, and a typical example is with its positioning of figures. The placement specifier parameter exists as a compromise, and its purpose is to give the author a greater degree of control over where certain floats are placed.

Specifier Permission
h Place the float here, i.e., at the same point it occurs in the source text.
t Position at the top of the page.
b Position at the bottom of the page.
p Put on a special page for floats only.
! Override internal parameters LaTeX uses for determining `good' float positions.

What you do with these placement permissions is to list which of the options that you wish to make available to LaTeX. These are simply possibilities, and LaTeX will decide when typesetting your document which of your supplied specifiers it thinks is best.

Tables

Although tables have already been covered, it was only the internal syntax that was discussed. The tabular environment that was used to construct the tables is not a float by default. Therefore, for tables you wish to float, wrap the tabular environment within a table environment, like this:

\begin{table}
  \begin{tabular}{...}
  ... table data ...
  \end{tabular}
\end{table}

You may feel that it is a bit long winded, but such distinctions are necessary, because you may not want all tables to be treated as a float.

Captions

It is always good practise to add a caption to any figure or table. Fortunately, this is very simple in LaTeX. All you need to do is use the \caption{text} command within the float environment. Because of how LaTeX deals sensibly with logical structure, it will automatically keep track of the numbering of figures, so you do not need to include this within the caption text.

The location of the caption is traditionally underneath the float. However, it is up to you to therefore insert the caption command after the actual contents of the float (but still within the environment). If you place it before, then the caption will appear above the float. Try out the following example to demonstrate this effect (See caption.tex and caption.pdf)

\begin{figure}
  \caption{A picture of a tucan.}
  \begin{center}
    \includegraphics{tucan.eps}
  \end{center}
\end{figure}

\begin{figure}
  \begin{center}
    \reflectbox{\includegraphics{tucan.eps}}
  \end{center}
  \caption{A picture of the same tucan looking the other way!}
\end{figure}

Labels and Cross-referencing

This is a good opportunity to introduce labels. Their purpose within LaTeX is to act as a marker, which can then be referenced to at any point within your document. It is very common to refer to each of your figures within the body of text. However, you don't want to be keeping track of the numbers, so you can use a label instead, and let LaTeX replace it with the correct figure number.

To add a label, you must embed the following command: \label{marker}.

To then reference a label: \ref{marker}.

Alternatively, to get a page reference: \pageref{marker}.

\begin{figure}
  \begin{center}
    \reflectbox{\includegraphics{tucan.eps}}
  \end{center}
  \caption{A picture of the same tucan looking the other way!}
  \label{toucan}
\end{figure}

Figure~\ref{toucan} shows a photograph of a toucan.

When a label is declared within a float environment, the \ref will return the respective fig/table number (although, it must occur after the caption). When declared outside, it will give the section number.

The tilde (~) in the above example is a special symbol within LaTeX. It represents a non-breaking space. It is useful here because it keeps 'Figure' and what ever number it \ref refers to as a single unit, and won't get split over lines or pages.

Wrapping figures

Although not normally the case in academic writing, an author may prefer that some floats do not break the flow of text, but instead allow text to wrap around it. (Obviously, this effect only looks decent when the figure in question is significantly narrower than the text width.) The package wrapfig was developed for this very task.

To use wrapfig, you must first add \usepackage{wrapfig} to the preamble. This then gives you access to the \begin{wrapfigure}{alignment}{width} command. Alignment can be either l for left, or r for right. The width is obviously the width of the figure. An example:

\begin{wrapfigure}{r}{40mm}
  \begin{center}
    \includegraphics{toucan.eps}
  \end{center}
  \caption{The Toucan}
\end{wrapfigure}
Example of image
			wrapped within text using wrapfig package

Note that the wrapfigure environment is in fact non-floating. This means that it may require some care in ensuring that your inserted figure doesn't run over page breaks.

Subfigures

A useful extension is the subfigure package. Unfortunately, only version 2.0 is installed on the SoC network, but a newer, much improved (in terms of extra features) version 2.1.4 is available, so be careful if you see examples of this package that use the latest version. Anyway, this gives the author the ability to have figures within figures. Subfigures have their own caption, and an optional global caption is also allowed. An obvious application would be to illustrate the before and after effects of a given operation. An example will best illustrate the usage of this package.

\begin{figure}[htp]
  \begin{center}
    \subfigure[Original image]{\label{fig:edge-a}\includegraphics[scale=0.75]{toucan.eps}}
    \subfigure[After Laplace edge detection]{\label{fig:edge-b}\includegraphics[scale=0.75]{laplace_toucan.eps}} \\
    \subfigure[After Sobel edge detection]{\label{fig:edge-c}\includegraphics[scale=0.75]{sobel_toucan.eps}}
  \end{center}
  \caption{Various edge detection algorithms}
  \label{fig:edge}
\end{figure}
Example of subfigure package in
		use - three sub-figures contained within a single figure, each with its own caption

You will notice that the figure environment is set up as usual. For each subfigure, you need to use the \subfigure[sub caption]{... figure ...} command. If you intend to cross-reference any of the subfigures, see where the label is inserted on the third example. \caption will provide the global caption. subfigure will arrange the figures side-by-side providing they can fit, otherwise, it will automatically shift figures below. This effect can be added manually, by putting the newline command (\\) before the figure you wish to move to a newline.

NB For subtables, there is a different \subtable command specifically for such types, but it works in much the same way.

Custom Floats

If tables and figures are not adequate for your needs, then you always have the option to create your own! A good example of such an instance would be a document that gives lots of source code examples of a programming language. One might therefore wish to create a program float. The float package is your friend for this task. All commands to set up the new float must be placed in the preamble, and not within the document.

  1. Add \usepackage{float} to the preamble of your document
  2. Declare your new float using: \newfloat{type}{placement}{ext}, where
    • type - the new name you wish to call your float, in this instance, 'program'.
    • placement - t, b, p, or h (as previously described.
    • ext - the file name extension of an auxiliary file for the list of figures (or whatever). LaTeX writes the captions to this file.
  3. The default name that appears at the start of the caption is the type. If you wish to alter this use, \floatname{type}{floatname}
  4. Changing float style can be issued with \floatstyle{style} (Works on all subsequent \newfloat commands, therefore, must be inserted before \newfloat to be effective.
    • plain - the normal style for LaTeX floats, i.e., nothing!
    • boxed - a box is drawn that surrounds the float, and the caption is printed below.
    • ruled - the caption appears above the float, with rules immediately above and below. Then the float contents, followed by a final horizontal rule.

An example document using a new program float type:

\documentclass{article}

\usepackage{float}

\floatstyle{ruled}
\newfloat{program}{thp}{lop}
\floatname{program}{Program}

\begin{document}

\begin{program}
  \begin{verbatim}

class HelloWorldApp {
  public static void main(String[] args) {
    //Display the string
    System.out.println("Hello World!");
  }
}
\end{verbatim}
  \caption{The Hello World! program in Java.}
\end{program}

\end{document}
an image showing the new
		program float

The verbatim environment is an environment that is already part of LaTeX. Although not introduced so far, its name is fairly intuitive! LaTeX will reproduce everything you give it, including new lines, spaces, etc. It is ideal for source code.

Boxed or ruled floats

The float package is perfect for styling your own custom floats, but what if you want similar styles for the existing table or figure floats? The float package can also be used here.

The float package has a couple of commands specifically designed to restyle LaTeX's existing figure and table environments. This can be activated in your document premable. For example, to set a document to have boxed figures and boxed tables:

\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}   % To re-style the 'figure' float

\restylefloat{table}    % To re-style the 'table' float.

Summary

That concludes all the fundamentals of floats. You will hopefully see how much easier it is to let LaTeX do all the hard work and tweak the page layouts in order to get your figures in the best place. As always, the fact that LaTeX takes care of all caption and reference numbering is a great time saver. Don't forget to have a look at the example .tex files below.