0

I have a command that checks to see if the page number of a label is even/odd, using \ifthenelse{\isodd{\pageref{mylabel}}}... that works well. I just came across a situation where I try to use the same command in my appendix, but my command fails.

This is because I've changed the appendix numbering. I use

\appendix
\pagenumbering{arabic}\renewcommand{\thepage}{A.\arabic{page}}

to create page numbers like A.1, A.2, etc. The \isodd command returns false on all these page numbers.

I'd like to have one command that is able to check for even/odd in both cases - regular and appendix page numbering.

I may be open to a different page numbering scheme in the appendix, though it needs to be clearly different than the regular page numbering.

Edit: Here's minimal code showing the effect I see. On Page 3, it shows it knows which label is on an even/odd page. On Page 6, it shows incorrectly that both are on even pages.

\documentclass{book}
\usepackage{ifthen}
\newcommand{\pagecheck}[1]{\ifthenelse{\isodd{\pageref{#1}}}{ The label is on an odd page. }{ The label is on an even page. }}
\begin{document}
\section{One}\label{firstsection}
\clearpage
\section{Two}\label{secondsection}
\clearpage
\pagecheck{firstsection}\pagecheck{secondsection}
\clearpage
\appendix
\pagenumbering{arabic}\renewcommand{\thepage}{A.\arabic{page}}
\section{Appendix A}\label{appA}
\clearpage
\section{Appendix B}\label{appB}
\clearpage
\pagecheck{appA}\pagecheck{appB}
\end{document}
  • Could you post a full compilable code that we can play with? – Bernard Jul 19 '18 at 12:06
  • on the first run mylabel won't be known so \pageref acts as 0 and \ifodd will be false, but it should pick up on the second run, the setting of \thepage is not used at all, so your \renewcommand has no affect on the test – David Carlisle Jul 19 '18 at 12:43
1

This solution assumes a single . before the number (when not an integer).

BTW, if you are trying to determine whether the current page is odd or even, you can use the ifoddpage package. It uses \arabic{page} instead of \thepage.

\documentclass{book}
\usepackage{ifthen}
\usepackage{refcount}% or hyperref
\usepackage{xstring}
\newcommand{\pagecheck}[1]{\edef\mystring{\getpagerefnumber{#1}}%
  \IfInteger{\mystring}{}{\StrBehind{\mystring}{.}[\mystring]}%
  \ifthenelse{\isodd{\mystring}}{ The label is on an odd page. }{ The label is on an even page. }}
\begin{document}
\section{One}\label{firstsection}
\clearpage
\section{Two}\label{secondsection}
\clearpage
\pagecheck{firstsection}\pagecheck{secondsection}
\clearpage
\appendix
\pagenumbering{arabic}\renewcommand{\thepage}{A.\arabic{page}}
\section{Appendix A}\label{appA}
\clearpage
\section{Appendix B}\label{appB}
\clearpage
\pagecheck{appA}\pagecheck{appB}
\end{document}

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.