1

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}}
1

2 Answers 2

2

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

1

I just had the same "problem" with the non-intuitive numbering in my thesis. A solution (inspired by this) is to reset the counter after \appendix, but before the first section and lstlisting.

\renewcommand\thesection{\Alph{section}}
\renewcommand\thelstlisting{\thesection.\arabic{lstlisting}}

\setcounter{lstlisting}{0}

Additionally, because I don't want to have to reset the counter manually for each section, I use \sectionbreak from the package titlesec, which enables one to define commands before the beginning of each section. I added the counter resetting like this

\newcommand{\sectionbreak}{\setcounter{lstlisting}{0}}

A full MWE would like this (credits also to @Christian Hupfer)

\documentclass{article}

\usepackage{listings}
\usepackage{titlesec}

\begin{document}
\appendix

\renewcommand\thesection{\Alph{section}}
\renewcommand\thelstlisting{\thesection.\arabic{lstlisting}}

\newcommand{\sectionbreak}{\setcounter{lstlisting}{0}} % to reset counter for listings before each new section

\section{First appendix}
Some text.

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

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

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

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


\section{Second appendix}
Some more text.

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

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

\end{document}

Important note: It seems you need to have some text within section to get the \sectionbreakworking.

image

I'm not quite sure whether this is a solution or a workaround, but it is working for me.

Btw, this is also working for figure and table.

2
  • Welcome to TeX.SX! It would be great if you could add a minimal working example (MWE) that demonstrates how this solution works. I tried to get your answer working but was unsuccessful, a full compilable MWE (\documentclass to \end{document}) will help others understand and appreciate your solution more easily.
    – Dai Bowen
    Oct 8, 2016 at 17:04
  • +1 Thanks for adding the MWE, now I can see this works, I think I may have only had the lstlisting environment in each section. I wonder whether that's intended on the part of titlesec. Rather than \sectionbreak you might be able to \apptocmd from etoolbox to append the necessary instructions to the \section macro.
    – Dai Bowen
    Oct 13, 2016 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .