I want to create a listing using minted that has background colour and can be split across pages. The usual advice in that case is to use the mdframed environment and I have written a command that follows this advice


But in that case I can't seem to be able to easily specify that I want the caption to be kept on the same page the listing is on and the samepage environment would keep the listing from splitting across the pages, which is not what I want.

Is there any solution to tie the caption to the bottom the the listing so they always appear on the same page that none the less lets the listing break?

\clojurefile command is generated with this \newmintedfile call:


EDIT2: I made a minimal example to demonstrate what's wrong: https://gist.github.com/jaen/68c6fe78c83d5d58721f

Ch1 is a good behaviour - minted breaks, caption starts next to listing end,
Ch2 is with samepage and behaves wrongly - minted doesn't break (though captions starts where it should),
Ch3 has longer code - minted breaks as I expected, but the caption is pushed to the next page and does not start immediately afterthe listing,
Ch4 is samepage again - it both doesn't let minted break and pushes the caption to the next page,
Ch5 is a longer code sample still - again minted breaks, but the caption is pushed to the next page,
Ch6 is samepage and commented out, because if you uncomment it, it won't even compile,seems it enters an infinite loop of trying to fit the code + caption on one page and it can't.

What I want is to get a behaviour where a) minted breaks across pages, b) the caption always starts next to the end of the listing and is not pushed to the next page.
And I can't seem to find one. Anyone?

  • 1
    Wouldn't the desired layout be easier achieved with tcolorbox? (Just a guess – I haven't actually tried it. tcolorboy has a minted library for integrated minted support, though…)
    – cgnieder
    Commented Mar 11, 2017 at 13:28

1 Answer 1


Here's a tcolorbox solution (only relevant code as your MWE is not minimal). I have removed the \usepackage{mdframed} and instead added the following:


That uses the package tcolorbox and enables you to have breakable boxes. They are used within your commands (I did not understand what the aim of your samepage version is). The following example typesets such a box (have a look into the documentation of tcolorbox to see how to customize the appearance. I've put the caption into the lower part of the box. This is a design decision, you can of course change it the way you want.


You must log in to answer this question.

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