Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using minted for source code highlighting. I want a List of Listings, and I want the List of Listings to itself have an entry in the main Table of Contents. I thought I could do this with tocbibind package, and tried to follow the instructions on renewing \lstlistoflistings (given on p5 of the tocbibind documentation), adapting that code (or so I thought) to work for the minted instead of listings package. When I do this (see preamble of MWE below), I get the TOC line, but the List of Listings formatting goes wonky (all entries overlapping on the same line, no row of dots, etc. Obviously I've left some stuff out of my \renewcommand but I'm not sure what.

MWE:

\documentclass{report}
\usepackage[nottoc,chapter]{tocbibind}
\usepackage[chapter]{minted} 
\usemintedstyle{default}

% If I comment out the next 3 lines, the list of listings looks 
% normal, but there is no "list of listings" line in the TOC
\renewcommand{\listoflistings}{\begingroup
\tocfile{\listoflistingscaption}{lol}
\endgroup}

\begin{document}
\tableofcontents
\listoflistings

\chapter{the first chapter}
\section{section one point one}
\begin{listing}
\begin{minted}[fontsize=\small, tabsize=2]{r}
x <- runif(10)
y <- rnorm(10)
plot(x, y, axes=FALSE, ann=FALSE, pch=16)
\end{minted}
\caption[short caption for lst1]{Longer caption to be used when listing one appears\label{lst:myLstOne}}
\end{listing}

\chapter{the second chapter}
\section{section two point one}
\begin{listing}
\begin{minted}[fontsize=\small, tabsize=2]{r}
a <- runif(5)
b <- rnorm(5)
plot(b, a)
\end{minted}
\caption[short caption for lst2]{Longer caption to be used when listing two appears\label{lst:myLstTwo}}
\end{listing}

\end{document}

EDIT: I can get a properly formatted LOL, and a LOL entry in the TOC, if I change the relevant part of the preamble to this:

\renewcommand{\listoflistings}{\begingroup
\listof{listing}{\listoflistingscaption}
\tocfile{\listoflistingscaption}{lol}
\endgroup}

I got the idea by looking for the definition of \listoflistings in the source code of minted. However, then I end up with a second (empty) LOL after the first (correct) one.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The common way to add an unnumbered section or chapter heading to the ToC is:

\addcontentsline{toc}{<secunit>}{<heading>}

This would suggest a redefinition like:

\renewcommand{\listoflistings}{%
  \addcontentsline{toc}{chapter}{\listoflistingscaption}%
  \listof{listing}{\listoflistingscaption}%
}

However, since \listof will issue a chapter heading this implicitly also calls \cleardoublepage which means you end up with the wrong page number in the ToC as \addcontentsline is expanded before. Placing it after \listof... also will fail if the list is more than a page long so the correct redefinition should explicitly call \cleardoublepage first itself:

\renewcommand{\listoflistings}{%
  \cleardoublepage
  \addcontentsline{toc}{chapter}{\listoflistingscaption}%
  \listof{listing}{\listoflistingscaption}%
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.