How can a tombstone be put flush right in an environment with tags?

I know that, if a theorem's proof ends with a displayed maths environment of a type such as align* or align, then, in order to get the QED tombstone placed on that environment's last line (rather than on a following line on its own), \qedhere must be put in that environment.

However, if that environment contains at least one tag, the tombstone is placed too far to the left. How can I get the tombstone placed flush right? (Adding a & or \hfill is to no avail.) MWE:

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsthm} % for \proof & \qedhere
\newtheorem{thm}{Theorem}
\begin{document}
\begin{thm} Theorem-statement.
\begin{proof}
\begin{align*}
a&=b\tag{by some earlier result}\\
&=c\\
&=d\qedhere
\end{align*}
\end{proof}
\end{thm}
\end{document}

• This is, in my opinion, abusing \tag, which is for setting a key for later reference, not for descriptions. – egreg May 29 '17 at 8:10
• Anyway, I seem to recall that \qedhere does not behave in tagged/numbered alignments. For instance, if you use align instead of align, the number of the third line disappears. – egreg May 29 '17 at 8:41
• @egreg So what do you recommend for typesetting text "descriptions" at ends of lines in display mode? And should they be set flush right or close to the equation? – Rosie F May 29 '17 at 8:45
• I'd make them part of the alignment: closer to it, not flush right. With alignat* if align* uses too much space. – egreg May 29 '17 at 8:48

The \qedhere command issued in a numbered or tagged environment doesn't really work (I think it's related to other misfeatures of \qedhere like in Misplaced QED symbol with \qedhere inside align* environment).

Here's a hack:

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}
\usepackage{amsthm} % for \proof & \qedhere

\usepackage{showframe} % just for showing the bounding box

\newtheorem{thm}{Theorem}

\makeatletter
\newcommand{\altqedhere}{%
\ifmeasuring@\else\sbox0{\popQED}\fi
\tag*{\qedsymbol}%
}
\makeatother

\begin{document}

\begin{thm}
Theorem-statement.
\end{thm}

\begin{proof}
\begin{align*}
a&=b\tag{by some earlier result}\\
&=c\\
&=d\qedhere
\end{align*}
\end{proof}

\begin{proof}
\begin{align*}
a&=b\tag{by some earlier result}\\
&=c\\
&=d\altqedhere
\end{align*}
\end{proof}

\end{document}


Note that proof should be outside the thm environment.

With \popQED inside an unused box, we make amsthm into thinking the tombstone has already been used.