我如何计算两个计数器(pageref)的差值?

我如何计算两个计数器(pageref)的差值?
  • 我如何计算两个计数器(如页码)之间的差异,以便pdfLateX在示例的最后一行输出“3”?

  • 以及如何计算“当前”页面(打印计算结果的位置)和\pageref{A}(未明确定义\label{B})之间的差异?

例子:

\documentclass{scrbook}

\usepackage[ngerman]{babel} 
\usepackage[latin1]{inputenc}

\begin{document}

Here is label A \label{A}

\newpage
text
\newpage
text
\newpage

Here is label B \label{B}

There are \pageref{B}-\pageref{A} pages between labels A and B. 

\end{document}

答案1

引用计数包可以从参考文献中提取数字。arabic当然,这只适用于页码。并且 A 和 B 之间不能有任何\addtocounter{page}{...}\setcounter{page}{...}。对于那些更复杂的情况\theCurrentPage页面包就可以使用了。

\documentclass{scrbook}

\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}

\usepackage{refcount}

\usepackage{pageslts}

\pagenumbering{arabic}

\begin{document}

Here is label A. \label{A}

\newpage
text
\newpage
text
\newpage

Here is label B \label{B}

\makeatletter
\@tempcnta=\getpagerefnumber{B}\relax%
\advance\@tempcnta by -\getpagerefnumber{A}%
% (maybe
% \advance\@tempcnta by -1%
% depending on your definition of "between")
\xdef\pagedifference{\the\@tempcnta}%
\makeatother

There are \pagedifference~pages between labels A and B.

\makeatletter
\@tempcnta=-\getpagerefnumber{A}\relax%
\advance\@tempcnta by \thepage%
% (maybe \advance\@tempcnta by -1, depending on definition of "between")
\xdef\pagedifference{\the\@tempcnta}%
\makeatother

There are \pagedifference~pages between labels A and the current page.


\newpage
\pagenumbering{Roman}

Here is page \thepage{} (the \theCurrentPage{}.~page).
\xdef\mypageA{\theCurrentPage}

\newpage
text
\newpage
text
\addtocounter{page}{10}
Just added 10 to the page number here.

\newpage

Here is page \thepage{} (the \theCurrentPage{}.~page).
\xdef\mypageB{\theCurrentPage}

\makeatletter
\@tempcnta=\mypageB\relax%
\advance\@tempcnta by -\mypageA%
% (maybe \advance\@tempcnta by -1, depending on definition of "between")
\xdef\pagedifference{\the\@tempcnta}%
\makeatother

There are \pagedifference~pages between page A and the current page B.

\end{document}

答案2

根据 Stephen 已经解释的限制,你可以定义

\usepackage{refcount}
\newcommand{\pagedifference}[2]{%
  \number\numexpr\getpagerefnumber{#2}-\getpagerefnumber{#1}\relax}

以便

\pagedifference{A}{B}

\label{A}将给出从发生 到 发生的页数。这需要(使用-系统\label{B}时通常如此)进行几次编译才能稳定下来。\label\ref

答案3

TeX-y 解决方案:

\newcount{\temp}
\temp=\pageref{B}
\advance\temp by -\pageref{A}\relax

There are \the\temp\ pages between labels A and B.

\newpage
\temp=\arabic{page}
\advance\temp by -\pageref{A}\relax

There are \the\temp\ pages between this one and label A.

更加 LaTeX-y 的一个:

\newcounter{temp}
\setcounter{temp}{\pageref{B}}
\addtocounter{temp}{-\pageref{A}}

There are \arabic{temp}\ pages between labels A and B.

\newpage
\setcounter{temp}{\arabic{page}}
\addtocounter{temp}{-\pageref{A}}

There are \arabic{temp}\ pages between this one and label A.

相关内容