# Removing \label from a string

Is there a way to create a command that removes, if present, a \label from some text. It should work like this:

\documentclass{article}
\newcommand{\removelabel}[1]{} % some code
\begin{document}
\removelabel{\label{some_label} This label should be removed} % output: 'This label should be removed'
\removelabel{This string is already label-less} % output: 'This string is already label-less
\end{document}

• see this for the full story. I'm trying to fix the bug shown in the code posted after 'EDIT: MWE showing the problem'. I'd call such a function on line 52, with \BODY as its argument. – noibe Feb 17 at 2:45
• is a lualatex solution acceptable? – JPi Feb 17 at 3:27
• @JPi not really – noibe Feb 17 at 11:00
• Then I can't tell you. 😣 – JPi Feb 17 at 12:24
• Shall all instances of the control word token \label plus the following undelimited argument be removed from the "string" or shall only the very first instance thereof be removed? What about instances thereof that within the "string" in question are nested in curly braces? What about control-word-tokens which are not \label but have the same meaning - e.g., what about \foobar after \let\foobar=\label? Must the removal-routine also work in full-expansion-contexts? – Ulrich Diez Feb 18 at 8:47

This is something I suppose could work for you:

\documentclass{article}
\usepackage{caption}
\usepackage{hyperref}
\def\myEmptyLabel#1{}
\newcommand{\removelabel}[1]{\let\oldlabel\label\let\label\myEmptyLabel#1\let\label\oldlabel} % some code
\begin{document}
\begin{minipage}{\textwidth}
\removelabel{\captionof{figure}{\label{some_label} This label should be removed}} % output: 'This label should be removed'
\end{minipage}
\removelabel{This string is already label-less} % output: 'This string is already label-less

Figure~\ref{some_label}
\end{document}


In the output the label is not defined and returns questionmarks

Edit:

\documentclass{article}
\usepackage{caption}
\usepackage{hyperref}
\def\myEmptyLabel#1{}
\newcommand{\removelabel}[1]{\let\oldlabel\label\let\label\myEmptyLabel\xdef\removedlabel{#1}\let\label\oldlabel} % some code
\begin{document}

\removelabel{\label{some_label} This label should be removed}
\removedlabel

\removelabel{This string is already label-less} % output: 'This string is already label-less

\removedlabel

%Figure~\ref{some_label}
\end{document}


Now \removedlabel gives the string without the label... But I am not sure what you want to do... May be you should be more clear on what you expect from the command.

• This code isn't actually removing the label, it just briefly changes the behaviour of the \label command to null. If I were to print the argument of \removelabel as if it was a string, I'd still see \label{some_label}. – noibe Feb 17 at 22:53
• if you expand the argument of \removelabel... it will not be there... If you want an argument of a command to be something and without even expanding it to disappear ane part of it... I am afraid it can't be done... – koleygr Feb 18 at 0:37
• I suggest \def\myEmptyLabel#1{\@bsphack\@esphack} . – Ulrich Diez Feb 18 at 21:55
• @UlrichDiez.... you may write an answer because I don't really know what is these commands... – koleygr Feb 19 at 10:56
• @koleygr \label shall not deliver visible things into the output-file/pdf-file. \@bsphack/\@esphack ensures that a space-token behind the argument of a \label-command does not produce undesired visible whitespace in case there already was a space before the \label-command... Many of these commands are explained in source2e.pdf – Ulrich Diez Feb 19 at 13:44