2

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}
noibe
  • 2,084
  • 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 '19 at 02:45
  • is a lualatex solution acceptable? – JPi Feb 17 '19 at 03:27
  • @JPi not really – noibe Feb 17 '19 at 11:00
  • Then I can't tell you. – JPi Feb 17 '19 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 '19 at 08:47
  • @UlrichDiez the command is intended to be used with arguments whose only appearance (if any) of \label{...} is at the beginning of the argument itself. If you're curious, fixing this problem is that intended use. – noibe Feb 18 '19 at 14:58
  • Are you aware that theorem-environments usually also do \refstepcounter which places an anchor/a destination for hyperlinking when doing \ref? Thus when using hyperref, you may get errors about destinations with same identifier... You may need a command which in the correct way disables every dangerous command and which--when re-stating a theorem--in a local scope sets correct counter values... E.g, setting \label to \@gobble isn't the best approach. Should be \def\label#1{\@bsphack\@esphack}... – Ulrich Diez Feb 18 '19 at 15:11
  • @UlrichDiez No, I wasn't aware of that. Where exactly should I place \def\label#1{\@bsphack\@esphack} in the code? – noibe Feb 18 '19 at 15:23
  • The \refstepcounter-issue in case of loading hyperref and the \def\label#1{\@bsphack\@esphack}-issue are two different problems. In case you wish \label to be a no-op in whatsoever expansion-context, make sure that that expansion-context is carried out within a local scope/group where \label is redefined as \def\label#1{\@bsphack\@esphack}. (You can use \renewcommand instead of \def as well.) In case of using hyperref or the like, you may need to turn a lot of other things, which may cause "destination with same identifier"-errors, into argument-gobbling no-ops, too. – Ulrich Diez Feb 18 '19 at 21:52

1 Answers1

1

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.

koleygr
  • 20,105
  • 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 '19 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 '19 at 00:37
  • 1
    I suggest \def\myEmptyLabel#1{\@bsphack\@esphack} . – Ulrich Diez Feb 18 '19 at 21:55
  • 1
    @UlrichDiez.... you may write an answer because I don't really know what is these commands... – koleygr Feb 19 '19 at 10:56
  • 1
    @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 '19 at 13:44
  • @UlrichDiez... Thanks for the information... I am tending to read the documentation of source2e these days... but didn't found the time yet. Thanks! (You may add an answer since it will be possibly more useful than mine and I have not the knowledge to add it and give the explanation s needed) – koleygr Feb 19 '19 at 14:00