I am making use of \qedhere in proof environments in order to avoid extra space when the proof ends in an equation. I am wondering about why it behaves differently in different environments.
- Apparently, \qedhere does not work in an
equationenvironment, when using the showonlyrefs feature from themathtoolspackage? I get the following error:A <box> was supposed to be here. \end{equation} - When using the
amsmathpackage, it does not give an error between a pair of$$, but it does not flush the\qedbox to the right, so it does not behave as desired. The same goes for thegatherenvironment. - It works in an
alignenvironment, but only if&is ever used, see Strange behavior by \qedhere in align environment without alignment mark (&) - It works as desired in square brackets
\[\]
Here is a MWE:
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{mathtools}
\mathtoolsset{showonlyrefs}
\begin{document}
\begin{proof}
Too much space afterwards:
\[
a=b.
\]
\end{proof}
\begin{proof}
Works fine:
\[
a=b.\qedhere
\]
\end{proof}
\begin{proof}
Not flushed right:
$$
a=b.\qedhere
$$
\end{proof}
\begin{proof}
Not flushed right:
\begin{gather}
a=b.\qedhere
\end{gather}
\end{proof}
\begin{proof}
Box completely misplaced:
\begin{align}
a=b.\qedhere
\end{align}
\end{proof}
\begin{proof}
Stuff in comments gives error.
% \begin{equation}
% a=b.\qedhere
% \end{equation}
\end{proof}
\end{document}
Why do I get these different behaviors, and what is the preferred way of using \qedhere?


\qedhere, if you tell the compiler explicitely that they are supposed to be unnumbered, right? I overlooked this, because the last equations of my proofs have never had numbers so far. The difference is only about the way these environments handle this conflict between equation number and qed box, which happens in different ways foralign,gatherandequation. – hexcoffee Aug 18 '22 at 09:23showonlyrefsfeature, so I learned now that the compiler can not figure out itself that there is no conflict between equation number and\qedherewhen usingshowonlyrefs– hexcoffee Aug 18 '22 at 09:32