I'm working on a two-side scrbook
in which references are made to other sections, figures, etc. If the target is on the very same page, I want to omit a page reference in the text (i.e., if we're on page 30, there souldn't be a text that says "c.f. p. 30"). For a label #1, this achieved with the following command:
\newcounter{samepage}
\newcommand{\IfRefOnSamePage}[3]{%
\stepcounter{samepage}%
\label{cmd:samepage-\thesamepage}%
\ifthenelse{\equal{\getpagerefnumber{#1}}{\getpagerefnumber{cmd:samepage-\thesamepage}}}%
{#2}%
{#3}%
}
It outputs #2 if the target is on the same page, otherwise #3.
Now since the book is two-sided, I am counting on the readers to also not need a reference if the target is on the facing page. I thought it should be a simple task to extend the condition to catch these cases, using the xifthen
package and Ifthispageeven
/-odd
:
\ifthenelse{%
\equal{\getpagerefnumber{#1}}{\getpagerefnumber{cmd:samepage-\thesamepage}}%
\OR
\Ifthispageodd{\equal{\getpagerefnumber{#1}}{\numexpr\getpagerefnumber{cmd:samepage-\thesamepage}-1}}{false}%
\OR
\Ifthispageeven{\equal{\getpagerefnumber{#1}}{\numexpr\getpagerefnumber{cmd:samepage-\thesamepage}+1}}{false}%
}%
{#2}%
{#3}%
This attempt yields numerous errors "Undefined control sequence", "Missing number", "Argument has an extra }", and so on. I triple-checked, but is there something wrong with my \ifthenelse
syntax? Or with the way I'm trying to do the +/- 1 calculations?
Consider this MWE:
\documentclass{scrbook}
\usepackage{xifthen}
\usepackage{hyperref}
\newcounter{samepage}
\newcommand{\IfRefOnSamePage}[3]{%
\stepcounter{samepage}%
\label{cmd:samepage-\thesamepage}%
\ifthenelse{\equal{\getpagerefnumber{#1}}{\getpagerefnumber{cmd:samepage-\thesamepage}}}%
{#2}%
{#3}%
}
\newcommand{\IfRefOnDoublePage}[3]{%
\stepcounter{samepage}%
\label{cmd:samepage-\thesamepage}%
\ifthenelse{%
\equal{\getpagerefnumber{#1}}{\getpagerefnumber{cmd:samepage-\thesamepage}}%
\OR
\Ifthispageodd{\equal{\getpagerefnumber{#1}}{\numexpr\getpagerefnumber{cmd:samepage-\thesamepage}-1}}{false}%
\OR
\Ifthispageeven{\equal{\getpagerefnumber{#1}}{\numexpr\getpagerefnumber{cmd:samepage-\thesamepage}+1}}{false}%
}%
{#2}%
{#3}%
}
\begin{document}
\section{First page}
Text.
\newpage
\section{Second page}
% This works:
Read more about this\IfRefOnSamePage{target}{ on this page}{ on page \pageref{target}}.
% This throws errors, but also works:
Read more about this\IfRefOnDoublePage{target}{ here}{ on page \pageref{target}}.
\subsection{I am the reference target}
\label{target}
Text.
\newpage
\section{Third page}
% This works as expected, but it yields a reference I actually don't want:
Read more about this\IfRefOnSamePage{target}{ on this page}{ on page \pageref{target}}.
% This doesn't:
Read more about this\IfRefOnDoublePage{target}{ here}{ on page \pageref{target}}.
\end{document}
varioref
package\pageref
as in the MWE, I'd use some customised\marginpar
in which the actual reference is printed.