# Numbering of figures within theorems

I have a lot of Theorems and within them some Figures, which I have to number according to the respective Theorem number. In principle, this works fine with \numberwithin{figure}{Theorem}. However, if I only have one Figure within a Theorem (e.g. within Theorem 1) I only want it to be called "Figure 1" instead of "Figure 1.1" Is there a way to do so?

Here is an example code:

\documentclass{article}

\usepackage{amsmath}
\newtheorem{Theorem}{Theorem}
\numberwithin{figure}{Theorem}

\begin{document}

\begin{Theorem}
This is Theorem 1.

\begin{figure}[htp]
\centering Figure
\caption{This should be Figure 1 (instead of Figure 1.1).}
\end{figure}
\end{Theorem}

\begin{Theorem}
This is Theorem 2.

\begin{figure}[htp]
\centering Figure
\caption{This is Figure 2.1.}
\end{figure}

\begin{figure}[htp]
\centering Figure
\caption{This is Figure 2.2.}
\end{figure}
\end{Theorem}

\end{document}


Since the figure-related graphs mustn't "float" (in the LaTeX-specific sense of the word), you probably shouldn't be using figure environments to begin with for those graphs. Instead, use minipage environments and \captionof{figure}{...} statements to create figure-like captions. Finally, for those theorems with more than one graph, issue the instruction

\counterwithin{figure}{Theorem}


immediately before the start of the theorem-like environment and

\counterwithout{figure}{Theorem}


immediately after the end of the environment.

\documentclass{article}
\usepackage{caption}
\usepackage{amsthm}
\newtheorem{Theorem}{Theorem}

\begin{document}

\begin{Theorem}
This is Theorem 1.

\noindent\begin{minipage}{\textwidth}\upshape
\centering
Figure
\captionof{figure}{This is Figure 1.}
\end{minipage}
\end{Theorem}

\counterwithin{figure}{Theorem}

\begin{Theorem}
This is Theorem 2.

\noindent\begin{minipage}{\textwidth}\upshape
\centering
Figure
\captionof{figure}{This is Figure 2.1.}

\bigskip
Figure
\captionof{figure}{This is Figure 2.2.}
\end{minipage}
\end{Theorem}

\counterwithout{figure}{Theorem}

\begin{Theorem}
This is Theorem 3.

\noindent\begin{minipage}{\textwidth}\upshape
\centering
Figure
\captionof{figure}{This is Figure 3.}
\end{minipage}
\end{Theorem}

\end{document}

• Seems to work perfectly fine, thanks. However, since I have not yet used minipage at all, what exactly is the advantage of using it instead of figure environments? – Jack Nov 9 '19 at 12:13
• @Jack - The disadvantage of the figure environment -- at least when used in theorem-like environments -- is that there's no guarantee that the figure environment will actually be placed right where you want it to be. That cannot happen with minipage environments. – Mico Nov 9 '19 at 12:17
• I have just identified a challenge with the proposed solution: If I have a Theorem without any figure, then the numbering of the subsequent figures goes wrong. For example, if you would change your Theorem 3 to Theorem 4 and add a new Theorem 3 without any Figure before, then the figure in Theorem 4 is still called "Figure 3". Is there a solution for this as well? – Jack Nov 9 '19 at 12:38
• @Jack - If theorems 2 and 4 are associated with figures but theorem 3 is not, you could issue the instruction \stepcounter{figure} immediately before theorem 4. If you've guess that \stepcounter increments its argument (which must be a counter variable) by 1, you'd be entirely correctly. If there are several theorems without associated figures, the \setcounter instruction may be more useful. (Before the start of theorem 7, say, issue the instruction \setcounter{figure}{6}.) – Mico Nov 9 '19 at 13:03