我試圖在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}