# Per-subsection figure numbering

I want to have per-subsection figure numbering in the appendix of the following MWE: it should read "Figure A.1" instead of "Figure 2".

\documentclass{article}

\usepackage{amsmath}
\numberwithin{equation}{section}

\usepackage[demo]{graphicx}
\usepackage{caption}
\captionsetup[figure]{figurewithin=section}

\newcommand*{\appheading}[1][Appendix]{%
\setcounter{secnumdepth}{0}\section{#1}\setcounter{secnumdepth}{3}%
\renewcommand*{\thesubsection}{\Alph{subsection}}
\numberwithin{equation}{subsection}
}

\begin{document}

\section{Foo}

$$x = 4$$

\begin{figure}[htbp!]
\centering
\includegraphics{foo}
\caption{Fooo}
\end{figure}

\appheading
\appendix
\subsection{Baz}

$$y = 3$$

\begin{figure}[htbp!]
\centering
\includegraphics{baz}
\caption{Baz}
\end{figure}

\end{document}


I tried changing the thefigure counter and also using the chngcntr package but couldn't make it work.

• Something like this should do what you want. \setcounter{figure}{0} \renewcommand{\thefigure}{Figure \Alph{chapter}.\arabic{figure}} – Nicholas Hamilton May 31 '13 at 21:25
• figurewithin=section should be directly passed to the caption package, i.e. \usepackage[figurewithin=section]{caption} or \captionsetup{figurewithin=section} (without the [figure] stuff). – Axel Sommerfeldt Jun 1 '13 at 12:23

## 2 Answers

Use \numberwithin instead of the figurewithin option:

\documentclass{article}

\usepackage{amsmath}
\numberwithin{equation}{section}

\usepackage[demo]{graphicx}
\usepackage{caption}
\numberwithin{figure}{section}

\newcommand*{\appheading}[1][Appendix]{%
\setcounter{secnumdepth}{0}\section{#1}\setcounter{secnumdepth}{3}%
\renewcommand*{\thesubsection}{\Alph{subsection}}
\numberwithin{equation}{subsection}
\numberwithin{figure}{subsection}
}

\begin{document}

\section{Foo}

$$x = 4$$

\begin{figure}[htbp!]
\centering
\includegraphics{foo}
\caption{Fooo}
\end{figure}

\appheading
\appendix
\subsection{Baz}

$$y = 3$$

\begin{figure}[htbp!]
\centering
\includegraphics{baz}
\caption{Baz}
\end{figure}

\end{document}


Why don't you use \part* instead of those tricks?

\documentclass{article}

\usepackage{amsmath}
\numberwithin{equation}{section}

\usepackage[demo]{graphicx}
\usepackage{caption}
\numberwithin{figure}{section}

\newcommand*{\appheading}[1][Appendix]{%
\part*{#1}
\renewcommand*{\thesection}{\Alph{section}}
}

\begin{document}

\section{Foo}

$$x = 4$$

\begin{figure}[htbp!]
\centering
\includegraphics{foo}
\caption{Fooo}
\end{figure}

\appheading
\appendix
\section{Baz}

$$y = 3$$

\begin{figure}[htbp!]
\centering
\includegraphics{baz}
\caption{Baz}
\end{figure}

\end{document}

• I don't use part because: (i) with the tricks I can control pdf bookmarks easily (allowing them to go straight to the section heading) (ii) I like the appendix to be a section rather than a part. Excellent answer BTW. It never ceases to amaze me how much you know about TeX. – petobens May 31 '13 at 21:51

insert these two lines immediately after the line \subsction{Baz}:

\setcounter{figure}{0}
\setcounter{section}{1}