Code spanning over two pages with minted, inside listing with caption

As far as I know, this is a fundamental restriction of floats. You cannot use floats that span over more than one page so you need to remove the surrounding listing.

To make the caption still work, you can load the caption package and use the \captionof command instead of \caption:

\inputminted{java}{code/JavaCode.java}
\captionof{listing}{Some caption}

Now the caption should also appear in the \listoflistings. If you want to use a label here, you need to put it inside the \captionof command:

\captionof{listing}{Some caption\label{lst:some-label}}

See also: Question “Label and caption without float”.


The suggested and accepted answer does not work correctly as the \captionof command requires to be inside an environment. This is pointed out in the caption documentation. For example in my case there was no proper vertical space after the caption.

A proper solution is to define a new environment:

\usepackage{caption}
% ...
\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{code}
\begin{minted}[frame=single]{py}
def my_func(x):
    print x
\end{minted}
\caption{My Func}
\label{lst:my_func}
\end{code}