fleqn 和 gather 之间有冲突吗?

fleqn 和 gather 之间有冲突吗?

我希望我的文档使用 进行格式化fleqn。我之前在使用 时遇到缺少方程编号的问题gather,因此按照使用收集环境进行交叉引用的问题我改用了gathered。这在重新引入方程编号方面效果很好,但现在方程式居中对齐,而不是左对齐……有点。一个非常简单的例子(方程 1)似乎仍然左对齐,但在我的实际方程(方程 3)中,它们彼此居中。

有人能解释一下为什么第三个方程式与第一个方程式不一致吗?我认为总的来说我更愿意使用gathered而不是gather——除了其他之外,如果我想让方程式编号引用方程块,这似乎是一个更好的解决方案。

\documentclass[fleqn]{report}

\usepackage{amsmath}


%Referencing objects/sections
%-----------------------------------
\usepackage[noabbrev]{cleveref}      % reference object types automatically
\crefformat{equation}{equation~\textup{#2#1#3}}


%Definitions of useful shortcuts (e.g. to add matrix formatting)
%---------------------------------------------------------------
\newcommand{\trans}{\ensuremath{^{\textup{T}}}}
\newcommand{\inv}{\ensuremath{^{-1}}}

\newcommand{\Kmat}{\ensuremath{\mathbf{K}}}
\newcommand{\yvec}{\ensuremath{\mathbf{y}}}
\newcommand{\xvec}{\ensuremath{\mathbf{x}}}
\newcommand{\xa}{\ensuremath{\mathbf{x}_{a}}}
\newcommand{\xhat}{\ensuremath{\hat{\mathbf{x}}}}
\newcommand{\Seps}{\ensuremath{\mathbf{S}_{\epsilon}}}
\newcommand{\Sa}{\ensuremath{\mathbf{S}_{a}}}
\newcommand{\Shat}{\ensuremath{\hat{\mathbf{S}}}}


%HERE BEGINS THE DOCUMENT
%------------------------------------
\begin{document}         



\section{My section}

A test equation: this seems to work fine, being both gathered and flushed left and having the equation number I want too.
\begin{equation}
\begin{gathered}
a=b \\
b=c
\end{gathered}
\label{eq:test}
\end{equation}
This one has an issue with the equation numbering -- see the last sentence where I reference it (partially solved if I move the \textbackslash notag to the top line rather than the bottom line):
\begin{gather}
        \xhat = \xa + ( \Kmat\trans \Seps\inv \Kmat + \Sa\inv )\inv \Kmat\trans \Seps\inv ( \yvec - \Kmat\xvec ) \\
        \Shat = ( \Kmat\trans\Seps\inv \Kmat + \Sa\inv)\inv \notag
\label{eq:retrieval}
\end{gather}
This one numbers OK but centre aligns the equations relative to each other:
\begin{equation}
    \begin{gathered}
        \xhat = \xa + ( \Kmat\trans \Seps\inv \Kmat + \Sa\inv )\inv \Kmat\trans \Seps\inv ( \yvec - \Kmat\xvec ) \\
        \Shat = ( \Kmat\trans\Seps\inv \Kmat + \Sa\inv)\inv
    \end{gathered}
\label{eq:retrieval2}
\end{equation}
I want to be able to refer to \cref{eq:retrieval} and \cref{eq:retrieval2} and \cref{eq:test}.

\end{document}

在此处输入图片描述

答案1

在我看来,gathered它正在做它应该做的事情;然而,很容易以某种方式修改它,使它在fleqn指定时进行左对齐。

在代码中我删除了所有无用的\ensuremath部分。

\documentclass[fleqn]{report}

\usepackage{amsmath}

\makeatletter
% amsmath.sty, line 1253:
\renewenvironment{gathered}[1][c]{%
    \RIfM@\else
        \nonmatherr@{\begin{gathered}}%
    \fi
    \null\,%
    \if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi \bgroup
        \Let@ \chardef\dspbrk@context\@ne \restore@math@cr
        \spread@equation
        \ialign\bgroup
            %%% In the original there is just \hfil
            \if@fleqn\else\hfil\fi\strut@$\m@th\displaystyle##$\hfil
            \crcr
}{%
  \endaligned
}
\makeatother


%Referencing objects/sections
%-----------------------------------
\usepackage[noabbrev]{cleveref}      % reference object types automatically
\crefformat{equation}{equation~\textup{#2#1#3}}


%Definitions of useful shortcuts (e.g. to add matrix formatting)
%---------------------------------------------------------------
\newcommand{\trans}{^{\textup{T}}}
\newcommand{\inv}{^{-1}}

\newcommand{\Kmat}{\mathbf{K}}
\newcommand{\yvec}{\mathbf{y}}
\newcommand{\xvec}{\mathbf{x}}
\newcommand{\xa}{\mathbf{x}_{a}}
\newcommand{\xhat}{\hat{\mathbf{x}}}
\newcommand{\Seps}{\mathbf{S}_{\epsilon}}
\newcommand{\Sa}{\mathbf{S}_{a}}
\newcommand{\Shat}{\hat{\mathbf{S}}}


%HERE BEGINS THE DOCUMENT
%------------------------------------
\begin{document}         



\section{My section}

A test equation: this seems to work fine, being both gathered and flushed left and having the equation number I want too.
\begin{equation}
\begin{gathered}
a=b \\
b=c
\end{gathered}
\label{eq:test}
\end{equation}
This one has an issue with the equation numbering -- see the last sentence where I reference it (partially solved if I move the \textbackslash notag to the top line rather than the bottom line):
\begin{gather}
        \xhat = \xa + ( \Kmat\trans \Seps\inv \Kmat + \Sa\inv )\inv \Kmat\trans \Seps\inv ( \yvec - \Kmat\xvec ) \\
        \Shat = ( \Kmat\trans\Seps\inv \Kmat + \Sa\inv)\inv \notag
\label{eq:retrieval}
\end{gather}
This one numbers OK but centre aligns the equations relative to each other:
\begin{equation}
    \begin{gathered}
        \xhat = \xa + ( \Kmat\trans \Seps\inv \Kmat + \Sa\inv )\inv \Kmat\trans \Seps\inv ( \yvec - \Kmat\xvec ) \\
        \Shat = ( \Kmat\trans\Seps\inv \Kmat + \Sa\inv)\inv
    \end{gathered}
\label{eq:retrieval2}
\end{equation}
I want to be able to refer to \cref{eq:retrieval} and \cref{eq:retrieval2} and \cref{eq:test}.
\begin{equation}
    \begin{aligned}
        \xhat &= \xa + ( \Kmat\trans \Seps\inv \Kmat + \Sa\inv )\inv \Kmat\trans \Seps\inv ( \yvec - \Kmat\xvec ) \\
        \Shat &= ( \Kmat\trans\Seps\inv \Kmat + \Sa\inv)\inv
    \end{aligned}
\label{eq:retrieval3}
\end{equation}
And here we show~\cref{eq:retrieval3}

\end{document}

aligned然而,在这种特殊情况下,使用如图所示的方法可能更好。

在此处输入图片描述

相同的代码,但删除了fleqn,会产生以下结果,您会gathered再次看到中心。

在此处输入图片描述

相关内容