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.

Is there a simple way to retrieve an absolute page number from a label, where by absolute I mean counting all pages as integers sequentially starting from the first page of the document, without any resets.

To be more clear, if I have three pages of front-matter with roman numerals, then 10 pages of content with arabic numbers, and I reference the third page of the content, \pageref will return 3, but I want an alternate command that will return 6 since it's the 6th overall page in the document.

Furthermore, I'd like the references returned as a basic integer so they can be used in a control statement or for arithmetic comparisons, e.g. I'd like to be able to do something like the following, where \abspageref is a hypothetical command that counts from the beginning of the document not the last page counter reset, and returns a simple integer with no unit associated:

if{\abspageref{labelA} > \abspageref{labelB}{Some text.}{Some alternate text.}

I realize that's not a correct if then control statement either, it's just to illustrate what I'm trying to do.

Thanks.

Bonus question: why is something like abspageref not a core part of Latex already? It seems like a predictable feature that advanced users would want, and when I search for solutions online, I see a lot of very complicated solutions that I haven't had any success with yet.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

Use the zref package, especially its user and abspage modules:

\usepackage[user,abspage]{zref}
% or
\usepackage{zref-user,zref-abspage}


% Label
\zlabel{mylabel}

% Reference
\zref[abspage]{mylabel}

% or, if needed as simple number:
\makeatletter
\zref@extract{mylabel}{abspage}
% also available: \zref@extractdefault
\makeatother

So, your example can be coded as:

\makeatletter
\newcommand\cmpabspage[2]{%
   \ifnum\zref@extract{#1}{abspage}>\zref@extract{#2}{abspage}
       \expandafter\@firstoftwo
   \else
       \expandafter\@secondoftwo
   \fi
}
\makeatother

% ...
\cmpabspage{labelA}{labelB}{Some text.}{Some alternate text.}
% where the labels are set using `\zlabel{labelA}` etc.

I have used this package before with good results. It's from Heiko Oberdiek, who also wrote many other good packages. Note that the counter starts at zero and is incremented at the beginning of the page ship-out routine. This means that it has value 0 when the first page is typeset or written by \immediate\write but will have the value of '1' before the page is finally shipped-out, i.e. a normal \write will write it as '1' e.g. into the .aux file.

share|improve this answer
    
Hi, thanks. This is pretty close to what I need to do. If I want to actually check that labelA is at least two greater than labelB, how would I modify your method? I.e. I want to make the third line of your macro \ifnum\zref@extract{#1}{abspage}>\zref@extract{#2}{abspage}+1 (notice the +1 at the end), but that doesn't work directly. Thanks. –  SSilk Jul 28 '11 at 2:20
1  
@SSilk: You can use \numexpr for this: \ifnum\zref@extract{#1}{abspage}>\numexpr\zref@extract{#2}{abspage}+1\relax –  Martin Scharrer Jul 28 '11 at 8:58
    
That did the job. Thanks. –  SSilk Jul 31 '11 at 20:30
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.