# Equation Counters in and outside Theorems

I am trying to setup the counters so that the equations and theorems increment by section and there is no overlap between equation and theorem numbering. I also would like to be able to number equations inside theorems as a subnumber of the theorem number.

For example in section 2 I might have an equation first that would be labeled (2.1) and then a theorem, labeled 2.2 where there is an equation inside the theorem which I would want labeled 2.2.1.

• What package(s) are you using to generate your theorem(s)? – Werner Mar 9 '13 at 4:38

In the solution below I've used

\numberwithin{equation}{section}            % (1.1), (1.2), etc
\newtheorem{mytheorem}[equation]{Theorem}   % Theorems share equation numbers


to get numbering (1.1), (1.2), etc. and then mytheorem gets the same numbers as equations.

I've borrowed some important code from Command behavior depending on current environment so that my newcounter subequations can be incremented, but only when within mytheorem.

I've used the etoolbox for its important \appto command.

Complete MWE:

% arara: pdflatex
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{etoolbox}
\numberwithin{equation}{section}            % (1.1), (1.2), etc
\newtheorem{mytheorem}[equation]{Theorem}   % Theorems share equation numbers

% https://tex.stackexchange.com/questions/39738/command-behavior-depending-on-current-environment
\makeatletter
\def\Mycurrentvir{document}
\def\ifenv#1{
\def\@tempa{#1}%
\ifx\@tempa\Mycurrentvir
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother

\newcounter{subequation}
\appto\mytheorem{\setcounter{subequation}{0}%
\def\Mycurrentvir{mytheorem}%
\renewcommand{\theequation}{\arabic{section}.\arabic{equation}.\arabic{subequation}}}

\begin{document}

\section{My section}
$$\label{eq:first} y=mx+b$$
\begin{mytheorem}\label{thm:test}
$$\label{eq:test} y=ax^2+bx+c$$
Another equation
$$y=ax^2+bx+c$$
\end{mytheorem}
$$y=x^3$$

\begin{itemize}
\item First equation: \eqref{eq:first}
\item Equation within theorem: \eqref{eq:test}
\item Theorem reference: \ref{thm:test}
\end{itemize}

\section{another section}
$$y=mx+b$$
\begin{mytheorem}
$$y=ax^2+bx+c$$
Another equation
$$y=ax^2+bx+c$$
\end{mytheorem}
$$y=x^3$$
\end{document}