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.

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}
share|improve this question

migrated from stackoverflow.com Oct 24 '11 at 20:44

This question came from our site for professional and enthusiast programmers.

marked as duplicate by diabonas, Torbjørn T., lockstep, Martin Scharrer Oct 24 '11 at 21:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
This typically happens when using floats. If you don't want yout floats to float, you can use the [H] float specifier from the float package. It is also not necessary to put your figure in a (floating) figure environment if you just want it to stay put, but this depends on your preference and other considerations like a "List of Figures" (say). The following UK TeX FAQ entry is also helpful: Figure (or table) exactly where I want it. Does this solve your problem? –  Werner Oct 24 '11 at 18:32
    
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Oct 24 '11 at 20:55
3  

4 Answers 4

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

Explanation of the figure placement parameters:

  • h - Place figure here
  • 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.

If that doesn't work then use:

\usepackage[section]{placeins}

This prevents placing floats before a section.

share|improve this answer
    
\usepackage[section]{placeins} doesn't seem to prevent a float from being put after subsections, however. –  Nate Nov 25 at 18:15

As Werner commented: the section Moving tables and figures in LaTeX at tex.ac.uk 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.

share|improve this answer
4  
using [H] is always not a good idea! –  Herbert Oct 24 '11 at 21:19
    
Why is that? Would you care to elaborate? –  jnns Jul 22 at 10:27
    
@Herbert: I think the above question was intended for you. –  Peter Grill Jul 22 at 18:47
    
@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. –  Herbert Jul 22 at 18:51

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.

share|improve this answer

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...

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.