TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
An example of what you've got so far would be useful. – egreg May 4 '12 at 13:15
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. – Martin Scharrer May 4 '12 at 13:19
\begin{minipage}{\textwidth} <minted+caption> \end{minipage} is then the way to go. – Martin Scharrer May 4 '12 at 16:40
@ms4py: Your bounty isn't helpful. A minimal working example is helpful! – Marco Daniel Jun 16 '12 at 11:04
@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 Jun 16 '12 at 19:50

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.
share|improve this answer
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 Jun 21 '12 at 10:25
Ok I see I had to restate the first point. What's with the rest? – Christian Jun 21 '12 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 Jun 21 '12 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 Jun 21 '12 at 12:18
So? No further idea? :( – schlamar Jun 22 '12 at 11:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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