对齐方程并附上注释/解释

对齐方程并附上注释/解释

在许多情况下,我想编写一些跨越多行的对齐方程式,并为其中某些行添加注释(我认为如果注释足够短,那么比在方程式末尾解释所有内容更清楚)。理想情况下,所有注释都应从同一条垂直线开始。以下示例显示了这一点:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{align*}
    a + b + c + d + e &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{document}

问题在于当某些等式的某些行太长时:

enter image description here

第二条注释位于右边距内,这是不允许的。

我知道会发生这种情况是因为align环境创建了一个表并且每个&符号都添加了一列,因此第二条注释不能与第一条注释(由其行上的等式推动)位于不同的列上。

但是,我仍然希望第二条注释以右边距结束。有没有简单的方法可以实现这一点?如果可能的话,我宁愿不要定义太复杂的命令,即使这意味着我无法完全得到我想要的东西,但其他东西可以解决问题。

答案1

您不能将所有注释都垂直对齐在其左边距,因为宽方程式和宽注释根本不适合这条线。

您可以通过将第一个术语单独设置在一行来节省空间,同时仍然具有显示的外观,避免转到左边距:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is.
Some more and more text yay text is good.
This is just some text to show where the margin is.
Some more and more text yay text is good.
\begin{align*}
\lefteqn{a + b + c + d + e}\quad \\
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \text{a little longer comment} \\
&= 0
\end{align*}

\end{document}

\lefteqn隐藏术语的宽度后,我在左边距添加了= \quad in order not to have the` 符号,结果发现仍然有一点可用空间。

enter image description here

您可以探索另一种可能性,但这取决于实际的方程式和注释,即将长注释分成多行。

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is. 
Some more and more text yay text is good.
This is just some text to show where the margin is. 
Some more and more text yay text is good.
\begin{align*}
a + b + c + d + e
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \begin{tabular}[t]{@{}l@{}}
                         a little \\
                         longer \\
                         comment
                         \end{tabular} \\
&= 0
\end{align*}

\end{document}

enter image description here

答案2

好吧,下面是一个非常简单的技巧,可以“解决问题”:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

% \slshape % if uncommented, will the reference to the Magic Theorem be slanted?

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                && \text{by \ldots} && \\
        &= x-x
                && && \makebox[0pt][r]{by the Magic Theorem} \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

这是我得到的输出(在 TeXLive 2016 上,抱歉,但这就是我目前得到的):

Output of the code

添加: 一个更简单的解决方案\hidewidth是使用不是产生与前一个完全相同的打印输出:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                & \text{by \ldots} & \\
        &= x-x
                & \hidewidth\text{by the Magic Theorem} & \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

确实,在我看来,输出结果实际上更好,两个都右边距对齐的解释:

Output of the second code sample

第二次补充: 如果你反对使用类似的普通 TeX 命令\hidewidth,你可以用以下代码替换第二个示例

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

% \slshape

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                & \text{by \ldots} & \\
        &= x-x
                & \makebox[0pt][r]{by the Magic Theorem} & \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

输出没有变化。

答案3

谷歌\lefteqn搜索egreg 的回答我发现这个答案这是关于\MoveEqLeft- 来自 mathtools 的命令(参见 3.4.4这里)因此示例变为:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{align*}
    \MoveEqLeft[1] a + b + c + d + e \\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{document}

产生以下输出(与 egreg 的相同):

enter image description here

\lefteqn我认为这个解决方案比使用专门为解决这个问题而提出的解决方案要好一些\MoveEqLeft(根据文档)。

答案4

这里有两种可能性,使用flalign*环境和\mathrlap来自mathtools,或者使用简单align*fleqn来自的环境nccmath

\documentclass{article}
\usepackage{mathtools, nccmath}
\usepackage{showframe}
 \renewcommand{\ShowFrameLinethickness}{0.3pt}

\begin{document}

    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{flalign*}
   \mathrlap{ a + b + c + d + e} \\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{flalign*}
Some other text and another possibility:
\begin{fleqn}
    \begin{align*}
    a + b &+ c + d + e\\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{fleqn}

\end{document} 

enter image description here

相关内容