It can be frustrating trying to get your figures and tables to appear where you want them in a LaTeX document. Sometimes, they just seem to float off onto another page of their own accord. Here is a collection of tools and ideas that help you get control of those pesky floats.
Use the placement options: h, t, b and p. For example
\begin{figure}[htb] |
causes LaTeX to try to fit the float “here”, or at the “top” of the current page (or the next page), or at the “bottom” of the current page (or the next page). If “p” is specified, it will allow the float to take a whole page to itself. You can’t specify only “h” as that is too restrictive, and LaTeX will automatically change it to “ht”. The default setting is “tbp”.
One of the reasons that the floats won’t go where you want them is that there are a lot of constraints on where they can go. The main ones are
Counter | Default | |
topnumber |
maximum number of floats at top of page | 2 |
bottomnumber |
maximum number of floats at bottom of page | 1 |
totalnumber |
maximum number of floats on a page | 3 |
Command | ||
\topfraction |
maximum fraction of page for floats at top | 0.7 |
\bottomfraction |
maximum fraction of page for floats at bottom | 0.3 |
\textfraction |
minimum fraction of page for text | 0.2 |
\floatpagefraction |
minimum fraction of floatpage that should have floats | 0.5 |
These can all be changed individually. But it is often easier to add ! before the placement options, thus forcing LaTeX to ignore most of these contraints. For example, I often use
\begin{figure}[!htb] |
If you want to change the defaults, the following values give reasonable results:
\setcounter{topnumber}{2} \setcounter{bottomnumber}{2} \setcounter{totalnumber}{4} \renewcommand{\topfraction}{0.85} \renewcommand{\bottomfraction}{0.85} \renewcommand{\textfraction}{0.15} \renewcommand{\floatpagefraction}{0.7} |
The \clearpage
command starts a new page and inserts all floats that have not yet appeared before continuing. This can leave a bad page break, so a useful alternative is to use the afterpage
package, and then insert
\afterpage{\clearpage} |
which will put all the floats at the end of the current page.
A very useful package is placeins
. This provides the command \FloatBarrier
which causes all unprocessed floats to be processed at that point, but does not start a new page unless it is necessary. To keep floats in the sections in which they were included, use
\usepackage[section]{placeins} |
This silently puts a \FloatBarrier
command before each section. There are other options explained in the placeins
documentation.
Another useful package is flafter
. This causes floats to always appear after their placement in the document.
If you really don’t want LaTeX to move your float at all, then use the float
package with the command \restylefloat{figure}
in the preamble. This allows you to specify[H]
as the position parameter which means “Here and only Here”. However, this often gives bad page breaks.
点击打开链接