# How can I calculate the difference of 2 counters (pageref)?

• How can I calculate the difference between 2 counters (like page numbers), so that pdfLateX outputs "3" in the last line of my example?

• and how could I calculate the difference between the "current" page (where the calculation is printed) and \pageref{A} (without explicitly defining \label{B})?

Example:

\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}

-
Note that the page counter is incremented asynchronously some time during the output routine. Therefore you can't be sure that the difference is correct when you don't have a second label. An exception is directly after a \clearpage. – Martin Scharrer Dec 13 '11 at 18:02

The refcount package can extract the number from a reference. That only works for arabic page numbers, of course. And there must not be any \addtocounter{page}{...} or \setcounter{page}{...} between A and B. For those more complicated situations, \theCurrentPage of the pageslts package can be used.

\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%
% (maybe
% 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%
% (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
Just added 10 to the page number here.

\newpage

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

\makeatletter
\@tempcnta=\mypageB\relax%
% (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}

-

With the limitations already explained by Stephen, you can define

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


so that

\pagedifference{A}{B}


will give the number of pages from that in which \label{A} occurs to that where \label{B} occurrs. This needs (as usual when using the \label-\ref system) a couple of compilations to stabilize.

-

A TeX-y solution:

\newcount{\temp}
\temp=\pageref{B}

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

\newpage
\temp=\arabic{page}

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


A more LaTeX-y one:

\newcounter{temp}
\setcounter{temp}{\pageref{B}}

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

\newpage
\setcounter{temp}{\arabic{page}}

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

-
@Stephen, Ant: \pageref isn't expandable when hyperref (a very common package after all) is loaded. This will break its usage inside the above assignments. It is better to use the zref package and to explicit as for the plain page number only. There should be some answers here which show that. The refcount package mentioned in the other answer is also good for this purpose. – Martin Scharrer Dec 13 '11 at 17:59
@MartinScharrer: Not only hyperref but also babel (as used in the MWE of the OP) causes a problem: \documentclass{scrbook} \usepackage[ngerman]{babel} \begin{document} \label{B} \newcount{\temp} \temp=\pageref{B} \end{document} results in \temp=\count90 ! Missing number, treated as zero. <to be read again> \protect l.11 \temp=\pageref {B} ?. I had not expected that. Thus, Ant's code works, but not with babel (or hyperref or with non-arabic page-numbers or when the page-number is not-continuous). There are always new ways to break things in TeX ;-) . – Stephen Dec 14 '11 at 8:37
@Stephen, re breaking things in TeX: indeed...! – Ant Dec 14 '11 at 15:54