# How to aligned long equations and shift equal sign to fit

I would like to fit the long equation derivations by shifting the left aligned equal signs to left.

 \begin{align*}
\MoveEqLeft
p(y = 1\mid x; \phi, \mu_0, \mu_1, \Sigma) \\
&= \frac{p(x\mid y=1)p(y=1)}{p(x)} \\
&= \frac{p(x\mid y=1)p(y=1)}{p(x\mid y=1)p(y=1)+p(x\mid y=0)p(y=0)}&\\
&= \frac{\frac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi}{\frac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi + \frac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_{0})^T \Sigma^{-1} (x-\mu_{0})\right) (1-\phi)} \\
&= \frac{1}{1+\exp \left( -\frac{1}{2}(x-\mu_{0})^T \Sigma^{-1} (x-\mu_{0}) + \frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1)\right) \frac{1-\phi}{\phi}} & (\text{divide by numerator}) \\
&= \frac{1}{1+\exp \left( \log \frac{1-\phi}{\phi} +\frac{1}{2}
\left[ (x-\mu_1)^T \Sigma^{-1} (x-\mu_1) - (x-\mu_{0})^T \Sigma^{-1} (x-\mu_{0})
\right]\right)}
\end{align*}


Here's the screenshot. You can see that in the third equal sign, it's too long to fit. How could I shift all aligned equal signs further left to fit that long equation.

• Welcome to TeX.SX! Do you want the equal signs to be aligned with any specific position on the first line? (And if this is not the case, why are you using flalign*, then? ;-) – GuM Oct 11 '19 at 22:54
• Please extend your code fragment to complete small document which reproduce your problem. >it seems that in your preamble you have defined some new commands or use some unknown packages. SO far your code is not compilable. – Zarko Oct 11 '19 at 23:01
• @GuM I am still new to Tex so don't know much about aligned yet. I thought i will shift the whole thing left so that the long equation will fit in. No, the equal signs don't need to be aligned with any specific position on the first line as long as the long equations can fit. – user199188 Oct 11 '19 at 23:07
• Then, I suggest you use the mathtools package: open its manual (texdoc mathtools) and have a look at Subsection 3.4.4, which begins on p. 20. (BTW, how is \di defined? ;-) – GuM Oct 11 '19 at 23:07
• Possibly related (or original of duplicate question): Formatting of equations. – GuM Oct 11 '19 at 23:16

One way is to write denominator in the last equation in two lines:

\documentclass{article}
\usepackage{mathtools, nccmath}
\newcommand\di{\mathrm{d}}

%---------------- show page layoutdon't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
\begin{align}
\MoveEqLeft[1]
p(y = 1\mid x; \phi, \mu_0, \mu_1, \Sigma)      \\
& = \frac{p(x\mid y=1)p(y=1)}{p(x)}         \\
& = \frac{p(x\mid y=1)p(y=1)}{p(x\mid y=1)p(y=1)+p(x\mid y=0)p(y=0)}    \\
& = \frac{\mfrac{1}{(2\pi)^{\di/2} |\Sigma|^{1/2}}\exp\left(-\frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi}
{\left(\begin{multlined}[0.85\hsize]
\mfrac{1}{(2\pi)^{\di/2} |\Sigma|^{1/2}} \exp\left(-\mfrac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi \\
+ \mfrac{1}{(2\pi)^{\di/2} |\Sigma|^{1/2}} \exp\left(-\mfrac{1}{2}(x-\mu_0)^T \Sigma^{-1} (x-\mu_0) \right) (1-\phi)
\end{multlined}\right)}
\end{align}
\end{document}


Note, you not provide information what is \di. Please redefine it or add package to preamble, which it define.

• In the denominator of the third fraction, ...\phi + should be ...\phi + {}, shouldn’t it? Edit: Also, the OP made it clear that \di should rather be d, arguably an ordinary variable. – GuM Oct 11 '19 at 23:37
• … You are right, in displays lines break before binary operators! :-) – GuM Oct 11 '19 at 23:47
• @GuM, in my answer I didn't check showed result of \di in image :-( . This can correct OP himself. This should not be big deal for him :-) – Zarko Oct 11 '19 at 23:53

You can set this in a regular align* with the left-hand side in a zero-width box (\mathrlap). Then you can insert a space to your liking, which will move the right-hand side according to your needs. Below I've used 5em, so change this to suit your needs.

\documentclass{article}

\usepackage[
margin=1in,
landscape
]{geometry}% Just for this example

\usepackage{mathtools}

\newcommand{\di}{\delta}

\begin{document}

\begin{align*}
\mathrlap{p(y = 1 \mid x; \phi, \mu_0, \mu_1, \Sigma)}
\hspace{5em} & \\ % <---------- Change to suit your needs
&= \frac{p(x \mid y = 1) p(y = 1)}{p(x)}\\
&= \frac{p(x \mid y = 1) p(y = 1)}{p(x \mid y = 1) p(y = 1) + p(x \mid y = 0) p(y = 0)} \\
&= \frac{\frac{1}{(2 \pi)^{\di / 2} |\Sigma|^{1 / 2}}
\exp\bigl(-\frac{1}{2} (x - \mu_1)^T \Sigma^{-1} (x - \mu_1) \bigr) \phi}{\frac{1}{(2 \pi)^{\di / 2} |\Sigma|^{1 / 2}}
\exp\bigl(-\frac{1}{2} (x - \mu_1)^T \Sigma^{-1} (x - \mu_1) \bigr) \phi + \frac{1}{(2 \pi)^{\di / 2} |\Sigma|^{1 / 2}}
\exp\bigl(-\frac{1}{2} (x - \mu_0)^T \Sigma^{-1} (x - \mu_0) \bigr) (1 - \phi)}
\end{align*}

\end{document}


In general, such large equations can be simplified by using a variable to denote common elements, like the fraction in front of exp.

You can combine the \MoveEqLeft command for the first line ( I grouped the first two lines) with the geometry package to have more sensible default matgins (unless you use \marginpar and the medium-sized fractions from nccmath:

\documentclass{article}
\usepackage[showframe]{geometry}

\usepackage{mathtools, amssymb, nccmath}

\begin{document}

\begin{align*}
\MoveEqLeft p(y = 1\mid x; \phi, \mu_0, \mu_1, \Sigma) = \frac{p(x\mid y=1)p(y=1)}{p(x)} \\
&= \frac{p(x\mid y=1)p(y=1)}{p(x\mid y=1)p(y=1)+p(x\mid y=0)p(y=0)} \\
&= \mfrac{\cfrac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi}{\cfrac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_1)^T \Sigma^{-1} (x-\mu_1) \right) \phi + \cfrac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu_{0})^T \Sigma^{-1} (x-\mu_{0})\right) (1-\phi)}
\end{align*}

\end{document}


• Thanks for this suggestion, today I learnt \MoveEqLeft tag, which is so much usable for my future projects... – MadyYuvi Oct 12 '19 at 4:45