9

I'm using listings in my document, and its command \lstlistoflistings to generate the list of listings. The problem is that I've customized all the other lists (TOC, LOF and LOT). More precisely, I've changed the color of the page number, removed the leading dots and move the page number to appear right after the entry text (i.e. the page number is no longer right-aligned). Here's a complete example:

\documentclass[a4paper, 11pt]{memoir}

\usepackage{microtype}
\usepackage{textcase}
\usepackage{titlesec}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

% Define color theme
\newcommand{\titleColor}{\color[rgb]{0.616, 0.0627, 0.176}}
\newcommand{\partChapterNumColor}{\titleColor}
\newcommand{\tocPageColor}{\color[rgb]{0.0980, 0.329, 0.651}}

% Rules
\newcommand{\thinRule}{\rule{\textwidth}{0.25pt}}

% Customize heading appearance
\newcommand{\partSize}{\Huge}
\newcommand{\partStyle}{\lsstyle\scshape}
\newcommand{\chapterSize}{\huge}
\newcommand{\chapterStyle}{\lsstyle\scshape}
\newcommand{\chapterAfter}{\thinRule}
\newcommand{\sectionSize}{\Large}
\newcommand{\sectionStyle}{\lsstyle\scshape\MakeTextLowercase}
\newcommand{\subsectionSize}{}
\newcommand{\subsectionStyle}{\itshape}
\newcommand{\numberStyle}{\fontsize{60pt}{72pt}\selectfont\partChapterNumColor}
\titleformat{\part}[display]
            {\filcenter}
            {\numberStyle\thepart}
            {0pt}
            {\partSize\partStyle}
\titleformat{\chapter}[display]
            {}
            {\filleft\numberStyle\thechapter}
            {0pt}
            {\chapterSize\chapterStyle}
            [\chapterAfter]
\titleformat{\section}
            {\sectionSize}
            {\thesection\quad}
            {0pt}
            {\sectionStyle}
\titleformat{\subsection}
            {\subsectionSize}
            {\thesubsection\quad}
            {0pt}
            {\subsectionStyle}

% Customize "Table of ..." appearance
% Customize headings
\newcommand{\fixCtfHeadingBeforeSpacing}{\vspace*{1ex}} % Needed to make chapter
\newcommand{\fixCtfHeadingAfterSpacing}{\vspace*{0.5ex}} % layout coherent
\newcommand{\renewPrintXTitle}[1]{%
  \renewcommand{#1}[1]{%
    \fixCtfHeadingBeforeSpacing
    \chapterSize\chapterStyle ##1
    \fixCtfHeadingAfterSpacing
    \par\chapterAfter
  }
}
\renewPrintXTitle{\printtoctitle}
\renewPrintXTitle{\printlottitle}
\renewPrintXTitle{\printloftitle}

\renewcommand{\cftpartfont}{\partChapterNumColor\partStyle}
\renewcommand{\cftchapterfont}{\chapterStyle}
\renewcommand{\cftsectionfont}{}
\renewcommand{\cftsubsectionfont}{}
\renewcommand{\cftfigurefont}{}
\renewcommand{\cfttablefont}{}
\newcommand{\cftlistingfont}{}
% No leader dots
\renewcommand*{\cftpartdotsep}{\cftnodots}
\renewcommand*{\cftchapterdotsep}{\cftnodots}
\renewcommand*{\cftsectiondotsep}{\cftnodots}
\renewcommand*{\cftsubsectiondotsep}{\cftnodots}
\renewcommand*{\cftfiguredotsep}{\cftnodots}
\renewcommand*{\cfttabledotsep}{\cftnodots}
% Set page numbers immediately after entry text
\newcommand{\tocEntryPageSep}{\hspace{2em}}
\renewcommand{\cftpartleader}{\tocEntryPageSep}
\renewcommand{\cftpartafterpnum}{\cftparfillskip}
\renewcommand{\cftchapterleader}{\tocEntryPageSep}
\renewcommand{\cftchapterafterpnum}{\cftparfillskip}
\renewcommand{\cftsectionleader}{\tocEntryPageSep}
\renewcommand{\cftsectionafterpnum}{\cftparfillskip}
\renewcommand{\cftsubsectionleader}{\tocEntryPageSep}
\renewcommand{\cftsubsectionafterpnum}{\cftparfillskip}
\renewcommand{\cftfigureleader}{\tocEntryPageSep}
\renewcommand{\cftfigureafterpnum}{\cftparfillskip}
\renewcommand{\cfttableleader}{\tocEntryPageSep}
\renewcommand{\cfttableafterpnum}{\cftparfillskip}
% Customize page numbers
\newcommand{\tocPageStyle}{\tocPageColor}
\renewcommand{\cftpartpagefont}{\tocPageStyle}
\renewcommand{\cftchapterpagefont}{\tocPageStyle}
\renewcommand{\cftsectionpagefont}{\tocPageStyle}
\renewcommand{\cftsubsectionpagefont}{\tocPageStyle}
\renewcommand{\cftfigurepagefont}{\tocPageStyle}
\renewcommand{\cfttablepagefont}{\tocPageStyle}



\begin{document}

\tableofcontents
\lstlistoflistings

\section{new section}

bla bla bla

\begin{lstlisting}[caption=test code]
Test
\end{lstlisting}

\end{document}

Now I've figured out how to change the fonts by redefining one of listings's internal commands:

\makeatletter
\renewcommand*{\l@lstlisting}[2]{%
  \@dottedtocline{1}
                 {0em} % Indent
                 {\cftfigurenumwidth} % Listing number width
                 {\cftlistingfont#1} % Entry text font
                 {\tocPageStyle#2}% % Page number font
}
\makeatother 

But how do I remove the dots and move the placement of the page number? Also, it appears that the entries in the LOL appears slightly higher up in the document compared to the other lists.

Also note that I'm using memoir, if that's of any use.

lockstep
  • 250,273
gablin
  • 17,006

3 Answers3

11

You have to register the list of listings to tocloft; I've found this to work

\makeatletter
\begingroup\let\newcounter\@gobble\let\setcounter\@gobbletwo
  \globaldefs\@ne \let\c@loldepth\@ne
  \newlistof{listings}{lol}{\lstlistlistingname}
\endgroup
\let\l@lstlisting\l@listings
\AtBeginDocument{\addtocontents{lol}{\protect\addvspace{10\p@}}}
\makeatother
\renewcommand{\lstlistoflistings}{\listoflistings}
\renewcommand{\cftlistingsfont}{\itshape}

The last line is just to show that commands of the type \cftlistings... are available for customizing the entries' appearance exactly in the same way as the normal ones.

The "slightly higher up" business depends on the fact that the standard classes issue \addtocontents{lof}{\protect\addvspace{10\p@}} and the same for lot; so we do the same for lol.

You can typeset the list either by \lstlistoflistings or \listoflistings.

FOR memoir

When memoir is used, one has to act in a slight different way

\begingroup\makeatletter
\let\newcounter\@gobble\let\setcounter\@gobbletwo
  \globaldefs\@ne \let\c@loldepth\@ne
  \newlistof{listings}{lol}{\lstlistlistingname}
  \newlistentry{lstlisting}{lol}{0}
\endgroup

Now one can customize the appearance of entries in the list of listings with commands of the form \cftlstlisting...; these should typeset the list of listings exactly as the others.

\renewcommand{\cftlstlistingdotsep}{\cftnodots}
\renewcommand{\cftlstlistingleader}{\tocEntryPageSep}
\renewcommand{\cftlstlistingafterpnum}{\cftparfillskip}
\renewcommand{\cftlstlistingpagefont}{\tocPageStyle}

In the case of memoir no vertical adjustment at the beginning of the lists seems to be necessary.

Addition

In order to add a separation in the "list of listings" for listings in different chapters, do

\makeatletter
\renewcommand*{\insertchapterspace}{%
  \addtocontents{lof}{\protect\addvspace{10pt}}%
  \addtocontents{lot}{\protect\addvspace{10pt}}%
  \addtocontents{lol}{\protect\addvspace{10pt}}}
\makeatother

or, more simply,

\makeatletter
\g@addto@macro\insertchapterspace
  {\addtocontents{lol}{\protect\addvspace{10pt}}}
\makeatother
egreg
  • 1,121,712
  • I tried copy-pasting the code into my document, but I get an error saying that lstlistoflistings is undefined (however that now happened). Could you please be so kind and use my complete example (which I just included) and modify it such that the List of Listings appears as the Table of Contents? – gablin Sep 06 '11 at 10:52
  • The vertical adjustment is needed. How do I include that? – gablin Sep 06 '11 at 12:53
  • It doesn't in my test based on your example; I only added a figure environment and \listoffigures. – egreg Sep 06 '11 at 12:58
  • Hm, it does in my real document... I tried adding \AtBeginDocument{\addtocontents{lol}{\protect\addvspace{10\p@}}} from your previous example but that just messed up the whole thing (although it did the vertical alignment). – gablin Sep 06 '11 at 13:02
  • Did you add it between \makeatletter and \makeatother? – egreg Sep 06 '11 at 13:18
  • No, I did not. And now it looks perfectly! ^^ Thanks! – gablin Sep 06 '11 at 13:29
  • I just noticed that the other listings have a vertical space between listings belonging to the same chapter, whereas the "List of Listings" defined here does not. Is it possible to fix that? – gablin Sep 12 '11 at 15:44
  • @gablin: see edited answer – egreg Sep 12 '11 at 15:51
  • That did it! Wish I could upvote this answer more. – gablin Sep 13 '11 at 06:24
3

The titletoc package works for any contents generated by \contentsline and \numberline. For a new type of contents, just use

\contentsuse{<content type>}{<content file extension>}

Then we can use \titlecontents command to customize the list just like ToC/LoF/LoT's. It's much easier than patching tocloft package.

And here is an example for \lstlistoflistings:

\documentclass{article}
\usepackage{listings}
\usepackage{titletoc}

\contentsuse{lstlisting}{lol}
\titlecontents{lstlisting}[2em]
              {\addvspace{0.25pc}}
              {\textbf{Code \thecontentslabel} }
              {}
              {\titlerule*[0.5em]{$\cdot$}\contentspage}
              []

\begin{document}

\lstlistoflistings

\begin{lstlisting}[caption=Test]
Some code
\end{lstlisting}

\end{document}
Leo Liu
  • 77,365
3

put this before \begin{document}

\makeatletter
    
\let\l@lstlisting\l@section
    
\makeatother 
    
  • This actually worked quite well. The only problem is that the entries are indented. – gablin Sep 06 '11 at 13:30
  • then copy the definition of \l@section and rename it to \l@lstlisting} and redefine the values. –  Sep 06 '11 at 13:35