[Tex/LaTex] Customizing the list of listings from minted

formattinglistingsmintedtable of contentstocloft

This question is in regards to customizing the format of the list of listings produced with the minted package. In particular, I want to replicate the formatting produced via the tocloft for Figures:

enter image description here

Despite being related to the following two questions:

(1) Formatting \listoftables and \listofalgorithms;

(2) Customizing the list of listings generated by \lstlistoflistings?,

I have been unable to replicate the formatting of the list of figures for list of listings from minted. This question differs from these two examples in that minted uses the float package to create the list of listings, whereas the above two questions address the algorithm and listing packages, respectively.

An attempt to produce a similar result follows.

\documentclass{report}
\usepackage{minted}
\usepackage[titles]{tocloft}


\makeatletter
\begingroup
  \let\newcounter\@gobble
  \let\setcounter\@gobbletwo
  \globaldefs\@ne
  \let\c@loadepth\@ne
  \newlistof{listings}{lol}{\listoflistingscaption}
\endgroup
\let\l@listings\l@listing
\makeatother



\renewcommand{\cftfigpresnum}{Figure }
\cftsetindents{fig}{0em}{5em}

\renewcommand{\listoflistingscaption}{List of Listings}
\renewcommand{\cftlistingspresnum}{Listing }
\cftsetindents{listings}{0em}{5em}


\begin{document}

\listoffigures
\listoflistings
\clearpage

A minted environment: 

\begin{listing}
\begin{minted}{java}
// Java class
\end{minted} 
   \caption{A Java Class.}
\end{listing} 

A Figure: 

\begin{figure}[h]
\centering
 \rule{1cm}{1cm} \caption{A Figure}
\end{figure}

\end{document}

This approach yields the unsatisfactory list:

enter image description here

Any suggestions would be greatly appreciated.

Best Answer

I think you are better off if you load minted.sty with option newfloat and use the interface to tocloft.sty as described in newfloat.pdf. Code:

\documentclass{article}

\usepackage[titles]{tocloft}
\newlistof{listing}{lol}{List of Listings}

\usepackage[newfloat]{minted}
\usepackage{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{%
  name={Listing},
  fileext=lol}

\renewcommand{\cftfigpresnum}{Figure~}
\setlength{\cftfigindent}{0pt}
\setlength{\cftfignumwidth}{2cm}

\renewcommand{\cftlistingpresnum}{Listing~}
\setlength{\cftlistingnumwidth}{2cm}

\begin{document}

\listoffigures
\listoflistings

\begin{listing}
\caption{A Java Class}
\begin{minted}{java}
// Java class
\end{minted} 
\end{listing}

Reference to \ref{code:c-code}.

\begin{code}
\captionof{listing}{My C-Code}
\label{code:c-code}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{code}

\begin{figure}[h]
\centering
 \rule{1cm}{1cm} \caption{A Figure}
\end{figure}

\end{document}

enter image description here

Related Question