# Increase counter for each line in custom align environment

What is a good way to advance the counter in a custom align environment? I defined the regalign environment below, but to get the counter to advance with each line, I had to define a new command (\regline) to both add the new line and change the counter. This is mostly fine, but it seems like a poor choice, because the syntax differs from similar environments' (making it difficult to read and write the code) and because editor highlighting will flag alignment characters after the first line (e.g. the & in z &= xy^2) as errors.

\documentclass{article}
\usepackage{amsmath}

% Set the counter for the regression environment
\newcounter{regcounter}
% Define the regression environment
\newenvironment{regalign}{%
\stepcounter{regcounter}
\renewcommand\theequation{Regression \theregcounter}
\align
}{
\endalign
}
% Define a command for new lines that advances the counter
\newcommand{\regline}{\\[1em] \stepcounter{regcounter}}

\begin{document}
\begin{regalign}
x & = y \regline
z &= xy^2
\end{regalign}
\end{document}


I suggest a method similar to what amsmath does for subequations: use a counter for storing the current value of equation and then redefine it to use the required format.

\documentclass{article}
\usepackage{amsmath}

\newcounter{tempcounter}
% Define the regression environment
\newcounter{regcounter}
\newenvironment{regalign}
{%
\setcounter{tempcounter}{\value{equation}}%
\setcounter{equation}{\value{regcounter}}%
\def\theequation{Regression \arabic{equation}}%
\align
}
{%
\endalign
\setcounter{equation}{\value{tempcounter}}%
\setcounter{regcounter}{\value{equation}}%
\stepcounter{regcounter}
}

\begin{document}

$$1=1$$

\begin{regalign}
x & = y \\
z &= xy^2
\end{regalign}

$$1=1$$

\begin{regalign}
x & = y \\
z &= xy^2
\end{regalign}

$$1=1$$

\end{document}


The equation environments are to show that the main equation counter is not affected by the regalign environment in between.

• I edited your answer to make the numbering continue in the next regalign environment, because that was the behavior in the initial question. Feel free to change it back. Oct 5 '15 at 23:08
• @RandyCragun I changed according to my style, I hope you don't mind. Oct 5 '15 at 23:15