4

I want my document formatted with fleqn. I was previously having issues with a missing equation number using gather, so following Problem with cross referencing using gather environment I switched to using gathered instead. This worked fine in terms of reintroducing the equation number but now centre-aligns the equations rather than them being flush left... kind of. A very simple example (equation 1) seems to remain flush left, but in my actual equation (equation 3) they are centred relative to each other.

Can someone explain why the third equation does not align like the first? I think overall I would prefer to use gathered rather than gather - apart from anything else if I want the equation number to refer to a block of equations it seems a better solution.

\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}

enter image description here

1
  • Here gathered just does what it's supposed to do. Use aligned instead.
    – egreg
    Oct 14, 2015 at 10:34

1 Answer 1

4

In my opinion, gathered is doing what it's supposed to do; however, it's easy to modify it in such a way that it does left alignment when fleqn is specified.

In the code I removed all useless \ensuremath parts.

\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}

However, it's probably better using aligned as shown, in this particular case.

enter image description here

The same code, but removing fleqn, produces the following result, where you see that gathered again centers.

enter image description here

6
  • Great, thanks for showing me how to tweak gathered. I don't actually want my equations aligned at the = really, however, it has just occurred to me I can align putting & at the beginning of the equation, rather than at the = sign. This has worked perfectly, so I think I will indeed use aligned. I was particularly curious to know why my eq 1 was left-aligned though, even in the gathered environment - any ideas?
    – FionaSmith
    Oct 14, 2015 at 11:15
  • PS. I knew you would complain about the ensuremath - I actually tried to remove for the MWE but got some weird errors - must have missed something. The thing is, I use them a lot in the text, not just in the equations.
    – FionaSmith
    Oct 14, 2015 at 11:16
  • @FionaSmith I see no problem in typing $\Kmat$ in text instead of \Kmat{}. The dollars even avoid the problem of the following space. Math is math and should always be confined in explicit math mode.
    – egreg
    Oct 14, 2015 at 11:34
  • Yeah, we've had this conversation before... I also use xspace normally which I think you would disapprove of, but it means I just type \Kmat. I know, lazy... but it works for me.
    – FionaSmith
    Oct 14, 2015 at 11:44
  • 4
    @FionaSmith xspace and ensuremath, you probably just killed egreg, Oct 14, 2015 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .