7

I have an article of nine pages which include three kinds of page number styles – Roman, arabic and Alph. The layout of the pages and their logical numbers are: I II 1 2 3 4 A B C.

I want to get the physical (absolute) page number of a given page from a macro (or counter). For example, it should be 7 for the page with logical number A, and 5 for the page with logical number 3.

I tried with the LaTeX macro \ReadonlyShipoutCounter, but this macro did not give the right number.

Anyone can help me? Preferably without using other packages.

Example:

\documentclass{article}
\usepackage{geometry,fancyhdr}
\geometry{showframe}
\pagestyle{fancy}

% My Try: (not succeessful)
\newcommand\abspagenumber{\the\ReadonlyShipoutCounter}

% My Intent:
\newcommand\abspage{...}%output the absolute page number of a certain page in article

\rhead{\thepage}
\chead{Currrent Physical Page Number is: \abspagenumber}
%\lhead{Currrent Physical Page Number is: \abspage}
\begin{document}
\pagenumbering{Roman}
first page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
sencond page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
\pagenumbering{arabic}
third page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
fourth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
fifth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
sixth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
\pagenumbering{Alph}
seventh page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
eighth page\par current absolute page number is: \abspagenumber\par
% The absolute page number of this page is: \abspage.
\clearpage
nineth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\end{document}

Edit:

Base on the solution given by Ulrike Fischer I have another request:
How can I make \abspagenumber work in a watermark?

\documentclass{article}
\usepackage{geometry,fancyhdr,eso-pic}
\geometry{showframe}
\pagestyle{fancy}


\newcommand\abspagenumber{\inteval{\ReadonlyShipoutCounter+1}}

\rhead{\thepage}
\chead{Currrent Physical Page Number is: \abspagenumber}

\begin{document}
\pagenumbering{Roman}
first page\par current absolute page number is: \abspagenumber
\AddToShipoutPictureBG{\AtPageCenter{\abspagenumber}}

\clearpage
sencond page\par current absolute page number is: \abspagenumber

\clearpage
\pagenumbering{arabic}
third page\par current absolute page number is: \abspagenumber

\clearpage
fourth page\par current absolute page number is: \abspagenumber

\clearpage
fifth page\par current absolute page number is: \abspagenumber
\AddToShipoutPictureBG{\AtPageCenter{\abspagenumber}}
\clearpage
sixth page\par current absolute page number is: \abspagenumber
\end{document}
2
  • If you load the hyperref packge, you can use the count \Hy@pagecounter (all counters are counts, but not all counts are counters). Sep 28, 2022 at 4:11
  • @John Kormylo I tried \the\Hy@pagecounter in each page of my article, and they all typeset 0. As I don't fine related description about the count \Hy@pagecounter in the manuel, would you provide with an example or answer to illustrate how to use it?
    – lyl
    Sep 28, 2022 at 9:04

3 Answers 3

4

Using the new hook facility provided by the LaTeX kernel to calculate the current abs. page number and to add the watermark:

\documentclass{article}

\usepackage{geometry,fancyhdr}
\geometry{showframe}
\pagestyle{fancy}

\AtBeginDocument{\xdef\currentPageNumber{1}}
\AddToHook{shipout/after}{\xdef\currentPageNumber{\inteval{\ReadonlyShipoutCounter+1}}}

\rhead{\thepage}
\chead{Current Physical Page Number is: \currentPageNumber}

\begin{document}
\pagenumbering{Roman}
first page\par current absolute page number is: \currentPageNumber
\AddToHookNext{shipout/background}{% upper left page corner coordinate is (0,0)
  \unitlength=\paperheight
  \put(0,-0.5){\makebox[\paperwidth][c]{\currentPageNumber}}
}

\clearpage
sencond page\par current absolute page number is: \currentPageNumber

\clearpage
\pagenumbering{arabic}
third page\par current absolute page number is: \currentPageNumber

\clearpage
fourth page\par current absolute page number is: \currentPageNumber

\clearpage
fifth page\par current absolute page number is: \currentPageNumber
\AddToHookNext{shipout/background}{
  \unitlength=\paperheight
  \put(0,-0.5){\makebox[\paperwidth][c]{\currentPageNumber}}
}
\clearpage
sixth page\par current absolute page number is: \currentPageNumber
\end{document}
10

You only need to add one to the counter:

\documentclass{article}
\usepackage{geometry,fancyhdr}
\geometry{showframe}
\pagestyle{fancy}


\newcommand\abspagenumber{\inteval{\ReadonlyShipoutCounter+1}}

\rhead{\thepage}
\chead{Currrent Physical Page Number is: \abspagenumber}

\begin{document}
\pagenumbering{Roman}
first page\par current absolute page number is: \abspagenumber

\clearpage
sencond page\par current absolute page number is: \abspagenumber

\clearpage
\pagenumbering{arabic}
third page\par current absolute page number is: \abspagenumber

\clearpage
fourth page\par current absolute page number is: \abspagenumber

\clearpage
fifth page\par current absolute page number is: \abspagenumber

\clearpage
sixth page\par current absolute page number is: \abspagenumber

\clearpage
\pagenumbering{Alph}
seventh page\par current absolute page number is: \abspagenumber
\clearpage
eighth page\par current absolute page number is: \abspagenumber\par

\clearpage
nineth page\par current absolute page number is: \abspagenumber
\end{document}
17
  • Is there a similar command readily available for the total page count (last page shipout #) of a document?
    – AlexG
    Sep 28, 2022 at 8:35
  • 1
    @AlexG \PreviousTotalPages, see ltshipout-doc.pdf Sep 28, 2022 at 8:37
  • 1
    @AlexG we don't reserve the good stuff only for us ;-) Sep 28, 2022 at 8:49
  • 1
    @lyl You may do \newcommand{\abspagenumber}{\the\numexpr\ReadonlyShipoutCounter+1\relax}, but updating your TeX system is much better.
    – egreg
    Sep 28, 2022 at 9:14
  • 1
    @lyl I must correct myself. Since \ReadonlyShipoutCounter is incremented only after the page has been shipped out, its value should be the same in both the page body and during shipout, hence starting with zero for both.
    – AlexG
    Sep 28, 2022 at 12:51
4

Still there, but no longer default.

\documentclass{article}
\usepackage{geometry,fancyhdr}
\geometry{showframe}
\pagestyle{fancy}
\usepackage[hypertexnames=false]{hyperref}

\makeatletter
\Hy@pagecounter=1
\newcommand\abspagenumber{\the\Hy@pagecounter}
\makeatother

\rhead{\thepage}
\chead{Currrent Physical Page Number is: \abspagenumber}
%\lhead{Currrent Physical Page Number is: \abspage}
\begin{document}
\pagenumbering{Roman}
first page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
sencond page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
\pagenumbering{arabic}
third page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
fourth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
fifth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
sixth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
\pagenumbering{Alph}
seventh page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\clearpage
eighth page\par current absolute page number is: \abspagenumber\par
% The absolute page number of this page is: \abspage.
\clearpage
nineth page\par current absolute page number is: \abspagenumber
% The absolute page number of this page is: \abspage.
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .