184

Possible Duplicates:
How do I ensure that figures appear in the section they're associated with?
Keeping tables/figures close to where they are mentioned

How can I force an image in a LaTeX documents to appear in the section in which I declared it?

I don't want that an image about "Section A" to appear in "Section F". :(

I actually include an image with

\begin{figure}[ht]
\begin{center}
\includegraphics[scale=0.7]{./img/RobotManipulator3D_WorkSpace.png}
\end{center}
\caption{3D View of the robotic workspace (in red).}
\label{Img:3DRobotWorkspace}
\end{figure}
3

4 Answers 4

205

Try \begin{figure}[!htb]. In nearly all cases it helps.

Explanation of the figure placement parameters:

  • h - Place figure here, if possible
  • t - Place figure at the top of a page
  • b - Place figure at the bottom of a page and
  • ! - Over-ride default LaTeX figure placement (do not use the parameter values).

If that doesn't work then use:

\usepackage[section]{placeins}

This prevents placing floats before a section.

7
  • 16
    \usepackage[section]{placeins} doesn't seem to prevent a float from being put after subsections, however.
    – Nate
    Nov 25, 2014 at 18:15
  • 1
    \usepackage[subsection]{placeins} would do that @Nate. Check out further options for placeins.
    – Midnighter
    Oct 16, 2015 at 13:32
  • 22
    @Midnighter: No. \usepackage[subsection]{placeins} produces LaTeX Error: Unknown option subsection' for package placeins'. However, this answer explains how to redefine \(sub)subsection to include \FloatBarrier.
    – Jakob
    Feb 17, 2016 at 16:14
  • This answer is awesome Jun 11, 2016 at 13:23
  • 4
    Using \usepackage[section]{placeins} solved for me.
    – kitsune
    Mar 15, 2019 at 12:05
37

As Werner commented: the section Moving tables and figures in LaTeX in the TeX FAQ states:

Even if you use the placement specifier [h] (for ‘here’), the figure or table will not be printed ‘here’ if doing so would break the rules; the rules themselves are pretty simple, and are given on page 198, section C.9 of the LaTeX manual.

Use the float package with the [H] specifier. If you comment out the \usepackage{float}, the following MWE results in the figure on the second page, but as is the figure appears between the two paragraphs.

\documentclass{article}
\usepackage{float}% If comment this, figure moves to Page 2
\usepackage{lipsum}
\usepackage[demo]{graphicx}

\begin{document}
\lipsum[1]
\begin{figure}[H]
  \centering
  \includegraphics{foo}
  \caption{caption text}
  \label{fig:nonfloat}
\end{figure}
\lipsum[2]
\end{document}

You should also be using \centering instead of \begin{center}...\end{center} as per should i use center or centering for figures for more details.

7
  • 8
    using [H] is always not a good idea!
    – user2478
    Oct 24, 2011 at 21:19
  • 6
    Why is that? Would you care to elaborate?
    – jnns
    Jul 22, 2014 at 10:27
  • 1
    @Herbert: I think the above question was intended for you. Jul 22, 2014 at 18:47
  • 1
    @jnns: An image with caption is just like one big box (character) for TeX and for filling the page with text it is easier if TeX can move such big objects to places where it makes sense. Otherwise you can have a lot of whitespace if an image must appear where it was declared.
    – user2478
    Jul 22, 2014 at 18:51
  • 3
    Latex attempts to minimize whitespace. As more documents are read on screens instead of paper, it makes sense to adjust the rules to make a document easier to read (e.g. by placing floats after the text that discusses them) even if it adds whitespace. Jun 28, 2019 at 18:48
29

If you don't want your figure to float, don't use a floating environment; you can use, for example, a center environment and (if a caption is needed) the \captionof command provided by the capt-of (or caption) package:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{capt-of}
\usepackage{lipsum}% just to generate text for the example

\begin{document}
\lipsum[1]
\begin{center}
  \includegraphics{foo}
  \captionof{figure}{A non floating figure}
  \label{fig:test}
\end{center}

\lipsum[2]
\end{document}

If you want the figure to float, but not passing a \section command. you can use the placeins package and its \FloatBarrier command beyond which floats may not pass. A package option allows you to declare that floats may not pass a \section command, but you can place \FloatBarriers wherever you choose.

4

You don't mention how tall the float is. This leaves open several reasons, related to LaTeX's float placement parameters, for why the figure is showing up "too late" relative to where you'd like to occur.

Assuming that the figure's total height is no more than 0.5\textheight, you may have a good experience with the following setup, which uses the afterpage package and its command \afterpage:

[code up to the figure of interest]
\afterpage{\clearpage
\begin{figure}[t] % note the use of the "t" placement specifier
\centering % don't use \begin{center} ... \end{center}: it wastes too much space
\includegraphics[scale=0.7]{./img/RobotManipulator3D_WorkSpace.png}
\caption{3D View of the robotic workspace (in red).}
\label{Img:3DRobotWorkspace}
\end{figure}}
[remaining code of your document]

The reason I mention the 0.5\textheight consideration is that one of LaTeX's key parameters for placing floats on a page is \floatpagefraction, a fractional number between 0 and 1; by default, it is equal to 0.5 (unless set to a different value by some class or package command). Thus, if your figure is taller than 0.5*\textheight, LaTeX will refuse to place it on a page along with other, i.e., text, material, causing the float to drift all the way back until some forced page break (caused possibly by a \clearpage command) occurs. In such a case, you should probably issue the commands

\renewcommand\floatpagefraction{0.8} %% default value: 0.5
\renewcommand\topfraction{0.8}       %% default value: 0.7

and recompile the document. Of course, if your float's height is, say 85% or 95% of \textheight, you should really be willing to place it on a page of its own (most easily accomplished by choosing the [p] placement specifier).

A comment on the use of the [H] placement specifier that's provided by the float package: While it's true that it sometimes helps with placing an otherwise difficult-to-place-correctly float, using it is really tantamount to treating a symptom rather than finding a cure for the underlying cause of the problems related to placing the float satisfactorily. Before you resort to using it, you should really consider modifying LaTeX's float-placement parameters. That said, I too have experienced cases, with my own papers, where I've resorted to using the [H] specifier after I struck out having tried all other remedies...

Not the answer you're looking for? Browse other questions tagged .