[Tex/LaTex] mixing minted with lstlisting

listingsminted

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?

Best Answer

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}}