1

I have a set of equations I'd like to align. Below is what it looks like now, but I'd prefer if I could have the first term after each equal sign align with the terms below, ignoring the operator below.

What I have now

This is the code I'm using for the equation above:

\begin{align}
\ell
={}
&  \mu_0 - y_0 \log \mu_0\\
&+ \mu_b - y_b \log \mu_b\\
&+ \mu_r - y_r \log \mu_r\\
&+ \mu_{br} - y_{br} \log \mu_{br}\\[4pt]
={}
&  \pi_0 U - y_0 \log \left( \pi_0 U \right)\\
&+ \pi_b U d_b - y_b \log \left( \pi_b U d_b\right)\\
&+ \pi_r U d_r - y_r \log \left( \pi_r U d_r \right) \\
&+ \pi_{br} U d_b d_r - y_{br} \log \left( \pi_{br} U d_b d_r \right) \\[4pt]
={}
&  \left(\pi_0 + \pi_b d_b + \pi_r d_r + \pi_{br} d_b d_r\right) U \\
&- (y_0 + y_b + y_r + y_{br}) \log U \\
&- y_0 \log \pi_0 \\
&- y_b \log \left( \pi_b d_b \right) \\
&- y_r \log \left( \pi_r d_r \right) \\
&- y_{br} \log \left( \pi_{br} d_b d_r \right) \\
\end{align}

I'd like it to look like the image below (achieved with some image editing):

What I'd like

I've tried adding a \phantom{+} in front of the first terms, and this does add some spacing, but it doesn't align correctly.

What's the best way to get the alignment I'm looking for?

  • 1
    \mathbin{\phantom{+}} would be the construction, but I'd go with the first version. Never use $$; around align they're even wronger. – egreg Oct 19 at 19:33
  • Perfect! That does it! And yes, I admit that I might have slightly demented taste for alignment :) – AJ Friend Oct 19 at 19:45
  • The $$ is just because the snippet is from writing TeX within a markdown document. – AJ Friend Oct 19 at 19:47
  • I suspected that; remove them nonetheless. – egreg Oct 19 at 19:48
2

You can use \mathbin{\vphantom{+}}, so the object will behave in everything like a +.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
\ell={}
&\mathbin{\phantom{+}}  \mu_0 - y_0 \log \mu_0\\
&+ \mu_b - y_b \log \mu_b\\
&+ \mu_r - y_r \log \mu_r\\
&+ \mu_{br} - y_{br} \log \mu_{br}\\[4pt]
={}
&\mathbin{\phantom{+}}  \pi_0 U - y_0 \log ( \pi_0 U )\\
&+ \pi_b U d_b - y_b \log ( \pi_b U d_b)\\
&+ \pi_r U d_r - y_r \log ( \pi_r U d_r ) \\
&+ \pi_{br} U d_b d_r - y_{br} \log ( \pi_{br} U d_b d_r ) \\[4pt]
={}
&\mathbin{\phantom{+}}  (\pi_0 + \pi_b d_b + \pi_r d_r + \pi_{br} d_b d_r) U \\
&- (y_0 + y_b + y_r + y_{br}) \log U \\
&- y_0 \log \pi_0 \\
&- y_b \log ( \pi_b d_b ) \\
&- y_r \log ( \pi_r d_r ) \\
&- y_{br} \log ( \pi_{br} d_b d_r )
\end{align}

\end{document}

enter image description here

However, I like this like a punch in the eye, even if we remove all numbers, which I suspect you don't actually want, because the code is supposed to be fed to MathJax. I checked and it works also there.

Note that I removed all the \left and \right tokens, whose only effect in this case is to add unwanted spaces.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align*}
\ell&=
         \mu_0 - y_0 \log \mu_0\\
&\qquad+ \mu_b - y_b \log \mu_b\\
&\qquad+ \mu_r - y_r \log \mu_r\\
&\qquad+ \mu_{br} - y_{br} \log \mu_{br}\\[4pt]
&=
         \pi_0 U - y_0 \log ( \pi_0 U )\\
&\qquad+ \pi_b U d_b - y_b \log ( \pi_b U d_b)\\
&\qquad+ \pi_r U d_r - y_r \log ( \pi_r U d_r ) \\
&\qquad+ \pi_{br} U d_b d_r - y_{br} \log ( \pi_{br} U d_b d_r ) \\[4pt]
&=
         (\pi_0 + \pi_b d_b + \pi_r d_r + \pi_{br} d_b d_r) U \\
&\qquad- (y_0 + y_b + y_r + y_{br}) \log U \\
&\qquad- y_0 \log \pi_0 \\
&\qquad- y_b \log ( \pi_b d_b ) \\
&\qquad- y_r \log ( \pi_r d_r ) \\
&\qquad- y_{br} \log ( \pi_{br} d_b d_r )
\end{align*}

\end{document}

enter image description here

| improve this answer | |

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.