0

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)

\documentclass{article}
\usepackage{nameref}
% 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:
\GetTitleStringSetup{expand}
\GetTitleStringDisableCommands{% 
    %\renewcommand{\Thm}[1]{}%
    \let\Thm\@gobble%
}
\newcounter{thm}
\newcommand*{\Thm}[1]{\refstepcounter{thm}#1 \thethm~}

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

    %following line doesn't compile without \protect
    \paragraph{\protect\Thm{Theorem}Title}
        \label{thm:label} Prove that ...
\end{document}

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.