Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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?

share|improve this question
    
Welcome to TeX.sx! –  Peter Jansson Jan 26 '13 at 11:18

1 Answer 1

up vote 6 down vote accepted

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

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

enter image description here

share|improve this answer
    
That works perfectly. Thanks a lot! :) –  MichaelF Jan 26 '13 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.