4

Using minted, I'm typesetting listings that are longer than one page. However, using the listing environment that comes with minted does not work, because it does not break at the and of a page. I have found a possible solution by using captionof so it still appears in the list of listings.

However, after using it, the parskip is reset and the option half is not used anymore, for any text below it.

\documentclass[
    12pt,
    paper=a4,
    parskip=half ]{scrreprt}

\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage[ngerman]{babel} 
\usepackage{libertine} 
\usepackage[libertine,cmintegrals,cmbraces,vvarbb]{newtxmath} 
\usepackage{inconsolata} 
\usepackage{minted} 
\usepackage{caption} 
\usepackage{lipsum}

\begin{document}
    \lipsum
    \begin{minted}{python}
        def foo():
            # Here would be code longer than one page
            return 0
    \end{minted}
    \captionof{listing}{Some Text}
    \lipsum 
\end{document}
jub0bs
  • 58,916
ap0
  • 329

1 Answers1

4

You get a warning:

Package caption Warning: \captionsetup{type*=...} or \captionof
(caption)                outside box or environment on input line 24.
See the caption package documentation for explanation.

This is the cause of problem you are facing. As a remedy to this put minted inside a group

\bgroup
    \begin{minted}{python}
        def foo():
            # Here would be code longer than one page
            return 0
    \end{minted}
    \captionof{listing}{Some Text}
\egroup

Your code:

\documentclass[
    12pt,
    paper=a4,
    parskip=half ]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage{libertine}
\usepackage[libertine,cmintegrals,cmbraces,vvarbb]{newtxmath}
\usepackage{inconsolata}
\usepackage{minted}
\usepackage{caption}
\usepackage{lipsum}

\begin{document}
    \lipsum
    \bgroup
    \begin{minted}{python}
        def foo():
            # Here would be code longer than one page
            return 0
    \end{minted}
    \captionof{listing}{Some Text}
    \egroup
    \lipsum
\end{document}

enter image description here

As an aside KOMA defines a similar command

\captionof{float type}[entry]{title}

and you don't need to load caption package.

  • I would extend the solution with the KOMA command. – Manuel Jan 22 '15 at 13:42
  • @Manuel It is just removing \usepackage{caption} from the code. –  Jan 22 '15 at 13:51
  • I have added the caption package to get the labels in bold. – ap0 Jan 22 '15 at 13:53
  • @ap0 OK. That was meant just for your information. –  Jan 22 '15 at 13:54
  • @HarishKumar Yeah, but I meant that it's more likely that people start using KOMA's \captionof if you put it in a full answer instead of just commenting about it. – Manuel Jan 22 '15 at 13:54
  • @ap0 \addtokomafont{captionlabel}{\bfseries}? – Manuel Jan 22 '15 at 13:56
  • @ap0 That too is possible with KOMA itself. See Manuel's comments. Bottom line is: whenever KOMA offers something it is better to use them. –  Jan 22 '15 at 14:01
  • @Manuel, thank you. But I do not quite understand why your solutions should be prefered to \usepackage[labelfont=bf]{caption}. – ap0 Jan 22 '15 at 14:11
  • @ap0 Because some times they may clash. :-) –  Jan 22 '15 at 14:15
  • @ap0 Because the author of KOMA-script takes high care of his own classes and packages. A more generic package like caption doesn't know all the features by KOMA and might break stuff. It's like understanding relativity, the special approach is the simplest, the more generic solution is a lot harder to get. – Johannes_B Jan 22 '15 at 14:28
  • Additional remark: if you have an element that spans over multiple pages, place the caption above the element. Also consider this: http://tex.stackexchange.com/questions/3243/why-should-a-table-caption-be-placed-above-the-table as code is text-like and not image-like. – MaxNoe Jan 22 '15 at 18:43