2

I'm trying to have two separate equation counters, one for TypeA equations and another of TypeB equations. They shouldn't share a counter, but pick up the counter from whichever type they are within.

Note that this is different than continuous sections, subsections, etc. I'm using the amsmath package.

I would like the following (I don't care whether its (1.1, 1.2, ...) or (1a, 1b, ...)):

TypeA 1 (Funky Equation)
a = n (1.1)
b = m (1.2)

TypeB 1 (Crazy Equation)
a = n (1.1)
b = m (1.2)

TypeA 2 (Funkier Equation)
a = n (2.1)
b = m (2.2)

TypeB 2 (Crazies Equation)
a = n (2.1)
b = m (2.2)

TypeB 3 (Crazies Equation)
a = n (3.1)
b = m (3.2)

TypeB 4 (Crazies Equation)
a = n (4.1)
b = m (4.2)

TypeA 3 (Crazy Equation)
a = n (3.1)
b = m (3.2)

I have this in my header:

\newtheorem{typeagroup}{TypeA}
\newtheorem{typea}[typeagroup]{TypeA}
\counterwithout{equation}{typeagroup}

\newtheorem{typebgroup}{TypeB}
\newtheorem{typeb}[typebgroup]{TypeB}
\counterwithout{equation}{typebgroup}

Notice that the counter is only incremented when a TypeB is present. This is because it's the last \counterwithin{} I have in the above definitions.

Here is the format of the code I use for each type of equation:

\begin{typea}[Funky Equation]
\begin{subequations}
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{subequations}
\end{typea}

\begin{typeb}[Crazy Equation]
\begin{subequations}
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{subequations}
\end{typeb}

I get the following:

TypeA 1 (Funky Equation)
a = n (0.1a)
b = m (0.1b)

TypeB 1 (Crazy Equation)
a = n (1.1a)
b = m (1.1b)

TypeA 2 (Funkier Equation)
a = n (1.1a)
b = m (1.1b)


TypeB 2 (Crazies Equation)
a = n (2.1a)
b = m (2.1b)

TypeB 3 (Crazies Equation)
a = n (3.1a)
b = m (3.1b)

TypeB 2 (Crazies Equation)
a = n (2.1a)
b = m (2.1b)

TypeA 3 (Crazy Equation)
a = n (2.1a)
b = m (2.1b)
4

Define new environments:

\documentclass{article}
\usepackage{amsmath}

\newtheorem{typea*}{Type A}
\newtheorem{typeb*}{Type B}

\newenvironment{typea}
 {\setcounter{equation}{\value{typea*}}\begin{subequations}\begin{typea*}}
 {\end{typea*}\end{subequations}}
\newenvironment{typeb}
 {\setcounter{equation}{\value{typeb*}}\begin{subequations}\begin{typeb*}}
 {\end{typeb*}\end{subequations}}

\begin{document}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\end{document}

If you also need “regular” equation numbers independent from these, it can be arranged.

enter image description here

It's quite simple to add prefixes:

\documentclass{article}
\usepackage{amsmath}

\newtheorem{typea*}{Type A}
\newtheorem{typeb*}{Type B}

\newenvironment{typea}
 {\setcounter{equation}{\value{typea*}}%
  \renewcommand{\theequation}{A-\arabic{equation}}%
  \begin{subequations}%
  \begin{typea*}}
 {\end{typea*}\end{subequations}}
\newenvironment{typeb}
 {\setcounter{equation}{\value{typeb*}}%
  \renewcommand{\theequation}{B-\arabic{equation}}%
  \begin{subequations}%
  \begin{typeb*}}
 {\end{typeb*}\end{subequations}}

\begin{document}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typeb}[Crazy Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typeb}

\begin{typea}[Funky Equation]
  \begin{align}
      a = n \\
      b = m 
  \end{align}
\end{typea}

\end{document}

enter image description here

  • Thanks egreg, that worked well. Follow up question: how do I combine phfaist's answer and define a custom counter prefix like A-1a, A-1b; B-1a, B-1b, etc? Thanks – Student Jan 28 '16 at 20:41
  • @Student Easy. ;-) – egreg Jan 28 '16 at 21:32
  • Thanks @egreg, that works. Anyway of removing the double alph counter? I'm getting A-1aa, A-1bb.... A2-aa, A-2ab, etc. when all I need is A-1a or A-1.1 Thanks! – Student Jan 29 '16 at 13:59
  • @Student Since I don't get the doubling, I can't tell you how to remove it. – egreg Jan 29 '16 at 15:54
  • Thanks @egreg, not sure why I was getting them but incase others do to, I updated your code with a comment, Thanks! – Student Jan 31 '16 at 18:42
2

I don't see why you'd like to go via new theorem definitions. You can just define a new counter and alias LaTeX's equation counter to it. Here's my version of a macro \makeeqcontext (adapted from some earlier code I had written for myself at some point):

\makeatletter
\def\makeeqcontext#1#2{%
  \edef\tmp@eqcontextcountername{eqcounter#1}%
  \expandafter\newcounter{\tmp@eqcontextcountername}%
  \expandafter\def\csname #1\endcsname{%
    \begingroup%
    \expandafter\let\expandafter\c@equation\csname c@eqcounter#1\endcsname%
    \renewcommand{\theequation}{#2}%
    %
  }%
  \expandafter\def\csname end#1\endcsname{%
    \endgroup%
  }
}%
\makeatother

It can be used as follows:

\makeeqcontext{<environment-name>}{<equation-label>}

and it defines a new environment with the given name in which equations have a separate counter with the given label. Here's a fully working example:

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\makeeqcontext#1#2{%
  \edef\tmp@eqcontextcountername{eqcounter#1}%
  \expandafter\newcounter{\tmp@eqcontextcountername}%
  \expandafter\def\csname #1\endcsname{%
    \begingroup%
    \expandafter\let\expandafter\c@equation\csname c@eqcounter#1\endcsname%
    \renewcommand{\theequation}{#2}%
    %
  }%
  \expandafter\def\csname end#1\endcsname{%
    \endgroup%
  }
}%
\makeatother
\makeeqcontext{typea}{A-\arabic{equation}}
\begin{document}
% default equation type:
\begin{align}
  a = b
  \label{a}
\end{align}
\begin{typea} % equation type A:
  \begin{align}
    c = d
    \label{c}
  \end{align}
\end{typea}
\begin{typea} % equation type A:
  \begin{subequations}
    \begin{align}
      e &= f \\
      g &= h \label{g}
    \end{align}
  \end{subequations}
\end{typea}
Reference~\eqref{a}, \eqref{c} and~\eqref{g}.
\end{document}
  • 1
    Thanks [phfaist], that worked well but I did want to define the equation within theorem definitions, etc, hence the usage. I did like the formatting prefixes in the counter like A-1a you used, and asked about it in my comment above. Thanks – Student Jan 28 '16 at 20:41
  • Sure, sorry I didn't realize you wanted theorems in the first place, I thought that was your approach to solving the problem :) – phfaist Jan 29 '16 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.