I have the following situation:

\subsection{Some subsection}
Some text here...

\section{New section}
New section starts here...

For some reason, the first 2 images are showing up before that subsection starts (above the title of it), and the 3rd image is showing up mid sentence on the next completely new section, on a brand new page! What gives? Why is this happening and how can I resolve it?

  • I'm baffled that none of the people answering explain that it's not the \includegraphics command that makes problems, it's the use of the floating environment called figure causing the wandering off. Many novices don't understand this.
    – lblb
    Apr 27 '18 at 20:34

In LaTeX document elements such as images and tables are known as "floats" because they are free to float around---even across section and page boundaries. The reason this happens is that the TeX engine is trying to decide the "best" way to break your document into lines, paragraphs and pages so that the end result is as good as it can be. If TeX is free to push large objects like floats around, it can make better decisions for the document as a whole.

Unfortunately, as a document writer your idea of what looks "best" is often very different from what TeX decides. Here is my three step program for resolving this situation:

1. Wait until the rough draft is finished

When you are adding a bunch of content to a document, floats are going to jump all over the place as the page breaking algorithm calculates new results on every run. Trying to "put the figures in their place" at this stage will just cause a lot of stress and lost time. Just focus on writing the content of your document and by the time a rough draft has taken shape you will find that 80% of the time TeX did an awesome job positioning the floats and there is nothing else you need to do.

2. Give TeX some guidance

When the rough draft is ready and you are correcting spelling errors, etc, you may notice the odd float that is just not in a good place. In these cases you can give TeX some helpful hints on how to handle the positioning by including optional parameters in your `\begin{figure/table/whatever}. Here is an example using a figure:

I would prefer that \autoref{fig:example} be placed right after this paragraph.
If that is not possible, I would like it at the the top of a page.


The figure definiton uses a "float specifier" of h to tell TeX to try and keep the figure near the point it occurred in the input and not several paragraphs before or after. The next specifier t is a fallback that tells TeX to place the figure at the top of a page if it floats too far away from the place where it was defined.

Here is another example using a table:

I really, really want \autoref{tab:example} be placed right after this
paragraph. If that is not possible, I would like it at the the bottom 
of a page.


In the above example an exclamation point has been added which strongly suggests that TeX try to place the figure at the point it occurs in the input. It also lets TeX know that is is okay to break some of it's rules about typesetting in order to satisfy this request. The float specifier b is again used as a fallback---it means to place the figure at the bottom of a page if it floats too far.

The final float specifier is that can be used is p and it means that if a float moves too far, it should be collected into a "float page" that contains nothing but stray floats and is inserted when TeX finds it convenient. I don't use p that often.

3. Tell TeX exactly how you want it done

Following steps 1 and 2 will give you good results in most cases. For those few floats that end up in messy places no matter what, there are a two tricks I use to nail them down. The first technique is something I use with a handfull of floats that together take up a whole page or more. It involves grouping them onto a separate page much like the p specifier, but gives me more control:

The graphs in \autoref{fig:graph1} and \autoref{fig:graph2} are quite
large, so they are shown on the next page.





The important bit is the \clearpage---it forms a page break that floats cannot cross which effectively confines the two graphs to their own page. The \vfill commands are used to vertically center the content. The second trick I use is the float package which provides the H float specifier:

\autoref{tab:example} absolutely, positively MUST occur after this sentence.


Tagging floats with H effectively makes them quit floating and behave like rather large "words". TeX can no longer move them around any more than it could rearrange the words in your sentences. If you find yourself reaching for H first consider if there is something else that could be done such as decreasing the size of the float, moving it to an appendix, etc as H is really a weapon of last resort.

  • 1
    I am trying the autoref recommendation, but I'm getting it as a syntax error. "Undefined control sequence"
    – user5000
    Apr 20 '11 at 2:49
  • Sorry about that---it's a goodie provided by the hyperref package. Try it out, I'm sure you'll love it!
    – Sharpie
    Apr 20 '11 at 2:53
  • I'd vote this up twice if I could; once for the answer, then a second vote for "Wait until the rough draft is finished" Apr 20 '11 at 21:09
  • So I added an H, and my stupid picture is still appearing in the next chapter. Once I fix this and make sure no other pictures have messed up because of it, the rough draft is done. Any help? It's like the 100% sure I want this here [H] tag isn't 100%. Clearpage was able to fix it Jul 2 '13 at 20:07
  • 1
    Very nice answer! Good to see some actual workflow description. Mar 6 '14 at 8:37

The first thing to try is to place a position specifier with the float environment. The format is

\begin{figure}[position specifier]
    %stuff to place in the figure

The valid options for the position specifier are:

h : approximately here

t : top of the page

b : bottom of the page

p : on a page that just has floats.

! : to put emphasis on a specifier --> h! means "really here"

There are other options, but they require the float package.

So you might want to try

  • 2
    Even using the exclamation point is still "just a suggestion". It just means that TeX can break some more rules to try and follow it.
    – Sharpie
    Apr 20 '11 at 2:58
  • 1
    Perfect! helped me. Honestly latex must either die or do things simpler. Saved my time. Thousand thanks.
    – Aubergine
    Mar 25 '12 at 0:41
  • 4
    h! means "really here". But what does !h mean?
    – Ka-Wa Yip
    Jun 19 '15 at 21:34
  • 3
    The same. The order does not matter.
    – stefanct
    Aug 31 '16 at 14:41
  • 1
    \begin{figure}[h!] seems still not working.. We must use \usepackage{float} And then say \begin{figure}[H]
    – recolic
    Mar 21 '20 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy