Prevent Figures from Floating Outside Sections in LaTeX

LaTeX floats a figure for you as if it was a rotting raft in the middle of a stormy ocean. Most folks learn to tame this beast of burden, but I find that there are times when I need my figures to stay within the section, subsection, or subsubsection I references them in.

Using \FloatBarrier from the placeins package, you can force LaTeX to display all the figures that precede the \FloatBarrier command before displaying any of the content after the command.

You can add \FloatBarrier to the section, subsection, or subsubsection commands using the extraplaceins package that I modified to include subsection and subsubsections. Then add this to your LaTeX document:

Download it here: http://lexfridman.com/files/research/extraplaceins.sty

One thought on “Prevent Figures from Floating Outside Sections in LaTeX

  1. Lisa

    Agreed – I know I have the package saved on another computer somewhere, but could you fix the download link? I liked this command, it made dealing with images in LaTeX just a little less painful.

    Thanks!

    Reply
  2. Mike

    Works fine. The ProvidesPackage statement should be updated though – right now, it says it provides package placeins, which triggers a warning.

    Reply
  3. Ankit

    Hi,
    While using the package, plots are leaving almost a complete page at some places. Just after a section say ‘Plot’ almost a page is left and the plot starts from a new page. I am not able to control it.
    Please help.
    Thanks in advance.

    Reply
    1. Lex Post author

      Please provide an example document since I’m not sure what you mean exactly. One of the problems with LaTeX is you can’t easily control the position of figures, but with this package, it should at least stay within the section that it’s placed in.

      Reply
      1. Ankit

        My plots do stay within the section but the problem is that at some places a complete page is left blank after the section heading and the plot is placed on a new page. The problem arises when I give caption to plots.
        Is there any way so that the plots don’t leave blank spaces after the section headings?
        for eg.
        section{Plots}
        begin{figure}[h!]
        includegraphics{plot.png}
        caption{plotLabel}
        end{figure}
        Floatbarrier

        Reply
        1. Lex Post author

          Why are you adding a FloatBarrier after the figure? That shouldn’t be the source of your problem, but perhaps it is. Also the [h!] could be the problem though also not likely. Let me know if you figure it out.

          Reply
          1. Ankit

            I am doing so because after the figure I have a new subsection. And I am using [h!] to prevent the figure from leaving blank pages but it doesn’t seem to work either. I would be glad if u can help me here or give me some pointers on why are my figure leaving blank spaces after section headings.

  4. Fred

    Hi,

    any idea how to prevent this warning?

    extraplaceins.sty LaTeX Warning: You have requested package extraplaceins', but the package provides placeins’.

    ty

    Reply
  5. Stephan

    Thank you, this package is fantastic. Knowing this some years earlier would have saved me dozens of hours taming the floating tiger 😀

    Reply
  6. DLC

    Hmmm… This generally works, but unfortunately, my Figures are still floating into my Bibliography. Don’t know why. This is a slightly different problem than the one you solved since the bibliography is a bit different from a proper section. FloatBarrier and clearpage don’t work for me, neither does putting [!ht] to my figures…

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *