Counting equations with two parallel counters

How about just play with counters?

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{environ}
\newif\ifsolution
\solutiontrue
\newcounter{fakeequation}
\newcounter{fakeeqtmp}
\NewEnviron{solution}{\setcounter{fakeeqtmp}{\value{equation}}\setcounter{equation}{\value{fakeequation}}%
    \renewcommand\theequation{S\arabic{equation}}\ifsolution\color{red}\expandafter\BODY\fi%
    \setcounter{fakeequation}{\value{equation}}\setcounter{equation}{\value{fakeeqtmp}}\renewcommand\theequation{\arabic{equation}}}
\begin{document}
\begin{equation}
    2x=4
    \label{eq:one}
\end{equation}

\begin{solution}
    \begin{equation}
        x=2
        \label{eq:solone}
    \end{equation}
\end{solution}

\begin{equation}
    3x=6
    \label{eq:two}
\end{equation}

\begin{solution}
    \begin{equation}
        x = 2
        \label{eq:soltwo}
    \end{equation}
\end{solution}

\end{document}

enter image description here

Explanation

I defined two extra counters fakeequation and fakeeqtmp.

At the start of the solution environment I stashed the value of equation into fakeeqtmp and loaded the value of fakeequation into equation. This is reversed at the end of the environment. So that inside the solution environment the counter being used is really the fakeequation counter, just addressed as equation.

I also included some code to format \theequation so that the equation labels look different inside and outside of the solution environments, included the capital "S" that you wanted.


Use \tag with \ref.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{amsmath}

\newif\ifsolution
\solutiontrue
\NewDocumentEnvironment{solution}{+b}{%
  \ifsolution\color{red}#1\fi
}{}

\begin{document}
\begin{equation}
    2x=4
    \label{eq:one}
\end{equation}

\begin{solution}
    \begin{equation}
        x=2
        \tag{S\ref{eq:one}}
    \end{equation}
\end{solution}

\begin{equation}
    3x=6
    \label{eq:two}
\end{equation}

\end{document}

enter image description here


Presumably both the question and the solution will sometimes have multiple equations, and they may not necessarily appear in the same order in the questions and the solutions, so using parallel counters is not an ideal solution. A better idea, as @egreg suggests, is to use \ref{...} to label the equations combined with the \tag{..} command.

To make this automatic you need to change the behaviour of the equation environment inside a solution environment. The code below redefines the equation environment inside a solution environment so that it is really an equation* environment (i.e. it has no equation number), and then the equation number is added using a \tag command. As well, a reference to the equation in the solution is added automatically. To make this work, inside a solution environment the equation environment requires an extra argument, which is the label of the equation from the questions.

With this in place, the code:

\begin{equation}
    2x=4
    \label{eq:one}
\end{equation}

\begin{solution}
  \begin{equation}{eq:one}% must give the ref to the question eq.
        x=2
    \end{equation}
\end{solution}

\begin{equation}
    3x=6
    \label{eq:two}
\end{equation}

Equations \ref{eq:one}, \ref{eq:onesol} and \ref{eq:two}.

produces:

enter image description here

Notice that the label for the equation in the solutions is the same as the label for the corresponding equation in the question with sol appended.

Here is the full code:

\documentclass{article}
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{xparse}

\newif\ifsolution
\solutiontrue

\NewDocumentEnvironment{solution}{+b}{
  \ifsolution\color{red}
    % redefine the equation* environment
    \RenewDocumentEnvironment{equation}{m +b}{
      \begin{equation*}##2\tag{S\ref{##1}}
      \xdef\@currentlabel{S\noexpand\ref{##1}}% attach an S to the equation label
      \label{##1sol}% add the label
    }{\end{equation*}}
    #1
  \fi
}{}

\begin{document}
\begin{equation}
    2x=4
    \label{eq:one}
\end{equation}

\begin{solution}
  \begin{equation}{eq:one}
        x=2
    \end{equation}
\end{solution}

\begin{equation}
    3x=6
    \label{eq:two}
\end{equation}

Equations \ref{eq:one}, \ref{eq:onesol} and \ref{eq:two}.
\end{document}

Note that I have used the \NewDocumentEnvironment command from the xparse package, together with the +b option (which makes the body of the environment equal to #1...and with the \RenewDocumentEnvironment command above the body becomes #2, since #1 is the equation label), instead of the environ package.

One side effect of this is that you cannot have a labelled equation in the solutions unless it corresponds to an equation in the questions. With a little more effort it is possible to support this as well.