Modify eqnarray to match that of amsmath's align
This is a robust solution based only on making &
active. The code is well commented I hope:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begingroup % we group everything so that our \catcode changes are local, and we use \gdef everywhere
\makeatletter
\gdef\eqna@origamp{&} % `&` stored
\catcode`\&\active % set `&` active in the scope of these hooks
\gdef\eqna@newamp{%
\ifx\@currenvir\eqna@currenvir % if the inner-most environment is `eqnarray`
\eqna@onlyfirstamp\let\eqna@onlyfirstamp\@empty % use `&`, but only once for each line
\else % if there's another environment inside `eqnarray`
\eqna@origamp % use `&` as is
\fi
}
\gdef\eqna@hook{% modification of `eqnarray`
\let\eqna@currenvir\@currenvir % store the current environment, which is `eqnarray` or `eqnarray*`
\catcode`\&\active % `&` has to be active
\let&\eqna@newamp % define `&` expansion
\let\eqna@onlyfirstamp\eqna@origamp % `&` that is used only once on a line
}
\catcode`\*11 % set `*` letter so that we can redefine `eqnarray*`
\gdef\eqnarray{\eqna@hook\align} % `eqnarray` is our hooks and `align`
\gdef\eqnarray*{\eqna@hook\align*} % `eqnarray*` is our hooks and `align*`
\global\let\endeqnarray\endalign
\global\let\endeqnarray*\endalign*
\endgroup
\begin{document}\thispagestyle{empty}
\begin{eqnarray}
f(x) & = & \begin{cases} 1 & x\geq0 \\ 0 & x<0 \end{cases}\\
\begin{pmatrix}1&0\\0&1\end{pmatrix} & = & dx^2 + ex + f \nonumber\\
kkk(x) & = & gx^2 + kx + m
\end{eqnarray}
\begin{align}
f(x) & = \begin{cases} 1 & x\geq0 \\ 0 & x<0 \end{cases}\\
\begin{pmatrix}1&0\\0&1\end{pmatrix} & = dx^2 + ex + f \notag\\
kkk(x) & = gx^2 + kx + m
\end{align}
\end{document}
Transmutation of eqnarray into align
original enthusiasm: I just didn't believe it when I saw it worked. Well it works in this very minimal case, but will certainly break I guess in real life.
When I proposed the method I had hesitated between a an active &
approach (exactly what tochecz
's code seems to do very robustly) and a more down-to-earth approach using delimited arguments. I was very surprised that I got the delimited argument method to work quickly so I posted it here and then extended to multi-line situations. I also wasted some time on extending to many-tabs as I didn't know that eqnarray
was a strictly three column format with two tabs.
However if one wanted to extend the code to allow \begin{env}...\end{env}
that would require quite an effort. Actually possibly recreating the amsmath \collect@body
or similar (perhaps one could just coerce it to serve us here). That's my two pence thought at this stage as I was busy with other things in-between and I come back to see that tochecz
has implemented the idea of transmutation with the active &
technique, obviously the much better choice. So purely by sentimentalism I will leave here some of my initial code.
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newtoks\arraytoaligntoks
%% Initial version for two lines
%%\long\def\eqnarray #1\\#4\end{%
%% \arraytoaligntoks={#1#3\\#4#6}%
%% \edef\endeqnarray{\noexpand\begin{align}\the\arraytoaligntoks
%% \noexpand\end{align}}\end}
%% version for arbitrarily many lines*
%% But this will fail completely if some \begin..\end is not within braces
\def\transmute #1\\{\arraytoaligntoks\expandafter
{\the\arraytoaligntoks #1#3}}
\def\jfbu{\jfbu}
\long\def\eqnarray #1\end{\arraytoaligntoks{}%
\def\\##1\\{\transmute ##1\\%
\def\\####1\\{\ifx\jfbu####1\jfbu\else
\arraytoaligntoks\expandafter{\the\arraytoaligntoks \\}%
\transmute ####1\\\expandafter\\\fi}\\}%
\\#1\\\\%
\edef\endeqnarray{\noexpand\begin{align}\the\arraytoaligntoks
\noexpand\end{align}}\end}
\begin{document}\thispagestyle{empty}
\begin{eqnarray}
f(x) & = & ax^2 + bx + c \\
g(x) & = & dx^2 + ex + f \nonumber\\
k(x) & = & gx^2 + kx + m
\end{eqnarray}
\begin{align}
f(x) & = ax^2 + bx + c \\
g(x) & = dx^2 + ex + f \notag\\
k(x) & = gx^2 + kx + m
\end{align}
\end{document}