# Align multiple eqnarray*

I am using multiple eqnarray* in one document, but the align doesnt look nice:

\begin{eqnarray*}
\bar{x}& = & \frac{1}{n} \sum_{i=1}^n x_i\\
& = & \frac{45+60+...+53+65}{10}\\
& = &\frac{284}{5}\\
\end{eqnarray*}
\subsection{Another one}
Some text and other tings...
\begin{eqnarray*}
s^2 & = & \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2\\
& = & \frac{1}{9} ((45-57,2)^2+(60-57,2)^2+\dots+(65-57,2)^2)\\
\end{eqnarray*}


If you look at the result, you can see that not all the equal signs align equally. The grouping with eqnarray* works, but because we have to insert some other stuff (like subsections), we can't continue the eqnarray* to force all equal signs from both eqnarray* to align the same way. How can we do this?

• note it is strongly recommended to use align* not eqnarray* although you would have the same issue here – David Carlisle Nov 23 '16 at 13:02
• Welcome to TeX.SX! I see no reason for aligning unrelated equations in different subsections. – egreg Nov 23 '16 at 13:05
• Adding multiple paragraphs between aligned equations might help, though I don't know if it works across multiple (sub)sections (or why you would want to do this). – Ian Thompson Nov 23 '16 at 14:39
• – Torbjørn T. Nov 23 '16 at 14:50

I can think of no convincing reason for aligning unrelated equation structures across sectioning units of the document. However, if you absolutely, positively must perform such an alignment, it can be achieved, but only through manual intervention. In the solution provided below, parts of the upper equation are encased in \parbox statements, where the widths of the boxes are determined by the longest items to the left and right of the = symbols in the lower equation.

As others have already remarked: Don't use eqnarray -- it's very badly deprecated, and far-superior alternatives are readily available. Also, don't type ...; use \dots instead.

\documentclass{article}
\usepackage{amsmath} % for 'align*' environment

\newlength\mylena
% save longest string of second equation block
\newcommand\longstring{\frac{1}{9}((45-57,2)^2+(60-57,2)^2+\dots+(65-57,2)^2)}
\settowidth\mylena{$\displaystyle\longstring$} % measure width of this string

\newlength\mylenb
\settowidth\mylenb{$\displaystyle s^2$} % measure width of this string too

\begin{document}

\begin{align*}
\parbox{\mylenb}{\hfill$\displaystyle\bar{x}$} % right-align the material
&= \frac{1}{n} \sum_{i=1}^n x_i\\
&= \frac{45+60+\dots +53+65}{10}\\
\parbox{\mylenb}{}
&= \parbox{\mylena}{$\displaystyle\frac{284}{5}$}\\
\end{align*}

\subsection{Another one}
Some text and other things \dots
\begin{align*}
s^2 &= \frac{1}{n-1} \sum_{i=1}^n (x_i-\bar{x})^2\\
&= \longstring\\
\end{align*}
\end{document}