Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

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}
share|improve this answer
4  
I am sure it's not a good idea. :) –  egreg Apr 27 '12 at 13:34
    
You guys are getting me worried. Will my laptop blow up if I try this? –  Malabarba Apr 27 '12 at 15:21
3  
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) –  David Carlisle Apr 27 '12 at 15:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.