6.5 Control the placement of figures

One common frustration with LaTeX is the placement of figures and tables. Unlike in a word processor like Microsoft Word, in which figures are placed directly where the user specifies, LaTeX will attempt to place a figure in a position that does not violate certain typographic rules. In doing so, figures may float away from where they are referenced in the text. This section will explain some background information on how floating environments (such as figures) work and provide several options for customizing their behavior.

6.5.1 Floating environments

By default, figures with captions are generated with the figure environment in LaTeX. For example, Pandoc will convert the following Markdown code that contains an image

![This is a figure.](images/cool.jpg)

to the LaTeX code:

  \caption{This is a figure.}

The figure environment is a floating environment. You can read a detailed description of floats at https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions. In summary, floats are used as containers for things that cannot be broken over a page, such as tables and figures. If the figure or table cannot be contained in the space left on the current page, LaTeX will try to place it at the top of the next page. If the figure is tall enough, it may occupy the whole next page, even if there is still space left for a few lines of text. The behavior can be controlled by different placement specifiers in square brackets after \begin{figure}, e.g., \begin{figure}[b]. Below is a list of possible specifiers:

  • h: Place the float here, i.e., approximately at the same point it occurs in the source text.
  • t: Position at the top of the page.
  • b: Position at the bottom of the page.
  • p: Put on a special page for floats only.
  • !: Override internal parameters LaTeX uses for determining “good” float positions.
  • H: Place the float at precisely the location in the LaTeX code. This requires the float package (\usepackage{float}).

These specifiers can be used in conjunction, e.g., !b forces LaTeX to place the figure at the bottom of a page. The default behavior is tbp. That is, LaTeX will try to position the figure at the top of the page, then at the bottom, and then on a separate page.

6.5.2 Prevent figures from floating

Many users will initially want to prevent figures from floating in their document, replicating the behavior of a traditional word processor. To do this, we must firstly load the LaTeX package float. This can be done by including the following line in the YAML:

    extra_dependencies: ["float"]

We can use the chunk option fig.pos to control the float behavior. The option value H from float will prevent any floating within the document. We can set the default behavior for the document so that all chunks have this setting by including the following line in the first code chunk in your R Markdown document:

knitr::opts_chunk$set(fig.pos = "H", out.extra = "")

In general, we do not recommend that users force LaTeX to stop floating figures. This solution was included in this book by popular demand,10 but there could be some serious side effects when LaTeX is unable to float figures.

6.5.3 Force floats forward

An alternative to forcing all floats to be held is to force floating forward in the text. This can remove a common issue, where a figure is shown at the top of the page before the relevant text comes. This can break the reading flow of a report. We can force the figure to always appear after the text by using the flafter LaTeX package as follows:

    extra_dependencies: ["flafter"]

6.5.4 Adjust LaTeX placement rules (*)

LaTeX’s own float placement parameters could prevent placements that seem entirely “reasonable” to you—they are notoriously rather conservative. These defaults are displayed in Table 6.1.

TABLE 6.1: Default LaTeX float settings.
Command Description Default
topfraction max fraction of page for floats at top 0.7
bottomfraction max fraction of page for floats at bottom 0.3
textfraction min fraction of page for text 0.2
floatpagefraction min fraction of page that should have floats 0.5
topnumber max number of floats at top of page 2
bottomnumber max number of floats at bottom of page 1
totalnumber max number of floats on a page 3

To encourage LaTeX not to move your figures, we can alter these default settings. We could include the following in our LaTeX preamble file, reducing the minimum amount of text required on a page and allow more room for floats:


If we have added these lines to a .tex file, we could include this file in the preamble of the LaTeX document using the method introduced in Section 6.1.

  1. The related Stack Overflow question has been viewed for over 45,000 times: https://stackoverflow.com/q/16626462/559676.↩︎