How to add theorem number by the \qed mark at the end of proof environment

I use amsthm. All theorem-like environments use a common counter. As I have some very long proofs I want to add the theorem number to which the proof refers at the end of proof just next to the box/square/qed mark.

Renewing command \qedsymbol like this:

\renewcommand\qedsymbol{\ensuremath{\openbox}~\arabic{theorem}}

works almost fine. The only problem is that if there is some fact or lemma inside my proof,

\begin{theorem}
\end{theorem}
\begin{proof}
...
\begin{fact}
...
\end{fact}
...
\end{proof}


then the number by \qed is this fact's number instead of the theorem number.

I was disappointed because looking at the proof definition in amsthm.sty file

\newenvironment{proof}[1][\proofname]{\par
\pushQED{\qed}%
\normalfont \topsep6\p@\@plus6\p@\relax
\trivlist
\item[\hskip\labelsep
\itshape
}{%
\popQED\endtrivlist\@endpefalse
}
\providecommand{\proofname}{Proof}


I can see that the qed is handled somehow and put on some stack when the environment is being initialized, then it is just popped. So it could remember the original theorem number.

I suspect that my problem is caused by fact that evaluation of my \qedsymbol is postponed until the proof environment is getting closed.

Is there a way to force it to evaluate it somewhere around \pushQED{\qed}%?

Or maybe someone could explain what is actually going on in these lines so that I could fiddle with it myself?:

\newcommand{\pushQED}[1]{%
\toks@{\qed@elt{#1}}\@temptokena\expandafter{\QED@stack}%
\xdef\QED@stack{\the\toks@\the\@temptokena}%
}
\newcommand{\popQED}{%
\begingroup\let\qed@elt\popQED@elt \QED@stack\relax\relax\endgroup
}


\renewcommand{\qedsymbol}{\edef\qednow{\ensuremath{\openbox}~\arabic{theorem}}\qednow}


but it didn't work as well.

-

migrated from stackoverflow.comNov 29 '11 at 14:33

This question came from our site for professional and enthusiast programmers.

It seems that you have something like

\newtheorem{theorem}{Theorem}
\newtheorem{fact}[theorem]{Fact}


Then, after \begin{fact}...\end{fact} the theorem counter will be stepped and \arabic{theorem} will become the number assigned to fact.

You can use \pushQED, but redefining it:

\let\ORIpushQED\pushQED
\def\pushQED#1{\begingroup\edef\x{\endgroup
\noexpand\ORIpushQED{\unexpanded{#1}\noexpand~\arabic{theorem}}}\x}


The purpose of \begingroup and \endgroup is just not leaving a defined \x around.

-
Thank you for the answer. It works however I'd love to achieve this only touching \qedsymbol. Do you think this is impossible? I tried something similar to your solution \let\ORIqedsymbol\qedsymbol \def\qedsymbol{\edef\x{\ORIqedsymbol~\arabic{theorem}}\x} but it doesnt work. Could you point out where is the magic in your code that make it work? I see you use some commands to postpone some expansion. My intuition would be to use something like \expand instead of \noexpand. What am I missing? –  misiek Nov 29 '11 at 20:37
Btw. your code works even if I replace \unexpanded{#1}\noexpand simply by #1. Are these two commands redundant? –  misiek Nov 29 '11 at 20:46
No, you can't act only on \qedsymbol, because its expansion happens only when the symbol has to be printed and it's too late for accessing at the correct theorem number. Your intuition about \expand is wrong. –  egreg Nov 29 '11 at 22:12

why not take advantage of the \label facility? for example:

\documentclass{article}
\usepackage{amsthm}

\renewcommand\qedsymbol{\ensuremath{\openbox}~\ref{\theqedlabel}}
\def\qedlabel#1{\def\theqedlabel{#1}}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}\label{mythm}
This is a theorem.
\end{theorem}

\begin{proof}
\qedlabel{mythm}
This is the proof.
\end{proof}

\end{document}


this method doesn't require a single counter for everything. that means, for example, that proofs can be in an order different from the order of theorems. or a variation on the above code could be constructed allowing the post-tombstone insertion to include a fuller identification of what's being proved, say "Theorem m" or "Lemma n". (construction is left as an exercise.)

-
Thank you. This is not exactly what I'm looking for but quite interesting workaround. –  misiek Nov 29 '11 at 20:49