[Tex/LaTex] Image from \includegraphics showing up in wrong location


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?

Best Answer

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.