# Numbering of listings

I would like to numerate listings according to sections. If the listing is in subsection then it should have subsection numbers.

This example is very good and I'm using it for some other numbering, but it gives me error because I can't seem to guess the parameter for renewcommand

\renewcommand{\thelstlistingname}{%
\ifnum\value{subsection}=0
\thesection.\arabic{lstlistingname}%
\else
\ifnum\value{subsubsection}=0
\thesubsection.\arabic{lstlistingname}%
\else
\thesubsubsection.\arabic{lstlistingname}%
\fi
\fi
}


This is the MWE

\documentclass[11pt]{article}
\usepackage{listings}
\renewcommand{\lstlistingname}{Pseudokod}

\begin{document}
\section{Section}
\subsection{Subsection}

\begin{lstlisting}[caption=Pseudokod za algoritam segmentacije slike u boji.]
...
ucitaj sliku
ucitaj zeljeni prag ili pragove za segmentaciju
podijeli sliku na kanale //ako je u pitanju RGB slika podijela je na R, G i B kanal boje
primjeni prag na svakom kanalu
AND operacija na kanalima
prikazi sliku
...
\end{lstlisting}
\end{document}


Since the listing is in section 1.1 I would like it to be numbered like 1.1.1. Next listing in that section would be 1.1.2, etc.

• Isn't it \thelstlisting? – Ludovic C. Sep 21 '13 at 15:57

I'm not sure to understand exactly what you want to achieve.

In any case, the right counter is lstlisting and gets defined only after the document has begun.

So, you can either put the following code after \begin{document}

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


or the following in the preamble

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


The result is the same, but I would suggest the latter.

• I tried to do that but I'm getting this error Error: \thelstlisting undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.114 \renewcommand{\thelstlisting} {%  Additionally, my preamble is huge so I'm writing it in other file which is included at the beginning of main doc. – Moirae Sep 21 '13 at 17:51
• @Moirae it works perfectly for me with your MWE. Do you you have problems with it or with a different document? – karlkoeller Sep 21 '13 at 18:18
• I'm using it with my thesis document. I have some additional macros defined, maybe that's the problem. One is for \lstset and the other is \renewcommand{\lstlistingname}{Pseudokod} – Moirae Sep 21 '13 at 18:56
• @Moirae Are you able to isolate the code that causes the error and edit your answer with it? – karlkoeller Sep 21 '13 at 19:05

You should redefine \thelstlisting, however note that you can only do that after \begin{document} since it gets defined at that point:

\documentclass[11pt]{article}
\usepackage{listings}

\begin{document}

\makeatletter
\renewcommand\thelstlisting{\thesection.\@arabic\c@lstlisting}
\makeatother

\section{Section}
\subsection{Subsection}

\begin{lstlisting}[caption=Pseudokod za algoritam segmentacije slike u boji.]
...
ucitaj sliku
ucitaj zeljeni prag ili pragove za segmentaciju
podijeli sliku na kanale //ako je u pitanju RGB slika podijela je na R, G i B kanal boje
primjeni prag na svakom kanalu
AND operacija na kanalima
prikazi sliku
...
\end{lstlisting}
\end{document}


This follows a common pattern: when you define an environment supporting cross-reference, there is always a counter by the same name keeping the unique id of the environment; the macro \the<counter> is responsible for "formatting" it. Here for example we make use of \thesection which works in the same way.

• There was a problem with TeXnicle but now it works. So, the important thing is to put this one AFTER the beginning of the document. Thank you for your answers! – Moirae Sep 21 '13 at 19:50