[Tex/LaTex] Appendix after each chapter v2.0

appendicesbooks

I'm interested in add an appendix with two or 3 sections after each chapter in a book, same as in question answered here.

The answer provided there works good, but it has undesired effect on numbering of figures etc. In the book class figures are numbered in a chapter specific way (e.g. 1.1, 1.2,…,2.1, 2.2) which I like, however the answer resets this to section specific numbering in all chapters after the new subappendices environment is used, which yields strange section-specific numbering – 1.1, 1.2 (chapter 1 good), subappendices, 2.1, 2.2 (ch 2 figures in first section), 2.1, 2.2 (ch 2 figures in the second section). How can one reset the numbering to chapter specific one?

Here is the MWE:

\documentclass{book}
\usepackage{appendix}
\usepackage{chngcntr}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{float}


\AtBeginEnvironment{subappendices}{%
\chapter*{Appendix}
\addcontentsline{toc}{chapter}{Appendices}
\counterwithin{figure}{section}
\counterwithin{table}{section}
}

\begin{document}

\tableofcontents

\chapter{Test Chapter One}
\section{A regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Another regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{subappendices}
\section{Some title for an appendix}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Some title for an appendix}
\lipsum[4]
\end{subappendices}

\chapter{Test Chapter Two}
\section{A regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Another regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\end{document}

Best Answer

What worked out well is defining the end of subappendices environment as follows

\AtEndEnvironment{subappendices}{%
\counterwithout{figure}{section}
\counterwithin{figure}{chapter}
\counterwithout{table}{section}
\counterwithin{table}{chapter}
}

The full example:

\documentclass{book}
\usepackage{appendix}
\usepackage{chngcntr}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{float}


\AtBeginEnvironment{subappendices}{%
\chapter*{Appendix}
\addcontentsline{toc}{chapter}{Appendices}
\counterwithin{figure}{section}
\counterwithin{table}{section}
}
\AtEndEnvironment{subappendices}{%
\counterwithout{figure}{section}
\counterwithin{figure}{chapter}
\counterwithout{table}{section}
\counterwithin{table}{chapter}
}

\begin{document}

\tableofcontents

\chapter{Test Chapter One}
\section{A regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Another regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{subappendices}
\section{Some title for an appendix}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Some title for an appendix}
\lipsum[4]
\end{subappendices}

\chapter{Test Chapter Two}
\section{A regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\section{Another regular section}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\begin{figure}; 
\begin{tikzpicture}; \draw (0,0) --(1,2);\end{tikzpicture}; \caption{some caption} 
\end{figure}

\end{document}