I need to obtain the logical number of a certain page by its absolute page number.
For example, in a document with 9 pages whose logical page numbers like this: I II 1 2 3 4 A B C, At the the part of the fifth page, using of \getlogicalpn{7} will get the logical number of the seventh page -- A.
My solution is: construct a keyval list(\pagelist in codes) of all pages first. Key denotes to the absolute page number, value is the related logical page number. Then consult \pagelist by a key to get its value.
My try fails. Please see the comments in the following codes. Any better ideas about this?
Codes:
\documentclass{article}
\usepackage{geometry,fancyhdr}
\pagestyle{fancy}\fancyhf{}
\ExplSyntaxOn
% obtain the page-list
\prop_new:N \pagelist
\AddToHook{shipout/after}{
\prop_gput:Nxx \pagelist {\the\ReadonlyShipoutCounter} {\thepage}
}
% defination of \getlogicalpn
\NewExpandableDocumentCommand{\getlogicalpn}{m}{
\prop_get:NnN \pagelist {#1} _temp
_temp
}
\ExplSyntaxOff
\lhead{absPage: \the\numexpr\ReadonlyShipoutCounter+1\relax}
\chead{Logical Page: \thepage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\tableofcontents
\pagenumbering{Roman}%-----------------
\section{S1}
first page\par
\clearpage
\section{S2}
sencond page\par
\clearpage
\pagenumbering{arabic}%----------------
\section{S3}
third page\par
\clearpage
\section{S4}
fourth page\par
\clearpage
\section{S5}
fifth page\par
% Test if the logical page number can be aquired in advance.
\ExplSyntaxOn
pagelist~is:~|\meaning\pagelist|\par
\prop_get:NnNTF \pagelist{7} _temp {_temp}{no~such~key}\par
\ExplSyntaxOff
%Typeset of "A" is wanted, but this causes codes fail to compile.
\getlogicalpn{7}
% Test if the macro is expandable.
% Typeset of \rule{1in}{5pt} is wanted.
% It fails. How to make it work?
\rule{\getlogicalpn{3} in}{5pt}
\clearpage
\section{S6}
sixth page\par
\clearpage
\pagenumbering{Alph}%------------------
\section{S7}
seventh page\par
\clearpage
\section{S8}
eighth page\par
\clearpage
\section{S9}
nineth page\par
\end{document}
\labeland\pageref? – Werner Oct 20 '22 at 06:57\getlogicalpnbeing used anywhere for any page, and it should be expandable. – lyl Oct 20 '22 at 07:04hard code the values (I, II etc.)? – lyl Oct 21 '22 at 03:12