# Listings numbering doesn't restart

I have problem with listing numbering from this topic. Numbering isn't restarted, it just continues.

\AtBeginDocument{%
\renewcommand{\thelstlisting}{%
\ifnum\value{subsection}=0
\thesection.\arabic{lstlisting}%
\else
\ifnum\value{subsubsection}=0
\thesubsection.\arabic{lstlisting}%
\else
\thesubsubsection.\arabic{lstlisting}%
\fi
\fi
}
}

For subsubsection 4.2.1 it should start from the beginning but it continues so I'm getting 4.2.1.2 (because there is one previous listing).

• Try \counterwithin{lstlisting}{subsubsection} from the chngcntr package. Does it help? Sep 23 '13 at 9:01
• @LudovicC. The chngcntr command is \counterwithin. Sep 23 '13 at 9:04
• @lockstep Thanks! When writing I thought it sounded weird \numberwithin{}{}. ;) Sep 23 '13 at 9:05
• @LudovicC. this is the error that I'm getting now... Error: No counter 'lstlisting' defined. Do I include it in preamble or after document beginning? Sep 23 '13 at 9:32
• Try to place it at the very end of your preamble. Sep 23 '13 at 9:35

It's no different from what I already suggested you as answer to Caption and sections, subsections and subsubsections

The only addition is that \AtBeginDocument is necessary, because listings defines there the counter.

\documentclass{article}
\usepackage{listings,chngcntr}

\AtBeginDocument{%
\counterwithin*{lstlisting}{section}
\counterwithin*{lstlisting}{subsection}
\counterwithin*{lstlisting}{subsubsection}
\renewcommand{\thelstlisting}{%
\ifnum\value{subsection}=0
\thesection.\arabic{lstlisting}%
\else
\ifnum\value{subsubsection}=0
\thesubsection.\arabic{lstlisting}%
\else
\thesubsubsection.\arabic{lstlisting}%
\fi
\fi
}%
}

\begin{document}
\section{A section}
\begin{lstlisting}[caption=Caption]
\caption{section.lstlisting}
\end{lstlisting}
\subsection{A subsection}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\subsubsection{A subsubsection}
\begin{lstlisting}[caption=Caption]
\caption{subsubsection.lstlisting}
\end{lstlisting}
\subsection{A subsection}
\begin{lstlisting}[caption=Caption]
\caption{subsection.lstlisting}
\end{lstlisting}
\section{A section}
\begin{lstlisting}[caption=Caption]
\caption{section.lstlisting}
\end{lstlisting}

\end{document}

• Is this written anywhere in the listings documentation? I couldn't find mention of using \AtBeginDocument anywhere. Thank you very much. Aug 17 '16 at 19:56
• @kando I don't think you find any mention of this in the manual for listings. Why it defines the counter “at begin document” is something I can't explain without a deep study of the package code. I just discovered the fact and so adjusted the solution to do the relevant work “at begin document”. Aug 17 '16 at 20:00
• Thank you for your tribal knowledge. I'd be lost without it. : j Aug 17 '16 at 20:50

I found the answer thanks to link that @LudovicC. posted. Since all of my listings are in subsubsections I added this line of code in the preamble

\AtBeginDocument{\counterwithin{lstlisting}{subsubsection}}

I don't think that it's right solution but it works in my case!

• It's the right solution except you shouldn't need \AtBeginDocument so long as you place the counterwithin command after loading the listings and chngctr packages Sep 23 '13 at 10:25
• If all your listings are at the same level you don't need the complicated if test in your question which is just to modify the format depending on the level. You just want \renewcommand{\thelstlisting}{\thesubsubsection.\arabic{lstlisting}} Sep 23 '13 at 10:27
• @DavidCarlisle listings defers defining \newcounter{lstlisting} at begin document. Sep 23 '13 at 11:59