Generalhow to use numberwithin with listings package?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
trashus
Posts: 3
Joined: Mon Sep 29, 2008 10:07 pm

how to use numberwithin with listings package?

Post by trashus »

I am composing a large document with many source code listings using the listings package, i.e.

Code: Select all

\usepackage{listings}
and I want the listings to be numbered with their section numbers (i.e. Listing 2.5 is the 5th listing in Section 2). I did this for equations with

Code: Select all

\numberwithin{equation}{section}
and it worked just fine, but I can't get it to work with listings.

My question is this: If I want to acheive listing numbers that have the section number using

Code: Select all

\numberwithin{X}{section}
what do I put in for X? I have tried listing, listings, lstlisting, lstlistings, and many more, but I get an error that says
! LaTeX Error: No Counter 'X' defined.
Does anybody know how to do this? What is the name of the listings counter?

Is there a way to do this without using numberwithin?

Recommended reading 2024:

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

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

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

how to use numberwithin with listings package?

Post by Stefan Kottwitz »

Hi trashus,

welcome to the board!
The correct counter is lstlisting. But in this case use

Code: Select all

\numberwithin{lstlisting}{section}
after \begin{document}.

Stefan
LaTeX.org admin
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

how to use numberwithin with listings package?

Post by localghost »

In the documentation of listings (Section 4.9 - Captions, p. 32f) you will find the corresponding counter lstlisting for the lstlisting environment. So you can try the following.

Code: Select all

\numberwithin{lstlisting}{section}
Now listings should be numbered as desired.


Best regards
Thorsten¹
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

how to use numberwithin with listings package?

Post by Stefan Kottwitz »

Just to explain why the command above did not work in the preamble: the counter lstlisting will be defined later, here's a piece of listing.sty showing it:

Code: Select all

\AtBeginDocument{
  \@ifundefined{thechapter}{\let\lst@ifnumberbychapter\iffalse}{}
  \lst@ifnumberbychapter
      \newcounter{lstlisting}[chapter]
      \gdef\thelstlisting%
           {\ifnum \c@chapter>\z@ \thechapter.\fi \@arabic\c@lstlisting}
  \else
      \newcounter{lstlisting}
      \gdef\thelstlisting{\@arabic\c@lstlisting}
  \fi}
Stefan
LaTeX.org admin
trashus
Posts: 3
Joined: Mon Sep 29, 2008 10:07 pm

how to use numberwithin with listings package?

Post by trashus »

Thanks for the quick responses Stefan_K and localghost!

I got everything working when \numberwithin{lstlisting}{section} is called after \begin{document}, but I didn't like having anything other than the document itself after \begin{document}, so I came up with a quick (sloppy) fix for \numberwithin having to be called in the document body when using the listings package:

I went into listings.sty and commented out these 2 lines (from the section of code in Stefan_K's previous post)

Code: Select all

\newcounter{lstlisting}
\gdef\thelstlisting{\@arabic\c@lstlisting}
and pasted the 2 lines into listings.sty immediately before the first instance of \AtBeginDocument.

This allows \numberwithin{lstlisting}{section} to be called in the preamble and hasn't caused any problems (yet) with inserting/referencing/hyperlinking listings, or any problems with \lstlistoflistings.

This also works for subsections instead of sections, but this method DOES NOT work if you use \numberwithin{lstlisting}{chapter}. Perhaps someone else knows how to fix this?
Last edited by trashus on Tue Sep 30, 2008 8:05 am, edited 1 time in total.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

how to use numberwithin with listings package?

Post by sommerfee »

trashus wrote:I got everything working when \numberwithin{lstlisting}{section} is called after \begin{document}, but I didn't like having anything other than the document itself after \begin{document}
Instead of patching listings.sty for this purpose you could use

Code: Select all

\AtBeginDocument{\numberwithin{lstlisting}{section}}
right after loading the listings package, this works, too.
I went into listings.sty and commented out these 2 lines (from the section of code in Stefan_K's previous post)
Changing packages is usually a very bad idea, because this makes your document giving a different result when run on a different machine.
This also works for subsections instead of sections, but this method DOES NOT work if you use \numberwithin{lstlisting}{chapter}. Perhaps someone else knows how to fix this?
Just place

Code: Select all

\lstset{numberbychapter}
into your document (after loading the (original) listings package).
I have attached my modified version of listings.sty.
:shock: And this is even a more bad idea, since it's against the license (LPPL) of the listings package! So please remove this attachment immediately, or give this modified version a different file and packaged name, and place your name + e-mail-address as maintainer. Thanks.

For further info on the license of the listings package, see listings.dtx and http://www.latex-project.org/lppl/.
Post Reply