Text Formattinglistings | Customize List of Listings

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

Is it possible to change the style used for the list of listings? This is highly desirable for consistency with the list of figures. The listings documentation doesn't say anything about this.

Currently, it's looking like this (the caption's name isn't used) :

1 <caption content>
2 <caption content>
...

For consistency with the list of figures, I would like the list of listings to be like this :
Code 1 : <caption content>
Code 2 : <caption content>

How can we do this ?

EDIT : Here's a compilable example of a list of listings :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{subfig}
\usepackage{listings}
\renewcommand*{\lstlistingname}{Code \arabic{lstlisting} de \textsl{Mathematica}}
\renewcommand*{\lstlistlistingname}{Table des codes de \textsl{Mathematica}}
\captionsetup[lstlisting]{margin=0cm,format=hang,font=small,format=plain,labelfont={bf,up},textfont={it}}
\captionsetup[lstlistoflistings]{\lstname Code \arabic{lstlisting} :}
\usepackage{chngcntr}
\usepackage{tocloft}

\begin{document}
\counterwithout{lstlisting}{chapter}

\chapter{Test}
Test
\lstset{}
\chapter{Test}
\begin{lstlisting}[caption={A simple caption}]
 x := -2 + y
 \end{lstlisting}

\begin{lstlisting}[caption={Another caption}]
 x := -2 + z^2
 \end{lstlisting}

\chapter{Another chapter}
Bla bla bla
\begin{lstlisting}[caption={And yet another caption}]
 x := -2
 \end{lstlisting}

%\renewcommand{\lstlistoflistings}{\thelstlisting }
\lstlistoflistings

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

You could redefine \l@lstlisting which prints a line in the list of listings.

For example:

Code: Select all

\makeatletter
\renewcommand\l@lstlisting[2]{\@dottedtocline{1}{1.5em}{1em}{Code~#1}{#2}}
\makeatother
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

Well, it's working, but how do you add the ":" just after the code number, before the caption ?

The figure list looks like this :

Figure 2.1.1 : <caption>
Figure 2.1.2 : <caption>
Figure 2.2.1 : <caption>

while the current list of listings (with your suggestion) looks like this :

Code 1 <caption>
Code 2 <caption>
Code 3 <caption>
...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

To be consistent, I would define the representation of the listings counter to be always Code x.

Here's some code with further adjustments in this regard. \numberline, which prints the number in the list lines, is slightly modified to add the :.

Code: Select all

\renewcommand*{\lstlistingname}{\textsl{Mathematica}}
\makeatletter
\renewcommand{\numberline}[1]{\hb@xt@\@tempdima{#1:\hfil}}
\renewcommand\l@lstlisting[2]{\@dottedtocline{1}{1.5em}{4em}{#1}{#2}}
\makeatother
\begin{document}
\counterwithout{lstlisting}{chapter}
\renewcommand*{\thelstlisting}{Code~\arabic{lstlisting}}
listings.png
listings.png (8.34 KiB) Viewed 18340 times
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: listings | Customize List of Listings

Post by Cham »

Wow !

There was no way I could find this by myself !

Thanks a lot, Stefan, it's working !
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: listings | Customize List of Listings

Post by Cham »

Stefan,

my list of listings doesn't show up in the ToC. How can I tell LaTeX to add it there ?

And when I add the code above to my main book, I'm getting into troubles. I'll describe them later.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

Use

Code: Select all

\addcontentsline{toc}{chapter}{List ot listings}
for this, at the page where the list starts.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: listings | Customize List of Listings

Post by Cham »

Now, this is turning into a nightmare !

The list of listings is now off by a few pages, according to the ToC.

And I'm still unable to add your code above... I'm having some serious compilation errors.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Customize List of Listings

Post by Stefan Kottwitz »

Cham wrote:The list of listings is now off by a few pages, according to the ToC.
It only depends on where you place that \addcontentsline command. Just ensure that it's on the corresponding first page of the list of listings. For example, if the list of listings starts on a new page and you write \addcontentsline before it, use \clearpage before \addcontentsline.
Cham wrote:I'm having some serious compilation errors.
The first one is usually important. Further error message may be just a consequence of the first error. I guess it will be cleare after you made an MWE.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Customize List of Listings

Post by Cham »

I think I isolated the source of the compilation problem with your code suggestion above. Please, try the code below. Desactivating the hyperref package solves the compilation issue.

However, how can we solve the conflict without desactivating the very important hyperref ?

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[]{hyperref}  % interfere with listings
\usepackage{listings}
\renewcommand*{\lstlistlistingname}{Table des codes de \textsl{Mathematica}}
\usepackage{chngcntr}

\begin{document}

\tableofcontents

\renewcommand*{\lstlistingname}{\textsl{Mathematica}~:}
\makeatletter
\renewcommand{\numberline}[1]{\hb@xt@\@tempdima{#1~:~\hfil}}
\renewcommand\l@lstlisting[2]{\@dottedtocline{1}{1.5em}{4em}{#1}{#2}}
\makeatother
\counterwithout{lstlisting}{chapter}
\renewcommand*{\thelstlisting}{Code~\arabic{lstlisting}}

\chapter{Test}
\begin{lstlisting}[caption={A simple caption}]
 x := -2 + y
 \end{lstlisting}

\lstlistoflistings

\end{document}
EDIT : I've found that adding the option hypertexnames=false to the hyperref package may solve the previous issue, but then I have a problem with my Index entries, which are pointing to the wrong page (off by 8 pages !). What's happening here ?

Without the hypertexnames=false option, the index was working correctly.

Is there a way out of this madness ?
Post Reply