# Environment definitions with arguments in \enddef

I was hoping to create a custom proof environment where along with the qed symbol it also mentions what was the claim that was proved. I thought the following would work:

\newenvironment{myproof}[1]%
{\vspace{1ex}\noindent{\em Pf.}\hspace{0.5em}}%
{\hfill{\tiny \qed (#1)}\vspace{1ex}}


I thought \begin{myproof}{Claim 42} ..\end{myproof} would work but it doesn't. After some googling I figured that it was because the argument is not passed on to \endmyproof.

Is there any fix to this? Or is this just bad TeX?

PS: FWIW, I was writing down a proof and had a small claim inside. The QED on the claim was a bit misleading that the proof of the Theorem was done. So this seemed like a natural(?) thing to do.

-
Actually, this seems to be a duplicate of Why can't the end code of an environment contain an argument?. –  Paŭlo Ebermann Aug 17 '11 at 15:11
Oh, I did not realise. I searched briefly for a similar question here and couldn't find this one. I should have searched a bit more before posting this perhaps. Sorry about the repost. –  Ramprasad Aug 17 '11 at 15:24
No problem, some (not too many) duplicates are good - they help others to find the right answer. I found it in the list of "Related" questions at the right. –  Paŭlo Ebermann Aug 17 '11 at 15:26

The \newenvironment{myproof} command behind the scenes actually defines two macros: \myproof and \endmyproof. These will be called by the \begin and \end commands to do the job. By the way of how \begin and \end work, only \begin can pass any arguments to the underlying macro, and thus \newenvironment only defines arguments for \myproof.

You can work around this by defining a temporary macro in \myproof and using its value in \endmyproof:

\newenvironment{myproof}[1]%
{\vspace{1ex}\noindent{\em Pf.}\hspace{0.5em}\def\myproofname{#1}}%
{\hfill{\tiny \qed (\myproofname)}\vspace{1ex}}


(If you put this environment definition in a package file, you could name this temporary macro \myproof@name, to avoid usage in the document and clashes.)

-
I think that needs to be a \gdef to make it work... –  Seamus Aug 17 '11 at 14:54
Why? \myproof and \endmyproof will be executed in the same group (created by \begin and \end), if you don't manually stop and restart the group between. –  Paŭlo Ebermann Aug 17 '11 at 14:56
That worked like a charm! Thanks a lot! –  Ramprasad Aug 17 '11 at 14:57
You're right. I was doing this earlier and I had the \def inside another environment and the use of the macro outside it... –  Seamus Aug 17 '11 at 15:01