# Figure breaks listing, how to avoid that?

I have a listing in my document and some lines before it I'm inserting a figure, doing the following:

\begin{figure}[htbp]
\begin{center}
\includegraphics[scale=0.7]{filename.png}
\caption{Caption}
\label{fig:label}
\end{center}
\end{figure}


Unfortunately the figure is breaking the listing in two and I don't know how to avoid that. I tried changing the figure positioning, but I couldn't find a good looking solution.

EDIT:

The listing mentioned is a container for software code, like the following:

\begin{lstlisting}[language=C, label=lst, caption="Software", breaklines=true]
//my code here
\end{lstlisting}

-
Can you provide a minimal working example? From your code it is not very clear what exactly your problem is... By the way, you can try \begin{figure}[ht!]. –  Count Zero Sep 13 '11 at 10:13

The exact positioning of "floats", such as tables and figures, is one of the more mysterious procedures that LaTeX performs. You don't mention what kind of listing you're performing, but I'll assume it's either an enumerated or an itemized list. You do mention that you use [htbp] as the positioning option; the first element, h, stands for "here," i.e., LaTeX may be taking you seriously and is placing the float right where the float's code occurs.

In your case, i.e., in the presence of an itemized or enumerated list, a promising first step towards obtaining a pleasing float placement might be to insert the \begin{figure} ... \end{figure} code after the end of the list. Try this at first without providing a positioning option (such as [h]). If this doesn't generate a satisfactory outcome, provide either the [t] or [b] positioning options.

Finally, do note that fine-tuning the exact placement of float materials should generally be performed only after the rest of the text of the document is more or less final. Otherwise, you may have to redo your work all over again after you change the text here or there.

-
You might also want to mention the [H] placement option from the floats package, which tells TeX to place exactly at the point where it is included, no floating allowed. –  Jake Sep 13 '11 at 10:46
Well, given that (i) the OP's question wasn't very specific about what exactly is going wrong and (ii) h is already in Raffo's positioning specification, I was suspecting that [H] wasn't going to be helpful either. Moreover, if the listing ends around the middle of the page and the figure float takes up, say, 40% of the textheight, then specifying [H] might end up forcing the float to move back all the way to the very end of the document -- probably not what Raffo is trying to get done, right? –  Mico Sep 13 '11 at 10:59
I edited the question adding the listing I'm using. I already tried H with the floats package, but this, clearly, breaks the layout for the page before the figure, adding too much space between the text. Moving the figure after the end of the listing fixed the problem, thank you. I know that the fine-tuning of the placement of floats must be done at the end, but I need a beta version of my work to be ready for a pre-final revision. Thank you all, again. –  Raffo Sep 13 '11 at 11:22
One more thing, Raffo: you note that the figure's insertion creates too much whitespace. You may be able to reduce this by using the \centering instruction instead of the \begin{center} ... \end{center} pair of commands. –  Mico Sep 13 '11 at 11:35
Wow thank you, I've never read this \centering thing before, it reduces the whitespace a lot! –  Raffo Sep 13 '11 at 12:46