How do I push the tag to my custom environment equation to be flush right?

The tag for the custom environment housing an equation is not placing the tag flush right. I may not have the right search terms because I am not finding the solution for this issue. Here is my MWE:

\documentclass{article}

\usepackage{mathtools}

% PRB: Tag placement on a line does not position flush right.
\newcounter{derivativeFormula}
\renewcommand*{\thederivativeFormula}{\textsc{df}\arabic{derivativeFormula}}
\usepackage{zref-savepos}
\makeatletter
\newenvironment{DerivativeFormula}{                               %
\def\equation{\refstepcounter{derivativeFormula}}           % order
\def\@eqnnum{{\normalfont \normalcolor \thederivativeFormula}}% is
\equation                                                     % important!
}{                                                                %
\@gobble                                                      %
\print@eqnum{(\thederivativeFormula)}                         %
\endequation                                                  %
}
\makeatother

\begin{document}

A regular equation which should be reference (1).
$$\label{eqn:testA} \frac{dx}{dx} = 1$$

A regular equation which should be reference (2).
$$\label{eqn:testB} \frac{d(u+v)}{dx} = \frac{du}{dx}+\frac{dv}{dx}$$

This is the \texttt{DerivativeFormula} environment. I want the tag
to be flush right. It should read (DF1) but way that way -->.
\begin{DerivativeFormula}\label{eqn:testC}
\frac{d(uv)}{dx} = u\frac{du}{dx} + v\frac{du}{dx}
\end{DerivativeFormula}

Another plain equation to check numbering and reference (3).
$$\label{eqn:testD} \frac{du^n}{dx} = nu^{n-1}\frac{du}{dx}$$

The values of the references are, in order,
\eqref{eqn:testA}, \eqref{eqn:testB}, \eqref{eqn:testC}, \eqref{eqn:testD}.

\end{document}


The key is to use \tag, Barbara reminds me that \tag works inside of equation when amsmath (or mathtools) is loaded, thus I EDITED to replace my choice of gather with the equation environment.

\documentclass{article}

\usepackage{mathtools}

\newcounter{derivativeFormula}
\renewcommand*{\thederivativeFormula}{\textsc{df}\arabic{derivativeFormula}}
\usepackage{zref-savepos}
\makeatletter
\newenvironment{DerivativeFormula}{
\refstepcounter{derivativeFormula}
\equation
}{
\tag{\thederivativeFormula}
\endequation
}
\makeatother
\begin{document}
A regular equation which should be reference (1).
$$\label{eqn:testA} \frac{dx}{dx} = 1$$

A regular equation which should be reference (2).
$$\label{eqn:testB} \frac{d(u+v)}{dx} = \frac{du}{dx}+\frac{dv}{dx}$$

This is the \texttt{DerivativeFormula} environment. I want the tag
to be flush right. It should read (DF1) but way that way $\rightarrow$.
\begin{DerivativeFormula}\label{eqn:testC}
\frac{d(uv)}{dx} = u\frac{dv}{dx} + v\frac{du}{dx}
\end{DerivativeFormula}

Another plain equation to check numbering and reference (3).
$$\label{eqn:testD} \frac{du^n}{dx} = nu^{n-1}\frac{du}{dx}$$

The values of the references are, in order,
\eqref{eqn:testA}, \eqref{eqn:testB}, \eqref{eqn:testC}, \eqref{eqn:testD}.
Here is the next derivative formula:
\begin{DerivativeFormula}\label{eqn:testZ}
\frac{d(uw)}{dx} = u\frac{dw}{dx} + w\frac{du}{dx}
\end{DerivativeFormula}
\end{document}


p.s. I also corrected the math in (DF1).

• If you load amsmath (or mathtools), you can use tag in eguations. – John Kormylo Mar 15 '18 at 13:12
• My original environment was based on a solution provided by tex.stackexchange.com/questions/84254. I see your solution much more elegant. – kpo7057 Mar 15 '18 at 13:13
• you don't want to leave a blank line before the last display. the spacing is bad, and this could allow the page to be broken before the display. also, since mathtools loads amsmath, there is no reason that \tag cannot be used; if you find otherwise, it's a bug and should be reported as such. – barbara beeton Mar 15 '18 at 17:26
• @barbarabeeton Thanks. I have corrected both items you cite. – Steven B. Segletes Mar 15 '18 at 17:33