如果目标位于同一页或对页,则省略页面引用

如果目标位于同一页或对页,则省略页面引用

我正在制作一个双面文件scrbook,其中引用了其他章节、图表等。如果目标在同一页上,我想省略文本中的页码引用(即,如果我们在第 30 页,则不应该有写着“cfp 30”的文本)。对于标签 #1,可以使用以下命令实现此目的:

\newcounter{samepage}
\newcommand{\IfRefOnSamePage}[3]{%
    \stepcounter{samepage}%
    \label{cmd:samepage-\thesamepage}%
    \ifthenelse{\equal{\getpagerefnumber{#1}}{\getpagerefnumber{cmd:samepage-\thesamepage}}}%
    {#2}%
    {#3}%
}

如果目标在同一页面上,则输出#2,否则输出#3。

现在,由于这本书是双面的,我希望读者如果目标在对页上,也不需要参考。我认为使用包xifthenand 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}%

这次尝试产生了许多错误,“未定义控制序列”、“缺少数字”、“参数有多余的 }”等等。我检查了三遍,但我的\ifthenelse语法有问题吗?还是我尝试进行 +/- 1 计算的方式有问题?


考虑一下这个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}

相关内容