6

If I want to test whether a page is odd/even (i.e. modulo 2) then I can use the following:

\ifthenelse{\isodd{\thepage}}{}{}

What can I do if I want to test whether a page is modulo 4?

A minimal example:

\documentclass{article}
\usepackage{ifthen}

\newcommand{\ismodfour}{???}

\begin{document}
Page 1: \ifthenelse{\ismodfour{\thepage}}{mod 4}{not mod 4} \newpage
Page 2: \ifthenelse{\ismodfour{\thepage}}{mod 4}{not mod 4} \newpage
Page 3: \ifthenelse{\ismodfour{\thepage}}{mod 4}{not mod 4} \newpage
Page 4: \ifthenelse{\ismodfour{\thepage}}{mod 4}{not mod 4} \newpage
Page 5: \ifthenelse{\ismodfour{\thepage}}{mod 4}{not mod 4} \newpage
\end{document}
Ludovic C.
  • 8,888
Geoff
  • 2,637
  • See If Then Else for odd page/even page for testing if a page is even or odd – cgnieder Oct 06 '13 at 12:32
  • @cgnieder I followed the second link, using the package intcalc and managed to solve the problem: \newcommand{\ismodfour}[1]{\equal{\intcalcMod{#1}{4}}{0}}. Many thanks! – Geoff Oct 06 '13 at 12:43
  • 3
    You can not reliably check the page number in the main page, the value used in those tests is one more than the page last shipped out but it may not be the value used on the page finally shipped out with that text as floats and other inserts or just general page breaking decisions may put that text on a later page after the test has been made. You can only test the value in the output routine (or immediately after \clearpage – David Carlisle Oct 06 '13 at 12:52
  • @DavidCarlisle Couldn't you reliably check the page number by setting a label where you want to do the test, and then run the test on the return value of \pageref{...}? – A.Ellett Oct 06 '13 at 15:16
  • @A.Ellett yes except you have to take care of the first run, when the label isn't defined and the fact that \pageref returns the print form (and with hyperref link information) and not a pure number that you can pass to the mod 4 or numeric test. – David Carlisle Oct 06 '13 at 17:05
  • 1
    I don't believe this is a duplicate. Indeed, a combination of the answers I linked to is needed to make a reliable mod4 page check... – cgnieder Oct 06 '13 at 18:38

1 Answers1

7

Here's a combination of egreg's expandable \modulo and the changepage package. It uses changepage's mechansim via labels to check the page number.

Note that this needs at least two compilations to give correct results.

\documentclass{article}

% only for this example make ridicously small pages:
\usepackage[papersize={3cm,.5cm},margin=0pt]{geometry}

% use `changepage's strict page check mechansim:
\usepackage[strict]{changepage}

% @egreg's expandable \modulo{<n>}{<m>}
%   see https://tex.stackexchange.com/a/34449/5049
\def\truncdiv#1#2{((#1-(#2-1)/2)/#2)}
\def\moduloop#1#2{(#1-\truncdiv{#1}{#2}*#2)}
\def\modulo#1#2{\number\numexpr\moduloop{#1}{#2}\relax}

\makeatletter
\newif\if@modfourpage

% a mod 4 version of `changepage's \checkoddpage
\DeclareRobustCommand\checkmodfourpage{%
  \@modfourpagefalse
  \ifstrictpagecheck
    \stepcounter{cp@cntr}\pmemlabel{\cplabel\thecp@cntr}%
    \cp@tempcnt=\pmemlabelref{\cplabel\thecp@cntr}\relax
    \ifnum\modulo{\cp@tempcnt}{4}=0 \@modfourpagetrue\fi
  \else
    \ifnum\modulo{\cp@tempcnt}{4}=0 \@modfourpagetrue\fi
  \fi
}


% \ifmodfourpage{<true>}{<false>}:
\newcommand*\ifmodfourpage{%
  \checkmodfourpage
  \if@modfourpage
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}

\makeatother

% \strictpagecheck

\begin{document}

\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}\newpage
\thepage: \ifmodfourpage{mod 4}{not mod 4}

\end{document}

enter image description here

cgnieder
  • 66,645