3

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?

David Carlisle
  • 757,742

1 Answers1

5

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}
David Carlisle
  • 757,742
Martin Scharrer
  • 262,582