18

I want to get the real page number. If I use \thepage, I get problems, when I change the pagenumbering. For pagenumbering gobble, it disappears, if I change from arabic to roman, it resets the counter. There must be actually some internal counter like \p@count (I made that up). What is it?

The following example should show 13 on the last page.

\documentclass[a5paper, 11pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}

\begin{document}
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage

\pagenumbering{gobble}

Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage

\pagenumbering{arabic}

Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage

\end{document}
Werner
  • 603,163
MaestroGlanz
  • 2,348

4 Answers4

10

The package xassoccnt provides a mean to force a stepping of an associated counter each time a driver counter is stepped as well -- the driver counter is page here.

This has an advantage over the totcount or lastpage packages, since the associated counter realpage (any name will do, but section etc. is unwise, of course ;-)) is not reset (unless explicitly done so with \setcounter) -- As such, it counts the total absolute values of pages.

So even in case of \pagenumbering commands, this does not have an effect on the realpage counter.

Please note that page is an unrealiable counter in some sense!

\documentclass[a5paper, 11pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}

\usepackage{xassoccnt}

\newcounter{realpage}
\DeclareAssociatedCounters{page}{realpage}
\AtBeginDocument{%
  \stepcounter{realpage}
}

\begin{document}
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\pagenumbering{gobble}

Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\pagenumbering{arabic}

Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\end{document}

A version with \NewTotalDocumentCounter (needs xassoccnt v.1.2. and above)

\documentclass[a5paper, 11pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}

\usepackage{xassoccnt}

\NewTotalDocumentCounter{totalrealpage}
\NewDocumentCounter{realpage}

\DeclareAssociatedCounters{page}{realpage,totalrealpage}
\makeatletter
\AtBeginDocument{%
  \setcounter{realpage}{1}
}

\makeatother

\begin{document}
In diesem Dokument sind insgesamt \TotalValue{totalrealpage} Seiten!

Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\pagenumbering{gobble}

Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\pagenumbering{arabic}

Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage
Seite \therealpage
\newpage

\end{document}
  • Thanks. This means, there isn't a real internal counter. So this has to be tweaked. – MaestroGlanz Jun 30 '16 at 18:58
  • @MaestroGlanz: I know of none, but this does not mean there is none, of course! –  Jun 30 '16 at 18:58
  • 1
    if you're willing to process the file twice, it should be possible to place an anchor on the page with a label, then evaluate \pageref in the next cycle. not trivial or immediate, but generally accurate. – barbara beeton Jun 30 '16 at 19:02
  • You say unreliable. What does this mean. I actually just want to print the real last page count to the aux-file within \AtEndDocument – MaestroGlanz Jun 30 '16 at 19:02
  • @barbarabeeton: The page label will only hold the number of the page as reported by \thepage, which is reset by \pagenumbering, but it will not give the total number of pages –  Jun 30 '16 at 19:04
  • @barbarabeeton doesnt seem to be bad, but it would mean, that I have to process three cycles, since I already have two. And I would have to take care of each circle not to do any error calculations. – MaestroGlanz Jun 30 '16 at 19:04
  • @MaestroGlanz: It depends on your position on the page. Use the \NewTotalDocumentCounter feature of xassoccnt, so you know the total number of pages at the begin of the document (needs two runs!) –  Jun 30 '16 at 19:04
  • @ChristianHupfer Ah, of course, floating stuff. It will be printed after everything to the aux-file, so this should be safe. I just used another example as a MWE, that is easy to understand. – MaestroGlanz Jun 30 '16 at 19:07
6

zref's abspage module provides access to an "absolute page number" property. Together with the lastpage module, you can extract the last absolute page number using \zref[abspage]{LastPage}. Here is a minimal example:

enter image description here

\documentclass{article}

\usepackage[paper=a5paper]{geometry}% Just for this example
\usepackage[abspage,user,lastpage]{zref}

\begin{document}
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage

\pagenumbering{gobble}

Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage

\pagenumbering{arabic}

Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage
\newpage
Seite \thepage

Last page: \zref[abspage]{LastPage}

\end{document}

The interface to use \zref[<prop>]{<label>} requires the user module. Any specific page's absolute page number (other than LastPage, say) can be obtained using a \zlabel{<label>} and subsequent \zref[abspage]{<label>}.

Werner
  • 603,163
4

The memoir class provides two macros, namely \lastpage and \lastsheet which will print the page number of the last page and the number of sheets of paper required for printing the document single sided. To date I have not heard of any problems with these. They can be used like this:

\documentclass[a4paper,...]{memoir}
...
\begin{document}
...
The last page is number \lastpage\ and requires \lastsheet\ of A4
paper for printing single sided.
\end{document} 

There is also a \thesheetsequence macro that prints the current number of sheets like \thepage which prints the current page number.

Peter Wilson
  • 28,066
2

Naturally there is a counter. \c@page (which is defined as \count0) and you can access its value:

\documentclass[a5paper, 11pt]{scrbook}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}

\begin{document}\makeatletter
Seite \the\value{page}--\the\c@page --\the\count0
\newpage
Seite \the\value{page}--\the\c@page --\the\count0
\end{document}

But you must be careful as its value can be wrong due to the asynchronous typesetting. After \newpage as in your example everything is fine, but in normal paragraphs it can be one off.

Ulrike Fischer
  • 327,261
  • This doesnt work. It is reset on a change of the pagenumbering. – MaestroGlanz Jul 01 '16 at 09:36
  • 1
    @MaestroGlanz: Well it is the counter underlying \thepage. If you want the absolute page number, zref-abspage offers the counter \c@abspage, and the package count1to \count1. – Ulrike Fischer Jul 02 '16 at 16:42