如何获取物理(绝对)页码

如何获取物理(绝对)页码

我有一篇九页的文章,其中包含三种页码样式—— RomanarabicAlph。页面布局及其逻辑编号为:I II 1 2 3 4 A B C

我想从宏(或计数器)获取给定页面的物理(绝对)页码。例如,它应该是7具有逻辑编号的页面A,以及5具有逻辑编号的页面3

我尝试使用 LaTeX 宏\ReadonlyShipoutCounter,但该宏没有给出正确的数字。

有人能帮帮我吗?最好不要使用其他软件包。

例子:

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

编辑:

根据 Ulrike Fischer 给出的解决方案,我还有另一个要求:
如何制作\abspagenumber带有水印的作品?

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

答案1

使用 LaTeX 内核提供的新钩子工具来计算当前绝对页码并添加水印:

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

答案2

您只需要在计数器上加一即可:

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

答案3

仍然存在,但不再是默认。

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

相关内容