3

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}{%
    \addtocounter{equation}{-1}
    \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}
3

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}

\begin{equation}
1=1
\end{equation}

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

\begin{equation}
1=1
\end{equation}


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

\begin{equation}
1=1
\end{equation}

\end{document}

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

enter image description here

  • 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. – Randy Cragun Oct 5 '15 at 23:08
  • @RandyCragun I changed according to my style, I hope you don't mind. – egreg Oct 5 '15 at 23:15

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.