[Tex/LaTex] Correct numbering of figures/tables/listings in Appendix

appendiceslistingsnumberingsectioning

In my thesis I have several appendices, which are defined via \section{First Appendix}, \section{Second Appendix}, etc. latex statements. The appendices have the following titles (hence, appear like this in the table of contents): A First Appendix, B Second Appendix, etc.

These appendices include listings and figures. Currently, the listings in the appendices are numbered as follows:

  • A.1 First Listing of Appendix A
  • A.2 Second Listing of Appendix A
  • B.3 First Listing of Appendix B
  • C.4 First Listing of Appendix C

However, I would like to have the following numbering (I think it's more intuitive and looks nicer):

  • A.1 First Listing of Appendix A
  • A.2 Second Listing of Appendix A
  • B.1 First Listing of Appendix B
  • C.1 First Listing of Appendix C

In other words, after each new appendix (which is defined via \section{Second Appendix}), I want the listings/figures/tables counter to be reset. In the main content of the thesis this is done automatically. I do not know why this is not the case in the appendix.

Note, that I use the following commands to achieve the desired numbering of figures/listings/tables which appear in the appendix:

\renewcommand{\thelstlisting}{\Alph{section}.\arabic{lstlisting}}
\renewcommand\thetable{\Alph{section}.\arabic{table}}
\renewcommand\thefigure{\Alph{section}.\arabic{figure}}

Best Answer

Add the lstlisting counter to the reset list of the section counter with

\counterwithout*{lstlisting}{section}

-- the * prevents redefinition of \thelstlisting then!

Saying

\makeatletter
\g@addto@macro\appendix{%
  \counterwithin*{lstlisting}{section}%
}
\makeatother

this will automatically enable this specific style of resetting if \appendix is used.

\documentclass{article}

\usepackage{listings}
\usepackage{chngcntr}

\AtBeginDocument{%
\renewcommand{\thelstlisting}{\Alph{section}.\arabic{lstlisting}}
\renewcommand\thetable{\Alph{section}.\arabic{table}}
\renewcommand\thefigure{\Alph{section}.\arabic{figure}}


}


%Automatically change the driver counter for reset:
\makeatletter
\g@addto@macro\appendix{%
  \counterwithin*{lstlisting}{section}%
}
\makeatother


\begin{document}

\appendix
\section{Foo}

\begin{lstlisting}[language={C},caption={Foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}

\begin{lstlisting}[language={C},caption={Foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}

\end{lstlisting}

\section{Foobar}

\begin{lstlisting}[language={C},caption={Foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}

\section{More foobar}

\begin{lstlisting}[language={C},caption={Foo}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}


\end{lstlisting}


\end{document}

enter image description here