Decide whether a label occurs in the current chapter

I want to decide if some label occurs in the same chapter as the current one. I use the zref package as suggested in Chapter ref with LaTeX to find out in which chapter a label occurs, and compare this to the current one. I tried to do this with both the counter's display and value.

Here is what I do:

\documentclass{scrbook}

\usepackage[user]{zref}

\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@newprop{chaptervalue}{\the\value{chapter}}
\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}

In any case, the result is "No.", even if the values are equal. I was using \ifstr from the KOMA-Skript package to compare the strings (as I am using this documentclass anyway), but this also happens when one uses other string comparison functions (I have tried \ifthenelse{\equal{<str1>}{<str2>}}{<true>}{<false>} from the ifthen package and \IfStrEq from the xstring package).

Does anyone understand why this happens? Or can you imagine another way to compare these values?

-
Welcome to TeX.sx! –  Peter Jansson Jan 26 '13 at 11:18

You have to use the "expandable" version of \zref, which is \zref@extract; the optional argument to \zref makes impossible to use it for this kind of comparisons.

\documentclass{scrbook}

\usepackage[user]{zref}

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

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

I've used only chaptervalue, so this is independent from the representation of the chapter number.

-
That works perfectly. Thanks a lot! :) –  MichaelF Jan 26 '13 at 11:26