7

I have a List of Listings that looks like this:

enter image description here

The first listing, which is correct, uses lstlisting. 3.1 is the correct chapter.

The second listing uses minted. Somehow it shows 1 as chapter, which should actually be 3.2.

This is my command for the List of Listings:

\renewcommand{\lstlistlistingname}{List of Listings}
\addcontentsline{toc}{chapter}{\listoflistingscaption}%
\listof{listing}{\listoflistingscaption}%

This is the first code:

\begin{lstlisting}[language=xml, frame=single, caption={"Segment of the XML input file"}, label={lst:xmlfile}]
    some code
\end{lstlisting}

This is the second:

\begin{listing}[H]
    \begin{minted}{c++}
        some code
    \end{minted}
    \caption{"Conversion functions for DirID and ID"}
    \label{lst:conversion}
\end{listing}

What do I need to change here so that it shows up correctly with the right chapter number?

1 Answer 1

3

The first thing you need is \usepackage[chapter]{minted}. This sets minted to use <chapter>.<listing> style numbering in the list of listings.

At this point, listings from the two packages will have the same format in the list of listings, but each will be using its own number scheme. They need to share a common counter to fix this. A simple solution is to modify the listing environment from minted so that it actually uses the lstlisting counter. This relies on the etoolbox package, which is automatically loaded by recent versions of minted. The code can be inserted in the preamble after minted is loaded.

\AtBeginEnvironment{listing}{\setcounter{listing}{\value{lstlisting}}}
\AtEndEnvironment{listing}{\stepcounter{lstlisting}}
3
  • Only adding [chapter] did not work for me, however I got it working when I included etoolbox and added the two lines you mentioned. There is one error though: The captions both say "Listing 3.1 ..." even though the second one should say "Listing 3.2 ..." and in the list of listings it shows correctly. Sep 26, 2015 at 16:34
  • @gartenriese Can you edit your question to add a minimal example of what you have now, that has the incorrect caption numbers? All the captions are correct for me, so I'm wondering if we have something different in our preambles that is changing things.
    – G. Poore
    Sep 26, 2015 at 17:33
  • I figured it out. The problem was that I wasn't using a listing with the first one but with the second one. Now I'm not using a listing for either and it works fine. I think it's better to only use one environment throughout the whole document anyways. Thanks! Sep 27, 2015 at 19:06

You must log in to answer this question.

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