Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following situation:

\subsection{Some subsection}
Some text here...
\\
\begin{figure}
    \caption{Raw}
\includegraphics[scale=0.25]{raw.png}
\end{figure}
\begin{figure}
    \caption{Median}
\includegraphics[scale=0.25]{median.png}
\end{figure}
\begin{figure}
    \caption{Data}
\includegraphics[scale=0.25]{data.png}
\end{figure}
\\

\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?

share|improve this question
add comment

2 Answers

up vote 28 down vote accepted

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.

\begin{figure}[ht]
  \label{fig:example}
\end{figure}

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.

\begin{table}[!hb]
  \label{tab:example}
\end{table}

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.

\newpage
\vfill

\begin{figure}
  \label{fig:graph1}
\end{figure}

\begin{figure}
  \label{fig:graph2}
\end{figure}

\vfill
\clearpage

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.

\begin{table}[H]
  \label{tab:example}
\end{table}

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.

share|improve this answer
    
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" –  Brent.Longborough 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 –  SwimBikeRun Jul 2 '13 at 20:07
    
Very nice answer! Good to see some actual workflow description. –  Rabarberski Mar 6 at 8:37
add comment

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
\end{figure}

The valid options for the position specifier are:

h : approximately here

t : top of the page

b : bottom of the page

! : 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

\begin{figure}[h]
    \caption{Raw}
\includegraphics[scale=0.25]{raw.png}
\end{figure}
share|improve this answer
    
This did not work. In fact, I get a warning "Latex warning: 'h' float specifier changed to 'ht'". and it still has the same issue –  user5000 Apr 20 '11 at 2:43
    
Strangely, this means that it is working :-). h is merely a "suggestion" and TeX needs another option if it doesn't work, so it adds the t. –  Sharpie Apr 20 '11 at 2:50
    
As Sharpie mentions, h is only a suggestion. If you really want to force things, use the exclamation point or use the H option, made available by the float package. Also, follow the advice in Sharpie's answer. –  Frédéric Apr 20 '11 at 2:55
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.