How can I make a command execute exactly once in an argument?

I'm wanting to create a command similar to AMS's proof environment that automatically calls a command (like AMS's \qedhere) when it gets to the end, but allows you to call the command earlier if you need to. In my case, I'm wanting the command to execute some code and place a label. When the command ends with text, the label is in the right spot. But when the command ends with a displayed equation, the label is too far down. In those cases, I'd like to manually move the label to its correct location. I tried to do this with a boolean to tell if the end had been reached, but it doesn't seem to toggle the boolean until after the command ends:

\documentclass{article}
\newcommand{\currentLabel}{\nosuchcommand}

\newcounter{exampleends}
\newif\ifendencountered

\newcommand{\exampleendhere}{\ifendencountered\else%
\stepcounter{exampleends}%
\label{e\currentLabel}%
\textbf{End of \currentLabel.}%
\endencounteredtrue%
\renewcommand{\currentLabel}{\nosuchcommand}%
\fi%
}

\newcommand{\myexample}[2]{%
\renewcommand{\currentLabel}{#1}%
\endencounteredfalse%
\textbf{Start of \currentLabel.}\label{#1}\\
#2\\
\exampleendhere}

\begin{document}

\myexample{exampleOne}{This is my example.}

\bigskip

\myexample{exampleTwo}{This is my next example.
$1+1=2\exampleendhere$}

\bigskip

example end called: \arabic{exampleends} time(s).

\end{document}


Results in:

I've looked at the source for amsthm.sty, but I can't understand how it does what it does, and how I could modify that to suit my needs (I've also tried the above with the ifthen and etoolbox packages, with the same results). Why is the boolean not changing to true? How can I remove the last End of exampleTwo'', and make \exampleends incremented only twice?

• maybe it will help, and maybe it won't, but you might consider looking at the documented code for amsthm which is part of amsclass.dtx. the typeset output can be viewed with texdoc amsclass.pdf if you're on a "full" tex live installation. – barbara beeton Jul 22 '16 at 2:01
• @barbarabeeton Thanks for the suggestion, but it looks like the documentation doesn't explain thoroughly enough for me to understand. – Teepeemm Jul 22 '16 at 17:24

Place \global prior to the \endencounteredtrue. As it is in your MWE, since it is issued inside the $...$ environment, the value of \ifendencountered is lost upon exit, unless it is saved globally.

You might also consider changing the #2\\ to #2\par\noindent, which I did not do here.

\documentclass{article}
\newcommand{\currentLabel}{\nosuchcommand}

\newcounter{exampleends}
\newif\ifendencountered

\newcommand{\exampleendhere}{\ifendencountered\else%
\stepcounter{exampleends}%
\label{e\currentLabel}%
\textbf{End of \currentLabel.}%
\global\endencounteredtrue%
\renewcommand{\currentLabel}{\nosuchcommand}%
\fi%
}

\newcommand{\myexample}[2]{%
\renewcommand{\currentLabel}{#1}%
\endencounteredfalse%
\textbf{Start of \currentLabel.}\label{#1}\\
#2\\
\exampleendhere}

\begin{document}

\myexample{exampleOne}{This is my example.}

\bigskip

\myexample{exampleTwo}{This is my next example.
$1+1=2\exampleendhere$}

\bigskip

example end called: \arabic{exampleends} time(s).

\end{document}


• That did the trick; thanks. I've run into a related problem: it looks like \exampleendhere doesn't work with the AMS display environments (gather, align, flalign): neither of the "End of"s appear. Do you know a reason that would fit into a comment, or should I make that a new question? – Teepeemm Jul 22 '16 at 1:06
• @Teepeemm I am definitely not an expert on the amsmath environments. Perhaps you might ask a follow up question, citing this original question along the way. – Steven B. Segletes Jul 22 '16 at 9:44