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


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.


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

  • 2
    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
  • 1
    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
  • 1
    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
  • 3
    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

I encountered the same problem, and figured out how to solve it. Use \pagebreak. My code looks like this




If you do this, the figure will appear first, then the code listing. Note: the listing won't jump onto a new page, unless the page is already full with the figure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.