TransWikia.com

Align equations as if first term has operator (+ or -) in front

TeX - LaTeX Asked by AJ Friend on February 25, 2021

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_bright)
&+ 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_rright) 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?

One Answer

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

Correct answer by egreg on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP