How do I make my commands context aware, or in particular, how do I remove commands when I refer to them within \nameref or TOC? See an example below, where a counter is incremented when I refer to a labeleled paragraph (and then again in TOC - not shown)

% Alas, the following \Get* functions don't work on protected commands
%\GetTitleStringExpand{\Thm} % don't know how to use this (?), so try to expand everything:
\newcommand*{\Thm}[1]{\refstepcounter{thm}#1 \thethm~}

    ... as in \nameref{thm:label}.  

    %following line doesn't compile without \protect
        \label{thm:label} Prove that ...

Output to .pdf

... as in Theorem 1 Title.

Theorem 2 Title Prove that ...

.aux file content:

\relax \newlabel{thm:label}{{}{1}{\protect \Thm {Theorem}Title}{}{}} @writefile{toc}{\contentsline {paragraph}{\Thm {Theorem}Title}{1}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.