Prevent floating of figures or tables

Often images are embedded using the figure environment, they can get a caption, a label for reference and they will get numbered and appear in the list of figures. Figure environments are intended to float, but sometimes it is needed to place an image exactly at a specific position, still wanting to benefit from the other mentioned features of the figure environment. A similar problem is the exact placement of tables where usually the table environment is used.

One possibility that may not always prevent floating is to set more positioning parameter:

\begin{figure}[!htbp]
\includegraphics{filename}%
\caption{text}%
\end{figure}

This may work well, but not in all cases.

To prevent floating at all we must not use the figure environment. The caption package gives a way to get all the other features. It provides the command \captionof that takes a counter (figure or table) as argument and of course the caption text, for example:

\usepackage{caption}
...
\begin{center}
\includegraphics{filename}%
\captionof{figure}{text}\label{labelname}%
\end{center}

The figure counter is used for numbering and the image will be listed by the \listoffigures command.

The caption package is very recommendable, it has a lot of features and is very well documented. But if you don’t want to use it there’s still the alternative package capt-of that provides \captionof too. It is very small, actually beside comments it contains just one line of code:

\newcommand\captionof[1]{\def\@captype{#1}\caption}

The float package provides another very easy alternative for preventing floating: just use the H positioning parameter:

\usepackage{float}
...
\begin{figure}[H]
...
\end{figure}

If the other features of float are not needed then I would recommend to use caption instead. Of course this works with tables too, just use tabular etc. together with \captionof{table}{…} and perhaps \center or \centering.

This topic was discussed in the LaTeX Community Forum and on the MatheBoard.

13. June 2008 by stefan
Categories: Uncategorized | 12 comments

Comments (12)

Leave a Reply to stefan