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)

  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.

  7. Thanks a lot!

  8. Awesome mate !!! After wasting hours with figures, your method just worked for me

  9. Hello.

    Why do you add the % at the end of some lines if you aren’t writing any comment?

    • The % at the end of the line makes the end of the line to a “comment”, so it is disabled. Otherwise the end of the line acts like a simple white space. That’s desired in normal text, but not needed between LaTeX commands, it may happen to appear in text causing unwanted space.

Leave a Reply to Thorstein Veblen Cancel reply

Required fields are marked *