3

I'm using the \captionof command from the caption package to add captions to listings produced with \inputminted. E.g.

\documentclass{report}

\usepackage{caption}
\usepackage{minted}

\begin{document}

\chapter{The first chapter}

\captionof{listing}{some code}
\begin{minted}{C}
#define X 1
\end{minted}

\chapter{The second chapter}

\captionof{listing}{some more code}
\begin{minted}{C}
#define Y 2
\end{minted}

\end{document}

This seems to be the only way to add captions to listings produced by minted that still allows listings to span multiple pages. What annoys me greatly is that I am not able to figure out how to number the resulting listings hierarchically. They show up as Listing 1 and Listing 2 but I'd rather have them numbered Listing 1.1, Listing 2.1 and so forth according to the chapter they are in (akin to numbering of floats). How can I achieve this?

1 Answer 1

3

\captionof{listing}{...} uses the macro \thelisting in order to show the listing number. If this should be changed and the listing number reset by chapter number use \counterwithin{listing}{chapter} from chngcntr package (or after the LaTeX kernel update, it is contained in the Kernel)

\documentclass{report}
\usepackage{chngcntr}% Only needed unless using new LaTeX kernel update from early of April 2018
\usepackage{caption}
\usepackage{minted}
\counterwithin{listing}{chapter}
%\renewcommand{\thelisting}{\thechapter.\arabic{listing}}

\begin{document}

\chapter{The first chapter}



\captionof{listing}{some code}
\begin{minted}{C}
#define X 1
\end{minted}

\chapter{The second chapter}

\captionof{listing}{some more code}
\begin{minted}{C}
#define Y 2
\end{minted}

\end{document}
1
  • That's exactly what I was looking for, thank you!
    – Peter
    Apr 16, 2018 at 10:13

You must log in to answer this question.

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