从字符串中删除 \label

从字符串中删除 \label

有没有办法创建一个命令,\label从某些文本中删除(如果存在)。它应该像这样工作:

\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}

答案1

我想这可能对你有用:

\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}

在输出中,标签未定义并返回问号

编辑:

\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}

现在\removedlabel给出没有标签的字符串...但我不确定你想做什么...也许你应该更清楚你对命令的期望。

相关内容