# Force LaTeX image to appear in the section in which it's declared [duplicate]

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}

-
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
–  diabonas Oct 24 '11 at 21:07

## migrated from stackoverflow.comOct 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

try \begin{figure}[!ht], in nearly all cases it helps. If not then use:

\usepackage[section]{placeins}


it prevents placing floats before the section.

-

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.

-
using [H] is always not a good idea! –  Herbert Oct 24 '11 at 21:19

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.

-

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


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

-