3

I noticed that 98% of the times I use align or aligned I place the & just before the first = of each line. Is there a way to define a new environment that automatically aligns equations to the first equal sign (as if placing an invisible & there)?

Ideally, this code

\begin{myalign}
a = b \\
= c = d
\end{myalign}

would be completely equivalent to this code

\begin{align}
a &= b \\
&= c = d
\end{align}

1 Answer 1

6

I'm not sure this is totally a good idea, but you can add a local definition of = to the alignment preamble:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\def\myalign@preamble{%
   \myeq\hfil
    \strut@
    \setboxz@h{\@lign$\m@th\displaystyle{##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \tabskip\z@skip
   &\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \hfil
    \tabskip\alignsep@
   &&\hfil
    \strut@
    \setboxz@h{\@lign$\m@th\displaystyle{##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \tabskip\z@skip
   &\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \hfil
    \tabskip\alignsep@
}

\def\myeq{\mathcode`\="8000\begingroup\lccode`\~`\=\lowercase{\endgroup\def~{&=}}}

\newenvironment{myalign}
{\let\align@preamble\myalign@preamble\align}
{\endalign}

\begin{document}


\begin{myalign}
 a = b \\
= c = d
\end{myalign}

\begin{align}
a &= b \\
&= c = d
\end{align}
\end{document}
3
  • 4
    I am sure it's not a good idea. :)
    – egreg
    Apr 27, 2012 at 13:34
  • You guys are getting me worried. Will my laptop blow up if I try this?
    – Malabarba
    Apr 27, 2012 at 15:21
  • 4
    No. It's just that ams alignments are fairly delicate code and so poking redefinitions in to them, is inherently fragile. It'll seem to work until you want to merge the document into a conference proceeding and adding that code breaks everyone else's paper, that sort of thing.... For something that's a minor editing convenience (for example an editor such as emacs could add the &s automatically given a bit of encouragement) Apr 27, 2012 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .