# Numbering of figures in chapter screwed up

I write on my thesis and it has three main chapters.
At first all figures were numbered as: "Figure number of chapter.continuous number per chapter".

So the figures in the first chapter were numbered: 1.1, 1.2, 1.3, ... and the ones in the second chapter 2.1, 2.2, 2.3, ...

Unfortunately I had to add an appendix after the second chapter. It requires a specific style which I achieved with some help and the following commands:

\appendix
\counterwithin{figure}{section}
\renewcommand{\thesection}{A.\arabic{section}}

... all the figures ...

\unappendix


The "appendix" and "unappendix" commands are defined at the beginning of the document (see minimal working example).
Everything looks fine except of the numbering of the figures in the third chapter.
The figures are now label as: "Figure number of chapter.number of section.continuous number per section".
So if there would be two sections in chapter three and one figure is in the first one and three in the second one it would look like this: 4.1.1, 4.2.1, 4.2.2, 4.2.3.
However, I would like to have the same numbering style as in the first two chapters (4.1, 4.2, 4.2, 4.4). If I remove the appendix the numbering looks fine so it seems like the commands I use for the appendix mess things up.

Does anyone know how I can obtain the desired numbering for the third chapter? I assume some of the "unappendix" characteristics need to be adjusted but I just cant figure out what and how.

This is a minimal working example that illustrates the problem:

 \documentclass[a4paper,12pt]{book}
\usepackage[british,ngerman]{babel}
\usepackage{chngcntr}

\makeatletter
\newcounter{savesection}
\newcounter{apdxsection}
\renewcommand\appendix{\par
\setcounter{savesection}{\value{section}}%
\setcounter{section}{\value{apdxsection}}%
\renewcommand\theHsection{appendix.\thesection}
\setcounter{subsection}{0}%
\gdef\thesection{\@Alph\c@section}}
\newcommand\unappendix{\par
\setcounter{apdxsection}{\value{section}}%
\setcounter{section}{\value{savesection}}%
\renewcommand\theHsection{\thesection}
\setcounter{subsection}{0}%
\gdef\thesection{\thechapter.\arabic{section}}}
\makeatother

\begin{document}

\chapter{First chapter}
This is chapter 1.

\section{First section}

\begin{figure}
\caption{Figure 1.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 1.2}
\end{figure}

\chapter{Second chapter}
This is chapter 2.

\section{First section}

\begin{figure}
\caption{Figure 2.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 2.2}
\end{figure}

\appendix
\counterwithin{figure}{section}
\renewcommand{\thesection}{A.\arabic{section}}

\begin{figure}
\caption{Does not matter}
\end{figure}

\begin{figure}
\caption{Does not matter}
\end{figure}

\unappendix

\chapter{Third chapter}
This is chapter 3.

\section{First section}

\begin{figure}
\caption{Figure 3.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 3.2}
\end{figure}

\begin{figure}
\caption{Figure 3.3}
\end{figure}

\end{document}


The code you provide is for a document where the main divisions are sections rather than chapters. However, there is already a package that helps with this sort of situation, called appendix. Using the subappendices environment provides appendices for individual chapters, introduced by a \section command. The default numbering of these appendix sections is \thechapter.\Alph{section}. I would suggest you just let the figure numbering continue unchanged.

\documentclass[a4paper,12pt]{book}

\usepackage[british,ngerman]{babel}
\usepackage{appendix}

\begin{document}

\chapter{First chapter}
This is chapter 1.

\section{First section}

\begin{figure}
\caption{Figure 1.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 1.2}
\end{figure}

\chapter{Second chapter}
This is chapter 2.

\section{First section}

\begin{figure}
\caption{Figure 2.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 2.2}
\end{figure}

\begin{subappendices}
\section{Appendix}

\begin{figure}
\caption{Does not matter}
\end{figure}

\begin{figure}
\caption{Does not matter}
\end{figure}
\end{subappendices}

\chapter{Third chapter}
This is chapter 3.

\section{First section}

\begin{figure}
\caption{Figure 3.1}
\end{figure}

\section{Second section}

\begin{figure}
\caption{Figure 3.2}
\end{figure}

\begin{figure}
\caption{Figure 3.3}
\end{figure}

\begin{subappendices}
\section{Appendix}

\begin{figure}
\caption{Does not matter}
\end{figure}

\begin{figure}
\caption{Does not matter}
\end{figure}
\end{subappendices}

\end{document}

• This fixes the numbering issue but creates a bunch of new issues. In my appendix I do have two sections. In the table of content they now appear as: "3.A Appendix, 3.B Section 1, 3.C Section 2". It would be nice to have maybe a title Appendix without a number and then below: "3.A Section 1, 3.B Section 2". At the moment I have "A.1 Section 1, A.2 Section 2". This is ok as well. May 6, 2019 at 15:29
• I did some further research and was able to change the style of the Appendix entries in the TOC so that they match my requirements (tex.stackexchange.com/questions/46976/…). It seems like there are no negative side effects either. I am ok with the solution even so it impacted the style of my document more than I originally intended. May 6, 2019 at 16:35
• The heading Appendix is from the \section command, you can change that as you wish. I would be inclined to have \section{Appendix 1} \section{Appendix 2} labelled 3.A and 3.B. Not having the chapter number included is going to confuse your readers. May 7, 2019 at 6:41