1

I am trying to create a simple if command to see if a reference go across one or more pages. In the case of one single page, it puts the "p." prefix, otherwise it puts the "pp." prefix.

I am using zref package. I am trying to use the command \zref@extractdefault to extract the value of the page and make a teste between the two labels. I saw at zref manual that the command \zref@extractdefault is expansible, but when I try to compile I get the error "Missing number, treated as zero."

What I am doing wrong?

\documentclass{scrbook}

\usepackage[USenglish]{babel}
\usepackage[titleref, user, abspage]{zref}
\usepackage{etoolbox}
\makeatletter%
\def\pagetest#1#2{%
\ifnumcomp{\zref@extractdefault{#1}{page}}{=}{\zref@extractdefault{#2}{page}}{p. \zref[page]{#1}}{pp. \zref[page]{#1}-\zref[page]{#2}}%
}%
\makeatother%
\begin{document}

Bla bla\zlabel{foo1}
\newpage
Bla bla bla\zlabel{foo2}

\pagetest{foo1}{foo2}

\zref[page]{foo1} and \zref[page]{foo2}


\end{document}
2
  • 1
    I'm not sure I get your use case well, so I'm not sure it covers it, but you may be interested in ctan.org/pkg/zref-check.
    – gusbrs
    Sep 17, 2021 at 18:50
  • 1
    \zref@extractdefault has three arguments, the last is the default value. Sep 17, 2021 at 18:57

1 Answer 1

2

You're allllmost there!: \zref@extractdefault takes three arguments, not two.

\documentclass{scrbook}

\usepackage[USenglish]{babel}
\usepackage[titleref, user, abspage]{zref}
\usepackage{etoolbox}
\makeatletter%
\def\pagetest#1#2{%
\ifnumcomp{\zref@extractdefault{#1}{page}{0}}{=}{\zref@extractdefault{#2}{page}{0}}{p. \zref[page]{#1}}{pp. \zref[page]{#1}-\zref[page]{#2}}%
}%
\makeatother%
\begin{document}

Bla bla\zlabel{foo1}
\newpage
Bla bla bla\zlabel{foo2}

\pagetest{foo1}{foo2}

\zref[page]{foo1} and \zref[page]{foo2}

\end{document}

enter image description here

3
  • 1
    the default should be a number, without it \ifnumcomp won't be happy. Sep 17, 2021 at 19:09
  • @UlrikeFischer Indeed, good point. I placed a safe "0" there, though probably this could be handled by simply using \zref@extract in this case...
    – gusbrs
    Sep 17, 2021 at 19:16
  • The devil is in the details! Thank you very much! Sep 17, 2021 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .