I'm using a custom listing environment which does not float taken from Code spanning over two pages with minted, inside listing with caption. The caption is done with the caption package.

The problem is that under certain circumstances the caption is on another page than the complete listing. Is there any way to prevent this behavior?

Update Here is the minimal example. Just to clarify again: It is allowed to have pagebreaks in the code, but not between code and caption. So in the example the caption should either be also on page 1 or on page 2 there should be some lines of code.

  • 8
    An example of what you've got so far would be useful.
    – egreg
    Commented May 4, 2012 at 13:15
  • 6
    Should page breaks inside the code be allowed or not? If not than simply wrap the whole thing in a {minipage}{\textwidth} environment. Otherwise try a \nopagebreak macro before \caption. Note that listings should AFAIK have the caption on top not below. Commented May 4, 2012 at 13:19
  • 2
    \begin{minipage}{\textwidth} <minted+caption> \end{minipage} is then the way to go. Commented May 4, 2012 at 16:40
  • 6
    @ms4py: Your bounty isn't helpful. A minimal working example is helpful! Commented Jun 16, 2012 at 11:04
  • 7
    @ms4py Your bounty will most probably be lost, as nobody can really guess what you're doing. Rather than offering a bounty you had better providing an example. Notice that the author of minted did make a comment here and even he didn't have many clues for answering.
    – egreg
    Commented Jun 16, 2012 at 19:50

1 Answer 1


Things you can try:

  • Wrap the part of the environment that shouldn't experience a break in a minipage environment. Use \vspace{-\baselineskip}s at will.
  • Include the needspace package and then use \needspace{20\baselineskip} before the caption, where 20 is the number of lines your caption is long.
  • Plaster your code or macros with \nopagebreak[4]s all over the place (although you mentioned that this doesn't work so just sayin').
  • Use manual \pagebreaks
  • STFTeX.SE and see if you can find something that can be applied to your problem as well.
  • Just to make it clear (again): Pagebreaks should be allowed, but not between the end of the listing and the caption. I try to make an example.
    – schlamar
    Commented Jun 21, 2012 at 10:25
  • Ok I see I had to restate the first point. What's with the rest?
    – Christian
    Commented Jun 21, 2012 at 10:32
  • It should be a general solution, so the length of the source code is dynamic. I'm not sure how the rest of the points can apply in this case. I added an example, see the answer.
    – schlamar
    Commented Jun 21, 2012 at 11:21
  • I see, tough one. You have a kind of verbatim environment, every interruption of the environment is marked by horizontal bars and fancyvrb does some dark sorcery to prevent anything from standing in the same line as the end of the environment. That package is your enemy, not necessarily minted.
    – Christian
    Commented Jun 21, 2012 at 12:18
  • So? No further idea? :(
    – schlamar
    Commented Jun 22, 2012 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .