3

After learning on the net I managed to get this type of list of figures :

enter image description here And I want exactly the same with the list of listings, but here is what I get : enter image description here My code is this one :

\documentclass[12pt,a4paper]{book}

% For french
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

% For figures
\usepackage{caption}
\usepackage{subcaption}
\addto\captionsfrench{%
\renewcommand{\listfigurename}{Liste des figures}% 
\renewcommand{\listtablename}{Liste des tables}%
}
\usepackage{tocloft}
\renewcommand{\cftfigfont}{\textsc{Figure} }
\renewcommand{\cftfigaftersnum}{~~--}
\renewcommand{\cftfignumwidth}{3em}

% Numbering
\makeatletter
\renewcommand{\thefigure}{\ifnum \c@chapter>\z@ \thechapter.\fi
\@arabic\c@figure}
\@addtoreset{figure}{chapter}
\makeatother

% Listings
\usepackage{listings}
\addto\captionsfrench{%
  \renewcommand{\lstlistlistingname}{Liste des codes}%
}

%Refs
\usepackage{hyperref}

% Begining of the document
\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{Introduction}

\begin{figure}
\centering
A figure
\caption{Caption}
\label{fig:draw}
\end{figure}

\begin{lstlisting}[caption=Hello,label={hello}]
A code
\end{lstlisting}

\chapter{Conclusion}

\begin{figure}
\centering
 A figure
\caption{Other caption}
\label{fig:drawbis}
\end{figure}

\begin{lstlisting}[caption=Good bye,label={bye}]
Another code
\end{lstlisting}

\backmatter

\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures

\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\lstlistlistingname}
\lstlistoflistings

\end{document}

All I need to write is the analog of the code :

\usepackage{tocloft}
\renewcommand{\cftfigfont}{\textsc{Figure} }
\renewcommand{\cftfigaftersnum}{~~--}
\renewcommand{\cftfignumwidth}{3em}

but for the listings. Is there anyway to do it with tocloft ? If not how can it be done ?

Many thanks.

1
  • tocloft does not hook into \lstlistoflistings, unfortunately
    – user31729
    Apr 28, 2016 at 14:22

2 Answers 2

4

By default tocloft can't hook into ToCs that are made by other packages, however, it's possible to use \newlistof{...} to redefine the \lstoflistings -- unfortunately, this requires knowledge of the counter being used for listings (its name is lstlisting) and \newlistof... tries to define a new counter for this -- this must fail (and is a design flaw of tocloft, in my point of view)

However, it's possible to \let\c@lstlisting\relax, after this \newlistof works.

Use the optional argument of \newlistof for the resetting counter to get correct hyperlinks.

\documentclass[12pt,a4paper]{book}

% For french

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

% For figures
\usepackage{caption}
\usepackage{subcaption}
\addto\captionsfrench{%
  \renewcommand{\listfigurename}{Liste des figures}% 
  \renewcommand{\listtablename}{Liste des tables}%
}
\usepackage{tocloft}
\renewcommand{\cftfigfont}{\textsc{Figure} }
\renewcommand{\cftfigaftersnum}{~~--}
\renewcommand{\cftfignumwidth}{3em}

% Numbering
\makeatletter
\renewcommand{\thefigure}{\ifnum \c@chapter>\z@ \thechapter.\fi
\@arabic\c@figure}
\@addtoreset{figure}{chapter}
\makeatother

% Listings
\usepackage{listings}

\makeatletter

 \AtBeginDocument{%
  \let\c@lstlisting\relax
  \newlistof[chapter]{lstlisting}{lol}{\lstlistlistingname}
  \renewcommand{\cftlstlistingindent}{20pt}
  \renewcommand{\cftlstlistingfont}{\textsc{Listing} }
  \renewcommand{\cftlstlistingaftersnum}{~~--}
  \renewcommand{\cftlstlistingnumwidth}{3em}
  \renewcommand{\theHlstlisting}{listings.\thelstlisting}
}
\makeatother



\addto\captionsfrench{%
  \renewcommand{\lstlistlistingname}{Liste des codes}%
}

\renewcommand{\lstlistoflistings}{%
  \listoflstlisting
}
%Refs
\usepackage{hyperref}

% Begining of the document

\begin{document}


\frontmatter
\tableofcontents
\lstlistoflistings
\listoffigures

\mainmatter


\chapter{Introduction}

\begin{figure}
\centering
A figure
\caption{Caption}
\label{fig:draw}
\end{figure}

\begin{lstlisting}[caption=Hello,label={hello}]
A code
\end{lstlisting}

\chapter{Conclusion}

\begin{figure}
\centering
 A figure
\caption{Other caption}
\label{fig:drawbis}
\end{figure}

\begin{lstlisting}[caption=Good bye,label={bye}]
Another code
\end{lstlisting}

\backmatter

\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\listfigurename}
\listoffigures

\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\lstlistlistingname}
\lstlistoflistings

\end{document}

enter image description here

3
  • The hyperanchors are not correct yet!
    – user31729
    Apr 28, 2016 at 14:48
  • Also the items in the LOL are not spaced just as in LOF but it is fixed with : \renewcommand{\cftlstlistingfont}{\vspace{.3cm}\textsc{Listing} } instead of \renewcommand{\cftlstlistingfont}{\textsc{Listing} } Apr 28, 2016 at 14:50
  • @JohnMarjan: Well, I leave that to you, the hyperlinks are working now with the update
    – user31729
    Apr 28, 2016 at 14:54
3

In case that tocloft.sty is not a must, I can offer a solution with titletoc.sty. I think the interface is cleaner. There is also a solution at the end how to get rid of the additional vertical space in LOF.

\documentclass[12pt,a4paper,draft,openany]{book}

% For french
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}

% For figures
\usepackage{caption}
\usepackage{subcaption}
\addto\captionsfrench{%
\renewcommand{\listfigurename}{Liste des figures}% 
\renewcommand{\listtablename}{Liste des tables}%
}

% Listings
\usepackage{listings}
\addto\captionsfrench{%
  \renewcommand{\lstlistlistingname}{Liste des codes}%
}

\usepackage{titletoc}
\contentsuse{lstlisting}{lol}

\titlecontents{figure}
  [2.6cm]
  {}
  {\contentslabel[\textsc{Figure}~\thecontentslabel~--]{2.6cm}}
  {\hspace*{0em}}
  {\titlerule*[1pc]{.}\contentspage}

\titlecontents{lstlisting}
  [2.65cm]
  {}
  {\contentslabel[\textsc{Listing}~\thecontentslabel~--]{2.65cm}}
  {\hspace*{0em}}
  {\titlerule*[1pc]{.}\contentspage}

%Refs
\usepackage{hyperref}

% Begining of the document
\begin{document}

\frontmatter
\tableofcontents

\mainmatter
\chapter{Introduction}

\begin{figure}
  \centering
  A figure
  \caption{Caption}
  \label{fig:draw}
\end{figure}

\begin{lstlisting}[caption=Hello,label={hello}]
A code
\end{lstlisting}

\chapter{Conclusion}

\begin{figure}
  \centering
  \fbox{A figure}
  \caption{Other caption}
  \label{fig:drawbis}
\end{figure}

\begin{lstlisting}[caption=Good bye,label={bye}]
Another code
\end{lstlisting}

\backmatter

\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{\listfigurename}
\begingroup
  \makeatletter
  \let\addvspace\@gobble
  \listoffigures
\endgroup

\clearpage
\phantomsection
\addcontentsline{toc}{chapter}{\lstlistlistingname}
\lstlistoflistings

\end{document}

enter image description here

1
  • Great answer. Good to now in case of option clashes :) Apr 28, 2016 at 16:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .