[Tex/LaTex] Aligning All Equations in a Document at Equality Sign

alignamsmathequations

This question may seem a little insane! 🙂 but I am inclined to align all of my equations in a document at the = sign. In fact, I want a general format for all of the equations like below

The distance of equality sign from the left margin which I want to be fixed = right side

and if the left side or right side of the equation are long they should break down to next line aligning from the left.

Here is simple example

\documentclass{article}
\usepackage{amsmath}
\begin{document}

This example shows text and  equations within
an \verb|align| environment.
\begin{align*}
a &= b + c + d\\
  &+ e + f + g
\intertext{A small amount of text can go here with $x=2$ inline math
But not a lot of text.}
k &= l + m + n + m + n + m + n\\
  &+ o + p + q
\end{align*}

\end{document}

But not exactly what I want since

  • the distance of = is not fixed form the left margin
  • if I use this method I should write all of my document in one align environment which will be too confusing!
  • I cannot write too much text between equations

Is there any possible solution for this?

Best Answer

I only considered implementing a global alignment as egreg hinted in comments that it was a bad idea. Alignment is supposed to highlight the corresponding parts of a display and that meaning is destroyed if you align everything. But...

enter image description here

\documentclass{article}

\usepackage[fleqn]{amsmath}
\usepackage{etoolbox}
\setlength\mathindent{3cm}
\makeatletter

\def\align@preamble{%
   &\hfil
    \strut@
    \setboxz@h{\@lign$\m@th\displaystyle{##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \llap{\set@field}%
    \tabskip\z@skip
   &\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \rlap{\set@field}
    \hfil
    \tabskip\alignsep@
}


\def\a{One two three four five six seven. }
\def\b{\a\a\a Red yellow blue green. \a\a\a\a}
\begin{document}

\b\b\a
\begin{align}
  1+2+3+4 &=4\\
x+y &=z
\end{align}

\b\b\b

\a\a\b

\a
\begin{align}
  1&=a+y+b+w+x+n+m+s+d+e\\
  2&=4
\end{align}

\b
\begin{align}
  1&=0\\
2&=9
\end{align}
\end{document}

the macros here are not particularly general or robust but show a basic idea of using a flush left but very indented setting along with hiding the width of the first column.

Related Question