判断标签是否出现在当前章节中

判断标签是否出现在当前章节中

我想判断某个标签是否出现在当前章节的同一章节中。我zref使用使用 LaTeX 进行章节引用找出标签出现在哪一章,并将其与当前章节进行比较。我尝试使用计数器的展示价值

这是我的做法:

\documentclass{scrbook}

\usepackage[user]{zref}

\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@newprop{chaptervalue}{\the\value{chapter}}
\zref@addprops{main}{chapter,chaptervalue}
\makeatother

\begin{document}

\chapter{Chapter 1}
\zlabel{chapone}

Compare \thechapter{} with \zref[chapter]{chapone}. Equal:
\ifstr{\thechapter}{\zref[chapter]{chapone}}{Yes.}{No.}
Compare \thechapter{} with \zref[chapter]{chaptwo}. Equal:
\ifstr{\thechapter}{\zref[chapter]{chaptwo}}{Yes.}{No.}

Compare \the\value{chapter} with \zref[chaptervalue]{chapone}. Equal:
\ifstr{\thechapter}{\zref[chapter]{chapone}}{Yes.}{No.}
Compare \the\value{chapter} with \zref[chaptervalue]{chaptwo}. Equal:
\ifstr{\thechapter}{\zref[chapter]{chaptwo}}{Yes.}{No.}

\chapter{Chapter 2}
\zlabel{chaptwo}

\end{document}

无论如何,结果都是“否”,即使值相等。我使用\ifstrKOMA-Skript 包中的函数来比较字符串(因为我无论如何都在使用这个 documentclass),但是当使用其他字符串比较函数时也会发生这种情况(我尝试过使用包\ifthenelse{\equal{<str1>}{<str2>}}{<true>}{<false>}中的函数ifthen和包\IfStrEq中的函数xstring)。

有人知道为什么会发生这种情况吗?或者你能想象另一种方法来比较这些值吗?

答案1

您必须使用 的“可扩展”版本\zref,即\zref@extract;可选参数 使得\zref无法将其用于这种比较。

\documentclass{scrbook}

\usepackage[user]{zref}

\makeatletter
\zref@newprop{chaptervalue}{\the\value{chapter}}
\zref@addprops{main}{chaptervalue}

\newcommand\comparechapter[1]{%
  \ifstr{\number\value{chapter}}{\zref@extract{#1}{chaptervalue}}{Yes.}{No.}%
}
\makeatother

\begin{document}

\chapter{Chapter 1}
\zlabel{chapone}

Compare \number\value{chapter} with \zref[chaptervalue]{chapone}. Equal:
\comparechapter{chapone}

Compare \number\value{chapter} with \zref[chaptervalue]{chaptwo}. Equal:
\comparechapter{chaptwo}

\chapter{Chapter 2}
\zlabel{chaptwo}

\end{document}

我只使用了chaptervalue,所以这与章节编号的表示无关。

在此处输入图片描述

相关内容