# Align pictures in text

I have some code for a picture that looks like this:

...Text before pic...

\begin{figure}[h!]
\caption{caption stuff}
\centering
\includegraphics{somegraph.png}
\end{figure}

... Text after pic...\\

\part{New Part}


However, LaTeX seems to be doing some weird positioning stuff, since what happens is this:

...Text before pic...

... Text after pic...

pic

New Part


Whereas what I want is this

...Text before pic...

pic

... Text after pic...

New Part


I can achieve what I want with only a \includegraphics{somegraph.png}, but I would like the captioning too, and the ability of LaTeX to automatically number figures for me. I tried isolating the problem, but the figure just keeps jumping around and eventually lands in the right place. h! worked before for a previous figure, but no longer... What should I be doing here?

-
You should take a look at Stefan's answer. Basically, you can use the H specifier from the float package, or you could use the \captionof macro from the capt-of package. – TH. Apr 3 '11 at 8:47
I was looking for a duplicate but I can't find a question which only handles preventing a figure to float without anything extra. Stefan's answer comes very close, but doesn't provide the solution in-text but only as external link. – Martin Scharrer Apr 3 '11 at 9:19

The figure environment is a float, i.e. a block which can float through the text and is placed where (La)TeX thinks it will look the best. Normally (i.e. in professional produced books) you \label the \caption (which is supposed to go below the image) and then \ref-erence it. This way the image position is not that important.

You can change the figure to a non-floating environment using the float package and the H positing parameter:

% In preamble:
\usepackage{float}
% In document:
...Text before pic...

\begin{figure}[H]
\centering
\includegraphics{somegraph.png}
\caption{caption stuff}
\end{figure}

... Text after pic...\\

\part{New Part}


Another possibility is to not use figure directly. The \caption can still be added using \captionof{figure}{<text>} which is provided by the two packages caption (quite big) or capt-of (quite small). I prefer to use the center environment in these cases. It takes care about the centering and adds some margin before and after:

% In preamble:
\usepackage{capt-of}
% In document:
...Text before pic...

\begin{center}
\includegraphics{somegraph.png}
\captionof{figure}{caption stuff}
\end{center}

... Text after pic...\\

\part{New Part}

-
Unfortunately, using H makes the figure go to the end of the document. Any ideas? – wrongusername Apr 3 '11 at 18:51
@wrongusername: See the answers to How can I get the figures not to be pushed to the end of the document? for help. Also try !H. – Martin Scharrer Apr 3 '11 at 18:58
Ah thanks! !H didn't help at all, but eventually I made the scale of the image to 0.76 and it worked fine. Which is weird, because the picture is totally small enough to fit in the spot in the first place – wrongusername Apr 3 '11 at 20:11