对齐问题

对齐问题

我目前有

\textbf{Theorem 4: } $\mathrm{E}[XY]=\mathrm{E}[X]\mathrm{E}[Y]$

\begin{flalign*}
\textbf{Proof: }     \mathrm{E}[XY]&=\sum_j\sum_i (x_i y_j)\mathrm{P}     
 (x_i)\mathrm{P}(y_j)=\sum_j\sum_i x_i\mathrm{P}(x_i) \ y_j\mathrm{P}(y_j) \\
 &=\sum_j y_j\mathrm{P}(y_j)\ \sum_i x_i\mathrm{P}(x_i)=\mathrm{E}     
 [X]\mathrm{E}[Y]
\end{flalign*}

以图像形式给出定理和证明当前格式

证明是缩进的,与定理不一致,并且所有这些都与文档的其余部分不一致(由黑线表示)。

我的问题是如何创建如下图所示的内容: 在此处输入图片描述

我尝试过将所有内容都包括在内,\begin{flalign*}并用它&来对齐所有内容,但最终却导致证明进一步缩进。

答案1

我会将整个结构放在里面flalign而不是将它们分开:

在此处输入图片描述

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{flalign*}
  \textbf{Theorem 4: } \mathrm{E}[XY] &= \mathrm{E}[X]\mathrm{E}[Y] & \\
  \makebox[0pt][l]{\bfseries Proof: }\hphantom{\textbf{Theorem 4: }}
    \mathrm{E}[XY] &= \sum_j\sum_i (x_i y_j)\mathrm{P}
    (x_i)\mathrm{P}(y_j)=\sum_j\sum_i x_i\mathrm{P}(x_i) \ y_j\mathrm{P}(y_j) \\
                                      &= \sum_j y_j\mathrm{P}(y_j)\ \sum_i x_i\mathrm{P}(x_i) = \mathrm{E}[X]\mathrm{E}[Y]
\end{flalign*}

\end{document}

Proof放置在零宽度框内以实现显示对齐Theorem 4(使用\hphantom)。

注意第一行使用了额外的对齐操作符&。这样做是为了实现左对齐显示。

答案2

如果你坚持用不寻常的方式写定理、证明、引理……那么flaling你可以得到:

\documentclass{article}
    \usepackage{empheq}

\usepackage[active,tightpage]{preview}% for showing equations only
\PreviewEnvironment{flalign*}
    \setlength\PreviewBorder{1em}

\begin{document}

\begin{flalign*}
\textbf{Theorem 4:} &&  
    \mathrm{E}[XY] & = \mathrm{E}[X]\mathrm{E}[Y]                               \\
\textbf{Proof:} &&  
    \mathrm{E}[XY] & = \sum_j\sum_i (x_i y_j)\mathrm{P} (x_i)\mathrm{P}(y_j) 
                        = \sum_j\sum_i x_i\mathrm{P}(x_i) \ y_j\mathrm{P}(y_j)  \\
                &&&= \sum_j y_j\mathrm{P}(y_j)\ \sum_i x_i\mathrm{P}(x_i)=\mathrm{E}
 [X]\mathrm{E}[Y]
\end{flalign*}
\end{document}

在此处输入图片描述

更合适的方法是使用专门用于编写此程序的软件包之一。当然,使用它们很难实现您对其中方程式进行格式化的特定愿望。

答案3

如果您想要纯 LaTeX 解决方案,请忽略此答案


[危险弯道标志]由于本网站主要关注 LaTeX 和 AMS 数学环境,我不太愿意提供纯 TeX 解决方案,但在这种情况下,您可以通过相当简单的对齐获得所需的灵活性。这一切都在 TeXBook 第 22 章中进行了解释。

在此处输入图片描述

\noindent
Plain \TeX\ version:
\def\E[#1]{\mathop{\rm E}[\vcenter{\hbox{$#1$}}]}
\def\P{\mathop{\rm P}}
%
\par\bigskip\noindent
\vbox{\openup6pt\halign{\bf #\hfil\quad &\hfil$\displaystyle #$&&$\displaystyle {}#$\hfil\cr
Theorem 4: & \E[XY] &=\E[X]\E[Y]\cr
\noalign{\medskip}
Proof:     & \E[XY] &=\sum_j \sum_i (x_i y_j)\P(x_i)\P(y_j) &=\sum_j\sum_i x_i\P(x_i) \, y_j\P(y_j) \cr
           &        &=\sum_j y_j\P(y_j)\,\sum_i x_i\P(x_i)  &=\E[X]\E[Y]\cr
}}
\bigskip

\noindent
In particular, any associated supporting element necessitates that urgent
consideration be applied to possible bidirectional logical relationship approaches.
Conversely, any associated supporting element recognizes other systems' importance
and the necessity for possible bidirectional logical relationship approaches.

\medskip\noindent
Or you might prefer a slightly tighter version without {\tt displaystyle}.
\par\bigskip\noindent
\vbox{\openup6pt\halign{\bf #\hfil\quad &\hfil$#$&&${}#$\hfil\cr
Theorem 4: & \E[XY] &=\E[X]\E[Y]\cr
Proof:     & \E[XY] &=\sum_j \sum_i (x_i y_j)\P(x_i)\P(y_j) &=\sum_j\sum_i x_i\P(x_i) \, y_j\P(y_j) \cr
           &        &=\sum_j y_j\P(y_j)\,\sum_i x_i\P(x_i)  &=\E[X]\E[Y]\cr
}}
\bigskip

\bye

由于向后兼容性,您当然可以在大多数 LaTeX 文档中使用该解决方案的部分内容,但您需要知道自己在做什么。

您可能注意到,在 Computer Modern 中,方括号和大写字母未垂直居中。我发现这在视觉上很不舒服,因此我添加了一个小宏来将它们居中。

\def\E[#1]{\mathop{\rm E}[\vcenter{\hbox{$#1$}}]}

这个宏充分利用了纯 TeX 的解析灵活性,但对于保留括号作为可选参数的 LaTeX 来说,它显然完全不符合标准。在这里,括号在宏定义中用作参数分隔符。宏的替换部分将它们放回到正确居中的参数周围。定义E为 amathop也使其与括号正确对齐。

相关内容