# How to redefine label within a command?

I want to replace every occurrence of \label with a given command \mylabel but only within a given command \mycommand.

\documentclass{article}

\newcommand\mylabel[1]{Test}
\newcommand\mycommand[1]{#1}

\begin{document}
\mycommand{This is a \label{tag1}.}\label{tag2}
\end{document}


So in this example the output should be This is a Test..

• The second label tag2 is not really useful, in my point of view. And don't use minimal class – user31729 May 31 '16 at 12:38
• Thanks. The second tag was just to make clear that Test should not be output outside of mycommand. – Daniel May 31 '16 at 12:45

It's possible to restrict some commands within a group, so a redefinition does not 'survive' the end of the group, fixing \let\label\mylabel here.

Be careful with specific redefinitions of the \label command that have an optional argument, e.g. the \label-version provided by cleveref, for example.

Outside of \mycommand, \label is acting as expected.

\documentclass{article}

\newcommand\mylabel[1]{Test}
\newcommand\mycommand[1]{%
\begingroup
\let\label\mylabel
#1%
\endgroup
}

\begin{document}
\mycommand{This is a \label{tag1}.}\label{tag2}
\end{document}