如何对齐长方程式并移动等号以适应

如何对齐长方程式并移动等号以适应

我想通过将左对齐的等号向左移动来适应长方程的推导。

 \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*}

这是屏幕截图。您可以看到,第三个等号太长了,无法容纳。我该如何将所有对齐的等号进一步向左移动以容纳这个长等式。

答案1

一种方法是将最后一个等式中的分母写​​成两行:

在此处输入图片描述

\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}

注意,您没有提供 是什么的信息\di。请重新定义它或将其定义的包添加到序言中。

答案2

您可以将其设置为常规格式align*,左侧为零宽度框 ( \mathrlap)。然后您可以根据自己的喜好插入空格,这将根据您的需要移动右侧。下面我使用了5em,因此请更改它以满足您的需要。

在此处输入图片描述

\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}

一般来说,这种大型方程可以通过使用变量来表示常见元素(如 前面的分数)来简化exp

答案3

您可以将\MoveEqLeft第一行的命令(我将前两行分组)与geometry包结合起来,以获得更合理的默认 matgins(除非您使用\marginpar 和来自的中等大小的分数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} 

在此处输入图片描述

相关内容