54

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?

0

3 Answers 3

34

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

7
  • Thanks! This works. Is there a way to also add a label so I can reference it from other part of my text? Also, if I use this, then then \listoflistings won't list my code right?
    – lander16
    Mar 1, 2011 at 20:05
  • @lander See updated answer. Mar 1, 2011 at 20:16
  • 2
    \captionof requires an environment so this does not work correctly, see my answer.
    – schlamar
    Apr 27, 2012 at 8:43
  • @ms4py Is right. Apr 27, 2012 at 8:51
  • I realize that minted is not really maintained anymore, but still thought I should mention that page-spanning breaks the moment you set a background colour. Sep 22, 2013 at 14:46
32

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}
3
  • 3
    When I use this solution with \inputminted, page spanning breaks. Sep 22, 2013 at 14:55
  • 2
    I am using \inputminted and it's running perfectly in my case. Are you sure you've included the caption package ?
    – Overdrivr
    Jan 11, 2017 at 9:40
  • In your example is better to use caption or captionof?
    – skan
    Sep 10, 2019 at 23:52
0

Adapting the solution by @Konrad Rudolph to something that is simple and doesn't break anything:

\inputminted[linenos]{python}{code.py}
\begin{figure}[H]
    \caption{Hello.}
    \label{fig:foo}
\end{figure}

Of course, this is going to be named and listed as a Figure, but that's not a problem for me.

5
  • 1
    You don't need to use a figure float. If \captionof doesn't work for you (which, weird!) you can (and should) simply use a listing float environment. May 29, 2016 at 8:34
  • The \captionof appeared to work, until I noticed that the paragraphs don't start with the usual indentation (and are restored to normal when I removed the \captionof). The listing environment doesn't split over two pages (I haven't tried mdframed for breaking a listing environment, because according to tex.stackexchange.com/a/103471/8666, mdframed helps with breaking in the presence of a background color).
    – 0 _
    May 29, 2016 at 21:43
  • No floating environment breaks across pages. I meant using the "correct" environment to put the title in. May 29, 2016 at 21:53
  • Interesting approach, thought its more like a very quick fix then an proper answer.
    – KcFnMi
    Apr 15, 2019 at 13:07
  • Sometimes the caption is placed on a different page. How can we fix it?
    – skan
    Jan 12, 2021 at 19:37

You must log in to answer this question.

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