Prevent floating of figures or tables
Juni 13th, 2008 by Stefan Kottwitz
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.
This entry was posted on Freitag, Juni 13th, 2008 at 17:08 and is filed under Graphics. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.





Februar 8th, 2012 at 11:18
Thank you so much, that has been buging me all morning. -.-
Juni 15th, 2012 at 15:26
Thank you!
August 16th, 2012 at 08:54
thanks
Januar 21st, 2013 at 10:29
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…
Januar 21st, 2013 at 10:35
My bad…I used the wrong brackets. Using the packages caption and capt-of works!
April 24th, 2013 at 05:58
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.