I have a document with some rather lengthy \lstlisting's (source code snippets). It looks really bad if there is a page break within a listing/snippet. Thus I tried to avoid page breaks in listings by putting the listings into a minipage ( Avoid page breaks in \lstlistings ).

However, on the other hand it looks really bad if consecutive long snippets with little text in between create very underfull vboxes and pages with only a few lines of text.

So basically I want to tell LaTeX that both extremes look rather bad, and that page breaks may be used sparingly to avoid bad cases of underfull vboxes but should be avoided/penalized. Right now I can totally ban page breaks or I can allow them, but then it is assumed they are "totally OK" and used a lot when they should be a last resort.

How can I fine-tune the badness of either alternative in order to get a visually pleasing result?

closed as unclear what you're asking by jub0bs, Mico, user13907, user31729, Benedikt Bauer Sep 13 '14 at 20:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Use the float option: \begin{lstlisting}[float]...\end{lstlisting}; the listing will not admit page breaks and will float to the best position. – Gonzalo Medina Feb 11 '14 at 18:23
  • @GonzaloMedina I have a lot of one-liners where it breaks the reading flow if I have to put a reference to a float and the reader has to search for the float. It might be a good idea to selectively allow the longer listings to float, I will try that. Floating will help the most in these cases anyways, and it will not break the reading flow that badly as the listing is longer. Thanks for the input! – mnagel Feb 13 '14 at 10:40
  • Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – jub0bs Sep 14 '14 at 12:30