# Eqnarray mutiple equals in each line

I'd like to produce the following array of aligned equations:

\begin{eqnarray*}
\operatorname{Cov}\left(u^{\prime} X, v^{\prime} Y\right) &=& u^{\prime} \Sigma_{xy} v &=& \alpha^{\prime} M \beta \\
\operatorname{Cov}\left(u^{\prime} X, u_{j}^{\prime} X\right)&=& u^{\prime} \Sigma_{xx} u_{j} &=& \alpha^\prime \alpha_j \\
\operatorname{Cov}\left(v^{\prime} Y, v_{j}^{\prime} Y\right)&=& v^{\prime} \Sigma_{yy} v_{j} &=& \beta^\prime  \beta_j
\end{eqnarray*}


But the above gives

The problem is the right-most column is not in the right place. I tried googling for a while and kept finding posts like eqnarray vs align telling me to use align instead. However, align didn't seem to give good spacing either; and existing tutorials on align only have one equals sign on each line, whereas I want two.

What should I do? Thanks!

• unrelated but there is never a need to write u^{\prime}  you can simply use u' which gives identical output. Commented Nov 4, 2021 at 16:13
• you should definitely use the ams alignments not eqnarray the posts that your found were correct. Commented Nov 4, 2021 at 16:14

Don't use eqnarray under any circumstance: it's buggy and inflexible.

What you need is alignat.

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\Cov}{Cov}

\begin{document}

\begin{alignat*}{2}
\Cov(u' X, v' Y)     &= u' \Sigma_{xy} v      &&= \alpha' M \beta \\
\Cov(u' X, u_{j}' X) &= u' \Sigma_{xx} u_{j}' &&= \alpha' \alpha_j \\
\Cov(v' Y, v_{j}' Y) &= v' \Sigma_{yy} v_{j}' &&= \beta'  \beta_j
\end{alignat*}

\end{document}


Note that I abbreviated \operatorname{Cov} into \Cov and also ^{\prime} into ' (clearer and easier to type). I also removed \left and \right that do nothing there other than adding unwanted spaces.

For your use case, no out-of-the-box solution will work quite right. Instead, we'll have to build it up from array manually. Getting equation numbers would be a little tricky, but fortunately you don't want those. I'll also set things up so that this looks like eqnarray for input (but with improved spacing)

\NewDocumentEnvironment{eqnarray3*}{}
{
\displaymath % ❶
\begin{array}
{
@{} % ❷
r % Left hand column
@{\;} % ❸
c % first equals sign
@{\;} % ❸
c % middle column
@{\;} % ❸
c % second equals sign
@{\;} % ❸
l % last column
@{} % ❷
}
}
{
\end{array}
\enddisplaymath % ❶
}


We use \displaymath\enddisplaymath ❶ to get into and out of display math mode. This has the advantage of also automatically getting us the ignore spaces effect of \end{displaymath} (if we had used $$ or \begin{displaymath}\end{displaymath},¹ we would get a spurious space at the beginning of the first line of a paragraph that continues after the displayed math.

We put at the beginning and ending columns @{} ❷ to avoid the normal inter-column spacing showing up at the beginning/end of the row. It shouldn't make a difference although there's a possibility that it can save you from overfull \hbox warnings on edge-case displays.

And finally, for the columns with the equals signs, we surround those with @{\;} which gives us the normal spacing around a relation rather than the huge gaps of eqnarray. It's worth noting that because of how you want to do alignments, it makes sense to keep relations in their own columns since there's no guarantee that the middle column will be of consistent width.

With the above definition, you can write:

\begin{eqnarray3*}
\operatorname{Cov}\left(u^{\prime} X, v^{\prime} Y\right) &=& u^{\prime} \Sigma_{xy} v &=& \alpha^{\prime} M \beta \\
\operatorname{Cov}\left(u^{\prime} X, u_{j}^{\prime} X\right)&=& u^{\prime} \Sigma_{xx} u_{j}^{\prime} &=& \alpha^\prime \alpha_j \\
\operatorname{Cov}\left(v^{\prime} Y, v_{j}^{\prime} Y\right)&=& v^{\prime} \Sigma_{yy} v_{j}^{\prime} &=& \beta^\prime  \beta_j
\end{eqnarray3*}


and you should get the desired output.²

1. Maybe the latter would work, I haven't bothered to check.
2. But as is typical with my answers, I typed this off the top of my head and didn't bother to test.
• Well, alignat works well. Commented Nov 4, 2021 at 16:14
• as egreg says, also I'm not sure \displaymath is having the desired effect here as the whole thing is set in \textstyle, being in array cells. Commented Nov 4, 2021 at 16:16
• @DavidCarlisle \displaymath is to get into math mode, not to enforce display style, which, tbh, I hadn't considered. and for the OP's use case works well enough. I was aiming for a template for generalized setting of alignments with this answer, but alignat is definitely a mentally lower-cost solution. Commented Nov 4, 2021 at 16:36