Force LaTeX image to appear in the section in which it's declared
Try \begin{figure}[!htb]
. In nearly all cases it helps.
Explanation of the figure placement parameters:
h
- Place figure here, if possiblet
- Place figure at the top of a pageb
- Place figure at the bottom of a page and!
- Over-ride default LaTeX figure placement (do not use the parameter values).
If that doesn't work then use:
\usepackage[section]{placeins}
This prevents placing floats before a 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.
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.