# Aligning equations with two equal signs

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}{\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: 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?

• Welcome to TeX.SX! eqnarray is outdated, use align or a similar environment
– user31729
Apr 10, 2015 at 1:33

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}{\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: Still another possibility: I would center the final equation rather than align its = symbol with the one on the preceding line. To enhance the visual impact, I would also increase the vertical offset between equations (3) and (4). \documentclass[a4paper]{article}
\usepackage{mathtools,amssymb}

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

\begin{document}
\begin{align}
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}\\
x'(0) &= \frac{M}{\omega}\sin{(\omega 0)}+B_1\omega\cos{(\omega 0)}-\frac{M}{T \omega^2}=0\\[2ex]
\end{align}
\end{document}


I would suggest still another variant, also with alignat:

\documentclass[a4paper]{article}
\usepackage{mathtools}
\usepackage{amssymb}
\newcommand{\ddn}{\frac{\mathrm{d}}{\mathrm{d} #1 } #2 }
\newcommand{\ddt}{\frac{\mathrm{d}}{\mathrm{d} t } }

\begin{document}

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

\end{document} • Is there a reason you specifically recommend a different alignment than that asked for by the OP? This doesn't look too bad, but it is basically just left-aligning everything after Eqn.(1), and arbitrarily doing so at the equality symbol of Eqn.(1). Apr 10, 2015 at 5:43
• It' only that it looks better, my opinion. It's not really a recommendation, just a suggestion. You didn't notice it, but the two = signs of eqs 2 and 3are also designed to be aligned. Apr 10, 2015 at 11:04

Use the amsmath package and \begin{align} / \end{align} set for a tighter look.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amssymb}

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

\begin{document}
\begin{align}
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 \\
& \hspace{34mm} \therefore B_1\omega-\frac{M}{T\omega^2} = 0
\end{align}
\end{document}

• You should probably revise your answer so that it compiles. But what about the alignment asked for by the OP? Apr 10, 2015 at 5:42
• @NieldeBeaudrap Where may I find the information needed to correct the code so that it does compile on site? Apr 10, 2015 at 17:59
• It doesn't compile on the site: users compile it locally and upload images of the compiled result. So you should check that your code compiles on your computer. (I would recommend that if you're posting something that looks like a complete document, that it include the \begin{document} ... \end{document}` as well!) Apr 10, 2015 at 18:02
• @NieldeBeaudrap Thanks for the help. Cut and pasting is great as long as the obvious is not missed. begin/end{document} is essential. Apr 10, 2015 at 18:07
• An image of the output would be helpful for viewers to determine whether copy and pasting this code will produce something they desire. May 30, 2020 at 15:00