# How To Reset Counter To Latest Theorem/Definition/Whatever?

I like to give frequent examples and would like the numbering of these examples based on whatever definition/theorem/lemma/corollary/whatever was just shown. I would also like this numbering to be alphabetic if possible.

So for instance I would like the numbering on the examples below to be 1.1.1a, 1.1.1a, 1.1.2a, and 1.1.2b respectively.

Current Output:

Current TeX:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{chngcntr}
\renewcommand{\qedsymbol}{\rule{0.7em}{0.7em}}
\newcommand{\Mod}[1]{\ (\mathrm{mod}\ #1)}
\counterwithin{figure}{section}
\theoremstyle{definition}
\newtheorem{exmp}{Example}[subsection]
\newtheorem{theorem}{Theorem}[subsection]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{definition}{Definition}[subsection]
\newtheorem{lemma}[section]{Lemma}
\numberwithin{equation}{section}

\begin{document}
\subsection{A Detailed Look}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}
\begin{exmp}
1+0=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}
\begin{exmp}
Not sufficient since (-1)+2=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}
\begin{exmp}
(-1)+(-2)=-3
\end{exmp}
\begin{exmp}
Not sufficient since (-2)+1=-1
\end{exmp}
\end{document}


Any ideas?

• In other words, definitions, theorems, lemmas, propositions and corollaries should step the main counter, but examples wouldn't? Nov 17, 2022 at 17:57
• @egreg definitions and the like would increase main counter, but only relative to its own "type" (e.g. if it goes definition, theorem, the numbering would be Definition 1.1 and Theorem 1.1). And example would reflect whichever "type" preceded it. So if there is an example for the third definition, it would be labeled Example 1.3a, and if there is an example afterwards for the second theorem, it would be labeled Example 1.2a
– EDS
Nov 17, 2022 at 18:28
• Now I understand and find this numbering system utterly confusing. A reader won't be able to know whether definition 1.1.5 precedes or follows theorem 1.1.3. And corollaries will have four numbers, while lemmas just two. So, what subsection will lemma 1.5 be in? And in the proof of what theorem would it be used? By the way, it should be \newtheorem{lemma}{Lemma}[section]. Nov 17, 2022 at 18:37
• @egreg thank you, still getting used to assembling the preamble to make it do what I want... And yeah, it'd probably be confusing for outside readers but I'm just using it for personal notes.
– EDS
Nov 17, 2022 at 18:42

You can define an example counter "prefix", and this prefix is updated with the use of every environment that you may consider having examples following it.

\documentclass{article}

\usepackage{amsthm}
\usepackage{chngcntr}
\usepackage{etoolbox}

\theoremstyle{definition}
\newtheorem{theorem}{Theorem}[subsection]
\newtheorem{definition}{Definition}[subsection]
\newtheorem{exmp}{Example}

% Capture latest environment that could have an example following it
\AtEndEnvironment{theorem}{\global\let\exampleprefix\thetheorem}
\AtEndEnvironment{definition}{\global\let\exampleprefix\thedefinition}

% Adjust Example counter to be reset with every theorem, definition, ...
\counterwithin{exmp}{theorem}
\counterwithin{exmp}{definition}
% Update representation of Example counter
\newcommand{\exampleprefix}{}% Define Example counter prefix to be empty at first
\renewcommand{\theexmp}{\exampleprefix\alph{exmp}}% Prefix counter with whatever environment was used previously

\begin{document}

\subsection{A Detailed Look}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}

\begin{exmp}
$1 + 0 = 1$
\end{exmp}

\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}

\begin{exmp}
Not sufficient since $(-1) + 2 = 1$
\end{exmp}

\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}

\begin{exmp}
$(-1) + (-2) = -3$
\end{exmp}

\begin{exmp}
Not sufficient since $(-2) + 1 = -1$
\end{exmp}

\end{document}


Please, don't use independent numbering for theorems, definitions, lemmas and corollaries. Your reader won't be able to find them when you cross reference them by number.

And you'll have, in this case, two examples numbered 1.1.1a, making the numbering useless.

If you accept this advice, you just define exmp to have its counter tied to definition.

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{definition}

\newtheorem{definition}{Definition}[subsection]
\newtheorem{theorem}[definition]{Theorem}
\newtheorem{corollary}[definition]{Corollary}
\newtheorem{lemma}[definition]{Lemma}
\newtheorem{exmp}{Example}
\counterwithin*{exmp}{definition}
\renewcommand{\theexmp}{\thedefinition\alph{exmp}}

\begin{document}

\subsection{A Detailed Look}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}
\begin{exmp}
1+0=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}
\begin{exmp}
Not sufficient since (-1)+2=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}
\begin{exmp}
(-1)+(-2)=-3
\end{exmp}
\begin{exmp}
Not sufficient since (-2)+1=-1
\end{exmp}

\subsection{A Check}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}
\begin{exmp}
1+0=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}
\begin{exmp}
Not sufficient since (-1)+2=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}
\begin{exmp}
(-1)+(-2)=-3
\end{exmp}
\begin{exmp}
Not sufficient since (-2)+1=-1
\end{exmp}

\end{document}


Otherwise, with a slight modification of Werner's code:

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{definition}

\newtheorem{definition}{Definition}[subsection]
\newtheorem{theorem}{Theorem}[subsection]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}{Lemma}[section]
\newtheorem{exmp}{Example}
\counterwithin*{exmp}{definition}
\counterwithin*{exmp}{theorem}
\counterwithin*{exmp}{lemma}
\counterwithin*{exmp}{corollary}
\renewcommand{\theexmp}{\thelaststatement\alph{exmp}}

\AtEndEnvironment{definition}{\xdef\thelaststatement{\thedefinition}}
\AtEndEnvironment{theorem}{\xdef\thelaststatement{\thetheorem}}
\AtEndEnvironment{corollary}{\xdef\thelaststatement{\thecorollary}}
\AtEndEnvironment{lemma}{\xdef\thelaststatement{\thelemma}}

\begin{document}

\subsection{A Detailed Look}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}
\begin{exmp}
1+0=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}
\begin{exmp}
Not sufficient since (-1)+2=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}
\begin{exmp}
(-1)+(-2)=-3
\end{exmp}
\begin{exmp}
Not sufficient since (-2)+1=-1
\end{exmp}

\subsection{A Check}

\begin{definition}
\textbf{addition} is denoted with the + symbol
\end{definition}
\begin{exmp}
1+0=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy I:} The addition of two positives is positive.
\end{theorem}
\begin{exmp}
Not sufficient since (-1)+2=1
\end{exmp}
\begin{theorem}
\textbf{Trichotomy II:} The addition of two negatives is negative.
\end{theorem}
\begin{exmp}
(-1)+(-2)=-3
\end{exmp}
\begin{exmp}
Not sufficient since (-2)+1=-1
\end{exmp}

\end{document}