[Tex/LaTex] Code spanning over two pages with minted, inside listing with caption

codefloatshighlightingmintedpage-breaking

I am trying using minted to display some code in my document, but the code is long and spans over two pages, this works fine, however when I place my code inside

\begin{listing}
  \inputminted{java}{code/JavaCode.java}
  \caption{Some caption}
  \label{label1}
\end{listing}

Then my code doesn't show up and I get the warning:

LaTeX Warning: Float too large for page

I have tried using the option H in listing, but then the code shows in only one page and it doesn't fit so I only see a part of it.

I also tried with the code directly in the latex document ie:

\begin{listing}
  \begin{minted}{java} 
    Java code
  \end{minted}
  \caption{Some caption}
  \label{label1}
\end{listing}

But the same happens, any ideas?

Best Answer

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”.