将数学置于中心,同时将注释置于右侧

将数学置于中心,同时将注释置于右侧

我试图在环境中实现数学运算gather*,其中数学方程式位于其空间的中心,但右侧也有注释来解释步骤。我该怎么做?下面是一些代码,它们使数学方程式在其空间内右对齐。我怎样才能让它居中?我想在不对齐所有等号的情况下做到这一点,因为有些方程式的右侧很长,而另一些方程式的左侧很长。

\begin{align*}
y'+Py=Q & \quad\textrm{by \eqref{eq:1}}\\
e^{\int Pdx}(y'+Py)=e^{\int Pdx}Q & \quad\textrm{multiply both sides by }I=J=e^{\int Pdx}\\
(ye^{\int Pdx})'=e^{\int Pdx}Q & \quad\textrm{by \eqref{eq:3}}\\
ye^{\int Pdx}=\int e^{\int Pdx}Q dx + C\\
y=e^{-\int Pdx}\int e^{\int Pdx}Q dx + Ce^{-\int Pdx}\\
\end{align*}

编辑:

我发现您可以使用数组环境来执行此操作,但数学行看起来非常接近。 有办法解决这个问题吗?

\begin{displaymath}
\begin{array}{cl}
 y'+Py=Q & \quad\textrm{by \eqref{eq:1}}\\
 e^{\int Pdx}(y'+Py)=e^{\int Pdx}Q & \quad\textrm{multiply both sides by }I=J=e^{\int Pdx}\\
 (ye^{\int Pdx})'=e^{\int Pdx}Q & \quad\textrm{by \eqref{eq:3}}\\
 ye^{\int Pdx}=\int e^{\int Pdx}Q dx + C\\
 y=e^{-\int Pdx}\int e^{\int Pdx}Q dx + Ce^{-\int Pdx}\\
\end{array}
\end{displaymath}

答案1

\displaystyle在数学列中强制执行,并扩展内容以匹配您通常在align类似环境中获得的内容:

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath,array}
\newcommand{\dx}{\mathrm{d}x}
\newcommand{\eqcomment}[1]{\qquad\textrm{#1}}

\begin{document}

\[
  \renewcommand{\arraystretch}{1.5}
  \begin{array}{ >{\displaystyle}c l }
                    y' + Py = Q                                  & \eqcomment{by (1)} \\
           e^{\int P \dx}(y' + Py) = e^{\int P\dx} Q             & \eqcomment{multiply both sides by $I = J = e^{\int P \dx}$} \\
        (ye^{\int P \dx})' = e^{\int P \dx} Q                    & \eqcomment{by (3)} \\
        ye^{\int P \dx} = \int e^{\int P \dx}Q \dx + C           \\
   y = e^{-\int P \dx}\int e^{\int P \dx}Q \dx + Ce^{-\int P\dx}
  \end{array}
\]

\end{document}

答案2

这是一个测量评论的实现;如果没有重叠,代码将像 一样将方程式置于中心gather;否则它会将它们置于剩余空间的中心。

重叠的公差为 1em,但如果我们发现这是可行的,则可以将其设置为负值。

如您所见,您的代码会将其中一个方程式推到左边距之外(第一个例子),但通过负重叠我们可以使其适合。

可以设置的另一个参数是stretch,以允许更多的垂直间距(默认为 1.2,在第二个示例中设置为 1.8)。

\documentclass{article}
\usepackage{amsmath,xparse,environ,array}

\usepackage{showframe} % just to see the text block borders

\ExplSyntaxOn
\NewEnviron{gathercomment}[1][]
 {
  \keys_set:nn { gathercomment } { #1 }
  \begin{equation*}
  \gathercomment:V \BODY
  \end{equation*}
 }

\keys_define:nn { gathercomment }
 {
  overlap .dim_set:N = \l__gathercomment_overlap_dim,
  stretch .code:n = \renewcommand{\arraystretch}{#1},
  stretch .initial:n = 1.2,
 }

\seq_new:N \l__gathercomment_lines_seq
\seq_new:N \l__gathercomment_arow_seq
\dim_new:N \l__gathercomment_equations_dim
\dim_new:N \l__gathercomment_comments_dim
\box_new:N \l__gathercomment_equation_box
\box_new:N \l__gathercomment_comment_box

\cs_new_protected:Nn \gathercomment:n
 {
  \seq_set_split:Nnn \l__gathercomment_lines_seq { \\ } { #1 }
  \dim_zero:N \l__gathercomment_equations_dim
  \dim_zero:N \l__gathercomment_comments_dim
  \seq_map_function:NN \l__gathercomment_lines_seq \__gathercomment_measure:n
  % compare the widths
  \dim_compare:nTF
   {
    \l__gathercomment_equations_dim + \l__gathercomment_comments_dim + \l__gathercomment_overlap_dim
    >
    0.5\displaywidth
   }
   {% there would be overlap
    \begin{tabular}
     {
      @{}
      >{$\displaystyle}w{c}{\dim_eval:n {\displaywidth-\l__gathercomment_comments_dim - \l__gathercomment_overlap_dim}}<{$}
      @{\hspace{\l__gathercomment_overlap_dim}}
      w{r}{\l__gathercomment_comments_dim}
      @{}
     }
    \seq_use:Nn \l__gathercomment_lines_seq { \\ }
    \end{tabular}
   }
   {% no overlap
    \begin{tabular}
     {
      @{}
      >{$\displaystyle}w{c}{\displaywidth}<{$}
      @{}
      w{r}{0pt}
      @{}
     }
    \seq_use:Nn \l__gathercomment_lines_seq { \\ }
    \end{tabular}
   }
 }
\cs_generate_variant:Nn \gathercomment:n { V }

\cs_new_protected:Nn \__gathercomment_measure:n
 {
  \seq_set_split:Nnn \l__gathercomment_arow_seq { & } { #1 }
  % measure the half widths of the equations
  \hbox_set:Nn \l__gathercomment_equation_box
   { $\displaystyle \seq_item:Nn \l__gathercomment_arow_seq { 1 }$ }
  \dim_set:Nn \l__gathercomment_equations_dim
   {
    \dim_max:nn
     { \l__gathercomment_equations_dim }
     { \box_wd:N \l__gathercomment_equation_box / 2 }
   }
  % measure the widths of the comments
  \hbox_set:Nn \l__gathercomment_comment_box
   { \seq_item:Nn \l__gathercomment_arow_seq { 2 } }
  \dim_set:Nn \l__gathercomment_comments_dim
   {
    \dim_max:nn
     { \l__gathercomment_comments_dim }
     { \box_wd:N \l__gathercomment_comment_box }
   }
 }

\ExplSyntaxOff

\begin{document}

\begin{gathercomment}
y'+Py=Q & by \eqref{eq:1} \\
e^{\int P\,dx}(y'+Py)=e^{\int P\,dx}Q & multiply both sides by $I=J=e^{\int P\,dx}$ \\
(ye^{\int P\,dx})'=e^{\int P\,dx}Q & by \eqref{eq:3} \\
ye^{\int P\,dx}=\int e^{\int Pdx}Q dx + C \\
y=e^{-\int P\,dx}\int e^{\int P\,dx}Q\,dx + Ce^{-\int P\,dx}
\end{gathercomment}

\begin{gathercomment}[stretch=1.8,overlap=-2em]
y'+Py=Q & by \eqref{eq:1} \\
e^{\int P\,dx}(y'+Py)=e^{\int P\,dx}Q & multiply both sides by $I=J=e^{\int P\,dx}$ \\
(ye^{\int P\,dx})'=e^{\int P\,dx}Q & by \eqref{eq:3} \\
ye^{\int P\,dx}=\int e^{\int Pdx}Q dx + C \\
y=e^{-\int P\,dx}\int e^{\int P\,dx}Q\,dx + Ce^{-\int P\,dx}
\end{gathercomment}


\begin{gathercomment}
abc=def & xyz \\
x=y & xx \\
1=2 \\
xxx=xxxxxxx & x
\end{gathercomment}

\end{document}

在此处输入图片描述

相关内容