Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
  • 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}
share|improve this question
1  
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
add comment

3 Answers

up vote 8 down vote accepted

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%
\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}
share|improve this answer
add comment

A TeX-y solution:

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

A more LaTeX-y one:

\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.
share|improve this answer
2  
@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
1  
@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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.