[Tex/LaTex] Aligning equations with two equal signs

equationshorizontal alignment

I am trying to write three equations, second with two equal signs. I would like the first two to be aligned by the first equal sign, and the second and third by the second equal sign. I have the following code:

\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{amssymb}

\newcommand{\ddn}[2]{\frac{\mathrm{d}}{\mathrm{d} #1 } #2 }
\newcommand{\ddt}{\frac{\mathrm{d}}{\mathrm{d} t } }

\begin{document}
\begin{eqnarray}
x'(t)&=&\ddt\left(\left[-\frac{M}{\omega^2}\cos{(\omega t)}+B_1\sin{(\omega t)}\right]+
\left[\frac{M}{\omega^2}-\frac{M}{T\omega^2}t\right]\right)\\
{}&=&\frac{M}{\omega}\sin{(\omega t)}+B_1\omega\cos{(\omega t)}-\frac{M}{T \omega^2}\\
\therefore x'(0) &=&
\frac{M}{\omega}\sin{(\omega 0)}+B_1\omega\cos{(\omega 0)}-\frac{M}{T \omega^2}=0\\
\therefore B_1\omega-\frac{M}{T\omega^2}&=& 0
\end{eqnarray}
\end{document}

which produces following output:
Original result

Equations (1), (2), and (3) are aligned as desired, but I wish (4) to be aligned with the second equal sign in (3). Is that feasible?

Best Answer

To achieve this alignment, I would use \mathllap and \mathrlap, but I would use these in conjunction with the alignat environment, to ensure the proper alignment of = symbols on each line.

Example code:

\documentclass[a4paper]{article}
\usepackage{mathtools,amsmathm,amssymb}

\newcommand{\ddn}[2]{\frac{\mathrm{d}}{\mathrm{d}#1}#2}
\newcommand{\ddt}{\frac{\mathrm{d}}{\mathrm{d}t}}

\begin{document}
% Start an equation environment with two sets of aligned equalities
\begin{alignat}{2}
    x'(t) &=
    % RHS "overlaps" anything on its right (occupies no space to its right)
    \mathrlap{\ddt\left(
        \left[-\frac{M}{\omega^2}\cos{(\omega t)}+B_1\sin{(\omega t)}\right] + 
        \left[\frac{M}{\omega^2}-\frac{M}{T\omega^2}t\right]
    \right)}
    % Add a little bit of space on the RHS in the second set of equalities, for balance
    &&\mspace{120mu}
    % Start a new line of the equation but without adding any equation numbers
    \notag\\
    &= \frac{M}{\omega}\sin{(\omega t)}+B_1\omega\cos{(\omega t)}-\frac{M}{T \omega^2}
    % New line and equation number, with some added vertical space
    \\[1.5ex]
    \therefore x'(0) &=
    \frac{M}{\omega}\sin{(\omega 0)}+B_1\omega\cos{(\omega 0)}-\frac{M}{T \omega^2}
    % Second set of equalities
    &&= 0
    % New line and new equation number, with some added vertical space
    \\[1.5ex]
    % Align the new equation at the second set of equalities; LHS "overlaps" anything
    % on its left so that it doesn't push away the second `=' in the previous equation
    && \mathllap{\therefore B_1\omega-\frac{M}{T\omega^2}} &= 0
\end{alignat}
\end{document}

Example output:

Output for alignment at multiple ` symbols using the "alignat" environment