Controlling LaTeX Floats

Introduction

One of the most frustrating features of LaTeX is its insistence on moving figures and tables (and other “floating matter”) to unexpected locations. However, there is a way to get some control over this problem.

It turns out that the main cause of the trouble is an extremely stupid choice of defaults for the float mechanism in LaTeX. Fortunately, these defaults are not hard-wired. They're set by the values of various parameters in the startup file used by LaTeX. That means you can just reset the ill-chosen values to something that gives more reasonable behavior.

Once these changes are made, the addition of an [htp] option to commands like \begin{figure} and \begin{table} — which is needed to override another set of ill-chosen defaults — will usually put figures and tables where you could reasonably expect them to appear in the formatted document.

Why there is a problem

Perhaps it's useful at this point to delve just a bit into the mechanism LaTeX uses to determine where a float goes.

There are several integer-valued registers (called counters in TeX jargon) that limit the number of floats on a page: the number at the top, the number at the bottom, and the total. In addition, there are limits on the fraction of a page that can be occupied by floats — again, at the top and bottom separately, as well as the page as a whole.

If there are too many floats to fit on a page, LaTeX pushes them on to the next page, and the next; eventually, floats may end up at the end of the document. If the [p] option has been provided to individual figures and tables, they may be pushed together onto a “float page” that has no text. But even here, the defaults are stingy about the amount of space taken up.

The result is often that some individual float is too big to go anywhere, and so migrates to the end of the document. Unfortunately, the rule that says all figures must appear in sequential order (and a similar rule for tables) means that the single offender sweeps away everything that should follow it.

These parameters are discussed in detail on pp. 199–200 of the LaTeX Reference Manual (Lamport's book).

Undoing the dumb defaults

The default values that cause so much trouble are set in the class file that's invoked by the argument of the \documentclass statement at the beginning of your *.tex file. (For example, if that argument is article, the file that's called in is /usr/share/texmf/tex/latex/base/article.cls.)

To override the values that cause the offensive behavior, you use the \setcounter{} command to reset integer values, and \renewcommand{} to reset floating-point values. Like this:

% Alter some LaTeX defaults for better treatment of figures:
    % See p.105 of "TeX Unbound" for suggested values.
    % See pp. 199-200 of Lamport's "LaTeX" book for details.
    %   General parameters, for ALL pages:
    \renewcommand{\topfraction}{0.9}	% max fraction of floats at top
    \renewcommand{\bottomfraction}{0.8}	% max fraction of floats at bottom
    %   Parameters for TEXT pages (not float pages):
    \setcounter{topnumber}{2}
    \setcounter{bottomnumber}{2}
    \setcounter{totalnumber}{4}     % 2 may work better
    \setcounter{dbltopnumber}{2}    % for 2-column pages
    \renewcommand{\dbltopfraction}{0.9}	% fit big float above 2-col. text
    \renewcommand{\textfraction}{0.07}	% allow minimal text w. figs
    %   Parameters for FLOAT pages (not text pages):
    \renewcommand{\floatpagefraction}{0.7}	% require fuller float pages
	% N.B.: floatpagefraction MUST be less than topfraction !!
    \renewcommand{\dblfloatpagefraction}{0.7}	% require fuller float pages

	% remember to use [htp] or [htpb] for placement

These values can be reset in the preamble of your LaTeX source file; any place before the \begin{document} will do.

A remark about the [htp] argument

Though it's tempting to hope that [htp] gives preference to the [h] (i.e., “here”) option, it doesn't work that way. The arguments in brackets tell LaTeX where it's possible to put the float; their order is unimportant. If you're willing to have floats at the bottom of a page, as well as the top, use [htpb].

Putting an exclamation mark in the list of placement options makes LaTeX ignore all the constraints but \topfraction, \bottomfraction, and the ones with floatpage in their names.

The default, [tbp], omits [h], which is not usually what you want. (But note that many journal and book publishers may insist on the [tpb]choices alone.)

If you find that liberal values of the float parameters still are causing trouble, you can try forcing a float page with \clearpage to disgorge the accumulated blockage. If you don't want to force a pagebreak, use the afterpage package and tell LaTeX  \afterpage{\clearpage}, which should force a float page when the current page comes to an end. If floats continue to pile up at the end, you probably have one too big to fit on a page; try reducing its size.

Run  texdoc fancyhdr  to read a good discussion of float placement. You may even need to use the float package, in extreme cases.

Fine Tuning

It can get particularly tricky to put floats where you want them in 2-column formats, because floats that use the whole width of the page can (normally) only  appear at the top of a page. Sometimes this makes a page-width figure appear before a column-width figure that should precede it. This problem can be fixed by adding

		\usepackage{stfloats}
to the preamble of your LaTeX source file, and specifying [b] instead of [h] as the location parameter:
		\begin{figure*}[b]
for the figure that spans two columns. (There is also a dblfloatfix package, if you need more flexibility.)

When a float should appear at the top of a page, you have to call for it in the text before the end of the previous page. But then adding or removing text anywhere before the declaration of the float can change the location of that page break in the formatted document. A particularly tricky situation can arise when a section or subsection heading comes near the page break (or a column break, in 2-column formats), because the heading carries with it some white space. If that white space is adjacent to the beginning or end of a column or page, the white spaces are combined.

In other words, a heading takes up more room in the middle of a page or column than it does at the end. That means that sometimes adding or removing a single word can shift some following text by several lines — and that can change the relative positions of a page or column break and a float several pages later in the document.

Remember that LaTeX is a once-through, sequential processor: it can't back up to adjust placement. (That's the reason for the need to invoke it twice, to get the numbers of figures and references correct.) So, once a float is put in the wrong place, it affects the placement of everything that comes after it.

So you should start at the beginning of your document, adjusting the placements of floats in order from beginning to end. Several cycles of trial and error may be needed to keep all your floats in the right place, near the text where they are discussed.

 

Copyright © 2005, 2006, 2010, 2017 Andrew T. Young


Back to the . . .
LaTeX-to-PDF conversion page

or the LaTeX top page

or the alphabetic index

or the website overview page