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 | 8 comments

Comments (8)

  1. Thank you so much, that has been buging me all morning. -.-

  2. Thank you! :)

  3. Thank you so much!

  4. I just tried the caption-package. Including graphics works the way you described it. But using “captionof{figure}[text]” produces an error (“Tex capacity exceeded…”).

    Same thing with the “capt-of”-package…

  5. My bad…I used the wrong brackets. Using the packages caption and capt-of works!

  6. Hmmm… Couldn’t get any of these three to work. I tried captionof with both kinds of brackets, but to no avail. And, yes, I used the package…

    In the first one, the important thing to include is [!htbp], right? That did not prevent floating in my case, but at least the code ran.

Leave a Reply

Required fields are marked *