5

For numbering figures and tables with 1 depth level like this:

Figure 1.2
Table 3.13

I use the package:

\usepackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{table}{section}
\numberwithin{equation}{section}

I wanted to get the same effect for code listings usepackage listings but the compiler says that chngcntr doesnt support listings. How can I accomplish this effect? I would appreciate all help.

4

1 Answer 1

6

Here is a solution.

The counter lstlisting is defined using \AtBeginDocument so the change must be done after \begin{document} or using \AtBeginDocument

\documentclass{book}
\usepackage{lipsum}
\usepackage{listings}
\usepackage{chngcntr}
\AtBeginDocument{\counterwithin{lstlisting}{chapter}}
\begin{document}
\chapter{Foo}
\begin{lstlisting}[caption={bla bla},label=list]
for i:=maxint to 0 do
begin
{ do nothing }
end;

Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\section{Foo}
\begin{lstlisting}[caption={bla bla},label=list]
for i:=maxint to 0 do
begin
{ do nothing }
end;

Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\end{document} 
2
  • You example works but when I add this to my existing document i get an error ! Package chngcntr Error: chapter is not a counter. My document template is this one tex.stackexchange.com/questions/316160/… I dont understand which package causes conflict. Jun 24, 2016 at 13:06
  • I think I got it-- I replaced parameter chapter in \AtBeginDocument{\counterwithin{lstlisting}{chapter}} with section and it seem to work! Thank you. Jun 24, 2016 at 13:13

You must log in to answer this question.

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