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

Moirae
  • 295

2 Answers2

7

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

egreg
  • 1,121,712
  • 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!

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