3

As described here I'm using chappg's \pagenumbering{bychapter} (implicitly via the auto option) to have pages numbered in the style \thechapter-\thepage. But now a \ifodd\thepage fails horribly, as this MWE shows:

\documentclass{scrbook}  % same for book, btw
\usepackage[auto]{chappg}

\newcommand*{\whereami}{%
    \ifodd\thepage%
        odd page
    \else
        even page
    \fi
}

\begin{document}
  \whereami\clearpage\whereami

  \chapter{new}
  \whereami\clearpage\whereami

  \chapter{again}
  \whereami\clearpage\whereami
\end{document}

While the first two pages (outside a chapter) correctly output "odd page" and "even page" respectively, the first actual chapter yields "-1odd page" and "-2odd page" for its two pages, and the second one "even page" twice.

So it seems chappg overrides \thepage to become e.g. 1-2, and \ifodd\thepage only checks the chapter number's oddness, in which case the -2 becomes part of the then output.

I already tried fixing this by using pageslts's \theCurrentPage instead, but unfortunately that seems to mess around with \thepage (or \pagenumbering?) as well, since by merely including that package in addition I get the error

! Argument of \extract@ has an extra }.
<inserted text>
                \par
l.18   \whereami\clearpage
                          \whereami

So, how can I fix this mess?

8
  • 1
    Isn't this kind of a duplicate of tex.stackexchange.com/questions/6143/…
    – cgnieder
    Dec 2, 2015 at 15:28
  • @clemens That shows how to get that page numbering style, not why my flawed \ifodd\thepage failed Dec 2, 2015 at 15:31
  • That's why I said “kind of” and haven't voted to close (BTW: the answers here so far do the same: they show how to get the correct numbering and not where your error is). Since your question is: “how can I fix this mess?” the other question might still be a duplicate…
    – cgnieder
    Dec 2, 2015 at 15:34
  • 1
    You are right that \thepage is not a fixed quantity until page shipout. Thus, testing upon it can lead to spurious results. Dec 2, 2015 at 15:48
  • 1
    \thepage is indeed wrong for testing the page number since \thepage might very well be \roman{page} or something, i.e., it does not have to expand to a number so testing for \value{page} would be better. But this is only a secondary problem. @StevenB.Segletes mentions the real problem: page numbers are only fixed when the page is shipped out – this is the reason for the various ways for checking even/odd pages which in the end all fetch the information from the aux file.
    – cgnieder
    Dec 3, 2015 at 10:46

4 Answers 4

3

Use KOMA's \ifthispageodd instead:

\newcommand*{\whereami}{%
    \ifthispageodd{%
        odd page
    }{%
        even page
    }%
}

You are not required to use a KOMA script class, you can also just \usepackage{scrextend}.

See also this question.

2

The changepage package could be of use.

\documentclass{scrbook}  % same for book, btw

\usepackage{changepage}

\newcommand\whereami{\checkoddpage\ifoddpage odd\else even\fi}

\begin{document}
  \whereami\clearpage\whereami

  \chapter{new}
  \whereami\clearpage\whereami

  \chapter{again}
  \whereami\clearpage\whereami
\end{document}
1
  • This one doesn't depend on KOMA, which is potentially good (though one can still use \usepackage{scrextend} without a KOMA class). On the other hand, I'll most certainly forget to put the \checkoddpage in place... Dec 2, 2015 at 15:42
2

You can use zref for this, see this answer by Martin Scharrer:

\documentclass{scrbook}  % same for book, btw
\usepackage[auto]{chappg}
\usepackage[user,abspage]{zref}

\newcounter{whereami}
\makeatletter
\newcommand*{\whereami}{%
  \refstepcounter{whereami}%
  \zlabel{\thewhereami @zref}%
    \ifodd\zref@extractdefault{\thewhereami @zref}{abspage}{0}\relax
        odd page
    \else
        even page
    \fi
}
\makeatother

\begin{document}
  \whereami\clearpage\whereami

  \chapter{new}
  \whereami\clearpage\whereami

  \chapter{again}
  \whereami\clearpage\whereami
\end{document}

Of course this needs multiple passes, but using the aux file is the only reliable method for checking page numbers.

1

I already tried fixing this by using pageslts's \theCurrentPage instead, but unfortunately that seems to mess around with \thepage (or \pagenumbering?) as well, since by merely including that package in addition I get the error

After adding \usepackage{pageslts} and \pagenumbering{arabic} before \usepackage[auto]{chappg} and replacing \thepage with \theCurrentPage, i.e.

\documentclass{scrbook}  % same for book, btw
\usepackage{pageslts}% <- changed
\pagenumbering{arabic}% <- changed
\usepackage[auto]{chappg}


\newcommand*{\whereami}{%
    \ifodd\theCurrentPage% <- changed
        odd page
    \else
        even page
    \fi
}

\begin{document}
  \whereami\clearpage\whereami

  \chapter{new}
  \whereami\clearpage\whereami

  \chapter{again}
  \whereami\clearpage\whereami
\end{document}

I do not have any problem compiling your MWE (current TeXLive 2015). Did you use another order of loading perhaps (or older versions of scrbook/pageslts/chappg)?

1
  • Hm, I'm not sure, but I think the problem was that I have a preamble, and \mainmatter implicitly does \pagenumberung{arabic} which overrides chappg's style then - so my MWE is too minimal... Feb 26, 2016 at 12:05

You must log in to answer this question.

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