# New gather and equation environments with a single counter

So I've created a new equation-style environment with an associated counter to track a certain set of definitions I introduce in my document:

\newcounter{defcounter}
\newenvironment{defeq}{%
\refstepcounter{defcounter}
\renewcommand\theequation{D.\thedefcounter}
$$} {$$}


This produces the desired result: a new environment defeq just like equation, but with its own counter and its own label format (e.g., (D.1)).

But occasionally I'll have several definitions in a row and want to use a gather environment to avoid the ugly vertical spacing that results from consecutive equation environments. A slight modification of the above kind of works:

\newenvironment{defgather}{%
\refstepcounter{defcounter}
\renewcommand\theequation{D.\thedefcounter}
\gather}
{\endgather}


However, linebreaking with \\ doesn't seem to step the newly created counter. The following MWE illustrates the issue:

\documentclass{article}
\usepackage{amsmath}
\newcounter{defcounter}
\newenvironment{defeq}{%
\refstepcounter{defcounter}
\renewcommand\theequation{D.\thedefcounter}
$$} {$$}

\newenvironment{defgather}{%
\refstepcounter{defcounter}
\renewcommand\theequation{D.\thedefcounter}
\gather$$} {$$\endgather}
\begin{document}

\begin{defeq}
1
\end{defeq}

Text text text text.

\begin{defgather}
2 \\
3
\end{defgather}

\end{document}


The lines containing "1" and "2" are appropriately labelled -- (D.1) and (D.2). However, the line containing "3" is labelled (D.2) indicating that the \\ hasn't stepped defcounter as the default gather does with the equation counter.

How can I fix my new gather environment so that \\ steps the new counter?

• The equation counter is stepped with every defeq as well. Is this the expected behaviour? That is, if you put an equation before and after everything, you'll have a numbering 1, D.1, D.2, D.2, 5. – Werner Jul 24 '15 at 5:11
• @Werner No, no it isn't. The counters are supposed to be independent. My MWE is even more messed up than I realized. – Dennis Jul 24 '15 at 5:18

It's easiest to \let the defcounter to that of equation within your environment(s). That way defcounter is stepped whenever equation would be:

\documentclass{article}
\usepackage{amsmath}
\newcounter{defcounter}

\makeatletter
\newenvironment{defeq}{%
\let\c@equation\c@defcounter% equation is equivalent to defcounter
\renewcommand\theequation{D.\arabic{equation}}
$$} {$$}

\newenvironment{defgather}{%
\let\c@equation\c@defcounter% equation is equivalent to defcounter
\renewcommand\theequation{D.\arabic{equation}}
\gather$$} {$$\endgather}
\makeatother

\begin{document}

$$abc \label{eq:first}$$

\begin{defeq}
1 \label{deq:first}
\end{defeq}

There is~\eqref{eq:first}, \eqref{deq:first}, \eqref{deq:second},~\eqref{deq:third}
and~\eqref{eq:second}.

\begin{defgather}
2 \label{deq:second} \\
3 \label{deq:third}
\end{defgather}

$$abc \label{eq:second}$$

\end{document}

• Works perfectly! Would you mind explaining what the \c@ construct does? I've seen this many times in answers from more experienced users but still seems like black magic to me. The only mention I've seen of it is in this answer, is that all it's doing here? Is \c@ how lower leveled commands that reference the counters are named? – Dennis Jul 24 '15 at 6:02
• @Dennis: \newcounter{mycount} defines a counter register to a macro \c@mycount. And to access @ symbols you need a \makeatletter...\makeatother pair. All I did was to sync the counter register of equation with that of defcounter (through \let\c@equation\c@defcounter). This way, when TeX steps equation (and therefore the counter \c@equation, it actually steps \c@defcounter, leaving the original \c@equation untouched. – Werner Jul 24 '15 at 6:09
• @Dennis: So, we make TeX think that "Dave" is now "Dan". Every call to "Dave" is actually "Dan"... only temporarily, since the switch is grouped and therefore have limited scope. The true "Dave"-"Dan" relation is reestablished after the environment closes. – Werner Jul 24 '15 at 6:18