page-numbering – How to Get the Physical Absolute Page Number in LaTeX

page-numbering

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}

Best Answer

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}