2

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).

enter image description here enter image description here

6
  • 1
    Try \counterwithin{lstlisting}{subsubsection} from the chngcntr package. Does it help?
    – Ludovic C.
    Sep 23 '13 at 9:01
  • @LudovicC. The chngcntr command is \counterwithin.
    – lockstep
    Sep 23 '13 at 9:04
  • @lockstep Thanks! When writing I thought it sounded weird \numberwithin{}{}. ;)
    – Ludovic C.
    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?
    – Moirae
    Sep 23 '13 at 9:32
  • Try to place it at the very end of your preamble.
    – Ludovic C.
    Sep 23 '13 at 9:35
6

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}

enter image description here

3
  • Is this written anywhere in the listings documentation? I couldn't find mention of using \AtBeginDocument anywhere. Thank you very much.
    – kando
    Aug 17 '16 at 19:56
  • 1
    @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”.
    – egreg
    Aug 17 '16 at 20:00
  • Thank you for your tribal knowledge. I'd be lost without it. : j
    – kando
    Aug 17 '16 at 20:50
1

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!

3
  • 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
  • 1
    @DavidCarlisle listings defers defining \newcounter{lstlisting} at begin document.
    – egreg
    Sep 23 '13 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.