If the float
package is not used, listing
'hi-jacks' the \@starttoc
command and 'pretends' to typeset the ToC
-- since this is all done in a group, the real ToC
is not affected by this.
tocloft
itself can't change such list of...
that are not provided by the package itself or one of the standard classes.
The following code changes the \l@lstlisting
command definition and inserts the word 'Program' before the listing number. \l@lstlisting
is doing the same like \l@section
etc.
The original definition in listings.sty
is
\def\l@lstlisting#1#2{\@dottedtocline{1}{1.5em}{2.3em}{#1}{#2}}
The 1.5em
width indicates the indentation, the 2.3em
is meant for the width of the number box. Since \l@lstlisting#1#2
is redefined within a \renewcommand
, I've to use ##1
and ##2
instead of #1
and #2
only.
\documentclass{book}
\usepackage{tocloft}
\usepackage{listings}
\makeatletter
\AtBeginDocument{%
\renewcommand\lstlistoflistings{\bgroup
\let\contentsname\lstlistlistingname
\def\l@lstlisting##1##2{\@dottedtocline{1}{1.5em}{2.3em}{\bfseries Program ##1}{##2}}
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lol}}%
\tableofcontents \egroup}
}
\makeatother
\begin{document}
\tableofcontents
\lstlistoflistings
\chapter{First}
\begin{lstlisting}[caption={My nice program},language=C]
#include<stdio.h>
int main(int argc,char **argv)
{
printf("Hello World!\n");
return(0);
}
\end{lstlisting}
\end{document}
tocloft
hooks into the standardToC
etc and thelist of ....
somethings that are generated bytocloft
itself, but not in afloat
package generatedToC
, which is the case forlistings
. You're out of luck here iftocloft
is applied directly.