6

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?

  • What are you currently using to align or write your equations? – Werner Dec 13 '16 at 20:23
  • @Werner: I usually use align environment for aligning equations – H. R. Dec 13 '16 at 20:25
  • @Werner: Do you think that there may be a solution? :) – H. R. Dec 13 '16 at 20:33
  • 1
    What if you have \sin^2 x+\cos^2x=1 somewhere? I mean, what's the reason for aligning completely unrelated equations at the same point? – egreg Dec 13 '16 at 20:50
  • @egreg: I think that the only reason I can refer to is beauty! :) In some documents I prefer it to be this way. Or maybe the reason is my aligmentoholic disease! ;) – H. R. Dec 14 '16 at 14:59
7

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.

  • (+1) Thanks. Yeah it may look bad for some purposes and look nice for another things! I mentioned at the beginning of my question that it may look insane! :D – H. R. Dec 14 '16 at 14:20
6

Equations can be left aligned by option fleqn. The left indentation can be controlled by register \mathindent (package amsmath). Then the left part of the equation can be written via \llap in the indentation space:

\documentclass[fleqn]{article}
\usepackage{amsmath}
\setlength{\mathindent}{3em}
\newcommand*{\ml}[1]{%
  \llap{$\displaystyle#1$}%
}
\begin{document}
\begin{gather*}
  \ml{a} = b + c + d\\
  {} + e + f + g
\intertext{A small amount of text can go htere with $x=2$ inline math,
but not a lot of text.}
  \ml{k} = l + m + n + m + n + m + n\\
  {} + o + p + q\\
  \ml{r + s} = t + u
\end{gather*}
\end{document}

Result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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