Page Layoutlistings | Numbering of Listings

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Numbering of Listings

Post by Cham »

I'm having a small problem with the numerotation of listings from the Appendix, as shown in the list of listings.

My listings style is defined i the preamble as this :

Code: Select all

\usepackage{listings}
\renewcommand\lstlistingname{Code \textsl{Mathematica}}
\renewcommand*{\lstlistlistingname}{Table des codes de \textsl{Mathematica}}
Currently, my appendix is defined at the end of the document (before the list of listings and of figures), as a chapter with a special name. I'm using this code for its definition in the preamble :

Code: Select all

\appendix %
	\pagestyle{appendices}
	\renewcommand\thesection{A.\arabic{section}}
	\input{Appendices}
Then, I add a list of listings and figures after the previous code :

Code: Select all

\newpage
\setlength{\cftfignumwidth}{3.5cm}
\renewcommand{\cftfigpresnum}{\figurename\ }
\renewcommand{\cftfigaftersnum}{~:}

\pagestyle{figures}

\lstlistoflistings % The list of listings
\listoffigures % The list of figures
The numerotation of all figures (including the ones from the appendix) are all fine in the list of figures. The numerotation of listings in the list of listings are all fine too, but only if the listing is from a normal chapter. When the listing is from the appendix, its number is unsatisfaying. It's missing the "A" (for Appendix) and shows a simple number.

I would prefer to define the listing's number as a simple number from 1 to the last one in the whole book, without any reference to the chapter or the section. How can I do that ?

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

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Numbering of Listings

Post by Cham »

In the preamble, using the listing setup shown below doesn't remove the chapter's number in the listing's label.

Code: Select all

\captionsetup[lstlisting]{numberbychapter=false}
How can I tell LaTeX to use a simple number from 1 to the last listing in the document, instead of something like chapter.number ?

Here's a compilable example of a few listings, with the chapter's number shown in the label. I would like to get rid of it but I don't know how.

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 \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}}

\begin{document}

\chapter{Test}
Test
\lstset{
	tabsize=4,frame=single,language=mathematica,basicstyle=\scriptsize\ttfamily,keywordstyle=\color{black},commentstyle=\color{gray},showstringspaces=false}

\begin{lstlisting}[caption={Title}]
 x := -2
 \end{lstlisting}

\begin{lstlisting}[caption={Title}]
 x := -2
 \end{lstlisting}

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

\lstlistoflistings

\end{document}
Again, the setup command numberbychapter=false isn't recognised.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: listings | Numbering of Listings

Post by Cham »

Nobody knows how to change the listings numbers in the caption ?

I'm unable to find any info on this on the web. :?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Numbering of Listings

Post by Stefan Kottwitz »

Hi Cham,
Cham wrote:In the preamble, using the listing setup shown below doesn't remove the chapter's number in the listing's label.

Code: Select all

\captionsetup[lstlisting]{numberbychapter=false}
I guess it should rather be an option to listings or set by \lstset instead of \captionsetup, such as

Code: Select all

\usepackage[numberbychapter=false]{listings}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Numbering of Listings

Post by Cham »

Stefan_K wrote:I guess it should rather be an option to listings or set by \lstset instead of \captionsetup, such as

Code: Select all

\usepackage[numberbychapter=false]{listings}
Stefan, it doesn't do anything ! And if I try it in the \lstset, it doesn't work either ! Try this code :

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{amsmath}
\usepackage{subfig}
\usepackage[numberbychapter=false]{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}}
%%
%\makeatletter
%\renewcommand\thelstlisting{\arabic{lstlisting}}
%\@addtoreset{lstlisting}{numberbychapter=false}
%\makeatother
%%
\begin{document}

\chapter{Test}
Test
\lstset{}

\begin{lstlisting}[caption={Title}]
 x := -2 (*  *)
 \end{lstlisting}

\begin{lstlisting}[caption={Title}]
 x := -2
 \end{lstlisting}

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

\lstlistoflistings

\end{document}
This is extremely annoying !
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

listings | Numbering of Listings

Post by Stefan Kottwitz »

You could also use the good old chngcntr package. Load it by \usepackage{chngcntr} and after \begin{document} write

Code: Select all

\counterwithout{lstlisting}{chapter}
It's important to do it after \begin{document} since the counter is defined at the beginning.

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

listings | Numbering of Listings

Post by Cham »

Your suggestion works !

Great, but can we also place the listing number elsewhere in the listing's name ?

For example, I would prefer to have something like this :

Code 1 de Mathematica,
Code 2 de Mathematica
...
instead of :

Code Mathematica 1,
Code Mathematica 2
...

EDIT : I have the impression that it's not possible without an hack. Could the listings package be buggy ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: listings | Numbering of Listings

Post by Stefan Kottwitz »

I would not assume that there's a bug just because the feature you desire isn't directly provided. That it doesn't exist is rather showing that you try to use a style which is pretty uncommon: putting the number within the label name.

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

listings | Numbering of Listings

Post by Cham »

Well, I was talking about a bug since the option numberbychapter=false is supposed to do what I was asking at first (remove the chapter's number) and yet it's doing nothing at all !

Of course, it's probably because I simply don't understand how it's actually working, despite having read the listings doc.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Numbering of Listings

Post by Cham »

Stefan,

do you think it's possible to change the style used for the list of listings ? 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
...

while the caption's name is actually "Code Mathematica 1 - caption content", etc...
Post Reply