2

Is there a way to make the following MWE work with roman page numbers?

\documentclass{article}
\usepackage{refcount}
\newcounter{mycount}
\begin{document}
%\pagenumbering{roman}
\stepcounter{mycount}
\label{test:page:\themycount}
\refused{test:page:\themycount}
\ifodd\getpagerefnumber{test:page:\themycount}
ODD
\else
EVEN
\fi
\end{document}

With roman page numbers, there's the following error:

! Missing number, treated as zero.
<to be read again>
                   i
l.9 \ifodd\getpagerefnumber{test:page:\themycount}

EDIT:

With egreg's comment the following works in textual context but fails again in programming context:

\documentclass{article}
\usepackage[user,abspage]{zref}
\newcounter{mycount}
\begin{document}
\pagenumbering{roman}
\stepcounter{mycount}
\zlabel{test:page:\themycount}
\zrefused{test:page:\themycount}
\zref[abspage]{test:page:\themycount}
% \ifodd\zref[abspage]{test:page:\themycount}
% ODD
% \else
% EVEN
% \fi
\end{document}
| improve this question | | | | |
  • I guess you want to look at zref. – egreg Jul 10 '16 at 11:13
  • @egreg This works in textual context but fails again in programming context, see edit. – Josef Jul 10 '16 at 11:45
2

\ifodd expects stuff that expands to a number. The documentation of zref marks expandable macros with exp or exp2:

\documentclass{article}
\usepackage[user,abspage]{zref}
\newcounter{mycount}
\begin{document}
\pagenumbering{roman}
\stepcounter{mycount}
\zlabel{test:page:\themycount}
\zrefused{test:page:\themycount}
\zref[abspage]{test:page:\themycount}

\makeatletter
\ifodd\zref@extractdefault{test:page:\themycount}{abspage}{0}\relax
  ODD
\else
  EVEN
\fi
\makeatother

\end{document}

Result

Module thepage provides property pagevalue, the page number as number value. The following example uses this with some macro definitions. It will also work and detects the oddness of \thepage, if the oddness of \thepage is out of sync with the oddness of the absolute page number.

\documentclass{article}
\usepackage{zref-thepage}% defines property pagevalue

\newcounter{mycount}
\renewcommand*{\themycount}{\the\value{mycount}}

\makeatletter
\newcommand*{\myzpagelabel}[1]{%
  \zref@labelbyprops{#1}{pagevalue}%
}
\newcommand*{\ifzpageodd}[1]{%
  \zref@refused{#1}%
  \ifodd\zref@extractdefault{#1}{pagevalue}{0} %
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\begin{document}
\pagenumbering{roman}

\stepcounter{mycount}
\myzpagelabel{test:page:\themycount}

Page number \thepage\ is %
\ifzpageodd{test:page:\themycount}{odd}{even}.

\end{document}

Result

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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