![从交叉引用标签中的描述标签中删除最后一个字符](https://linux22.com/image/332858/%E4%BB%8E%E4%BA%A4%E5%8F%89%E5%BC%95%E7%94%A8%E6%A0%87%E7%AD%BE%E4%B8%AD%E7%9A%84%E6%8F%8F%E8%BF%B0%E6%A0%87%E7%AD%BE%E4%B8%AD%E5%88%A0%E9%99%A4%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6.png)
我读过如何制作带有描述标签的标签作为标签的参考(LaTeX 中描述列表项的参考名称):
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
\let\orglabel\label
\let\label\@gobble
\phantomsection
\edef\@currentlabel{#1}%
%\edef\@currentlabelname{#1}%
\let\label\orglabel
\orgdescriptionlabel{#1}%
}
\makeatother
这可行。但是,描述标签通常以特殊字符结尾,如:
或,
,这不应成为参考标签的一部分。我如何从参考标签中删除此字符?
编辑:
尝试过:
\usepackage{xstring}
...
\edef\@currentlabel{\StrGobbleRight{#1}{1}}%
...
但由于某种原因,这似乎不起作用。
编辑
以下是完整文档(来自上面的链接):
\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
\let\orglabel\label
\let\label\@gobble
\phantomsection
\edef\@currentlabel{#1}%
%\edef\@currentlabelname{#1}%
\let\label\orglabel
\orgdescriptionlabel{#1}%
}
\makeatother
\begin{document}
\section{Definitions}
\begin{description}
\item [Vehicle\label{itm:vehicle}] Something
\item [Bus\label{itm:bus}] A type of \ref{itm:vehicle}
\item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description}
The item `\ref{itm:bus}' is listed on page~\pageref{itm:bus} in section~\nameref{itm:bus}.
\end{document}
答案1
您可以根据以下条件选择删除最后一个标点符号\spacefactor
:
\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}
\usepackage{nameref}
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
\begingroup
\let\label\@gobble
\phantomsection
\protected@edef\daniel@currentlabel{#1}%
\sbox\z@{\daniel@currentlabel\xdef\daniel@sf{\the\spacefactor}}%
\ifnum\daniel@sf>1000
\noexpandarg
\expandafter\StrGobbleRight\expandafter{\daniel@currentlabel}{1}[\daniel@currentlabel]%
\fi
\def\x{%
\expandafter\endgroup
\expandafter\def\expandafter\@currentlabelname
\expandafter{\daniel@currentlabel}}%
\x
\orgdescriptionlabel{#1}%
}
\makeatother
\begin{document}
\section{Definitions}
\begin{description}
\item [Vehicle\label{itm:vehicle}] Something
\item [Bus,\label{itm:bus}] A type of \nameref{itm:vehicle}
\item [Car\label{itm:car}] A type of \nameref{itm:vehicle} smaller than a \nameref{itm:bus}
\end{description}
References: `\nameref{itm:bus}' and `\nameref{itm:car}'.
\end{document}
标点符号后,空间因子大于 1000(使用标准\nonfrenchspacing
设置;如果使用\frenchspacing
,还会加载amsthm
调整内容,以便仍可使用空间因子)。
为了使用\ref
而不是\nameref
,只需要做一个小小的改变;我建议在\ref
数字和\nameref
文本的使用上保持一致,但我能理解不改变现有文档的必要性。
\documentclass{article}
\usepackage{xstring}
\usepackage{hyperref}
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
\begingroup
\let\label\@gobble
\phantomsection
\protected@edef\daniel@currentlabel{#1}%
\sbox\z@{\daniel@currentlabel\xdef\daniel@sf{\the\spacefactor}}%
\ifnum\daniel@sf>1000
\noexpandarg
\expandafter\StrGobbleRight\expandafter{\daniel@currentlabel}{1}[\daniel@currentlabel]%
\fi
\def\x{%
\expandafter\endgroup
\expandafter\def\expandafter\@currentlabel
\expandafter{\daniel@currentlabel}}%
\x
\orgdescriptionlabel{#1}%
}
\makeatother
\begin{document}
\section{Definitions}
\begin{description}
\item [Vehicle\label{itm:vehicle}] Something
\item [Bus,\label{itm:bus}] A type of \ref{itm:vehicle}
\item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description}
References: `\ref{itm:bus}' and `\ref{itm:car}'.
\end{document}