1

Having a document with different page numberings: roman (i,ii,etc.) and arabic (1,2,etc.) and the chapters setting the \setcounter{page}{1}.

How to get the "real/actual" page number despite the numbering?

In the following example we have two roman-numerated pages, and two chapters of 2 pages each. This are 6 "real/actual" pages.

i  -> 1
ii -> 2
1  -> 3
2  -> 4
1  -> 5
2  -> 6

Update MWE as requested:

\documentclass{book}

\usepackage{lipsum}
\let\oldchapter\chapter
\renewcommand{\chapter}[1]{
    \oldchapter{#1}
    \par\noindent Page:~ \thepage
    \par\noindent Absolute:~ ??? % <- here I need the real/actual page
}

\begin{document}

\frontmatter
\chapter{Primus}
\par\lipsum
\chapter{Secundus}
\par\lipsum
\mainmatter
\chapter{First}
\par\lipsum
\chapter{Second}
\par\lipsum

\end{document}
15
  • 1
    depends where you want to use the absolute page number so provide a complete example. Nov 1, 2023 at 13:25
  • 1
    In OpTeX, there is the counter \gpageno which counts the absolute page number.
    – wipet
    Nov 1, 2023 at 13:26
  • @wipet we have a counter too, but due to the asynchronous page breaking you still need to decide if you can use it directly (in the header) or if you need to go through an auxiliary file and a reference (in the text body). Nov 1, 2023 at 13:30
  • Is this just academic curiosity or you have an application in mind?
    – egreg
    Nov 1, 2023 at 13:35
  • 1
    Possible duplicate of tex.stackexchange.com/questions/659781/… Nov 1, 2023 at 13:40

3 Answers 3

2

Be aware that page counters (both \thepage and totalpages) are not always reliable in the text body due to the asynchronous page breaking. Directly behind \chapter it should work.

\documentclass{book}

\usepackage{lipsum}
\let\oldchapter\chapter
\renewcommand{\chapter}[1]{
    \oldchapter{#1}
    \par\noindent Page:~ \thepage
    \par\noindent Absolute:~ \inteval{\value{totalpages}+1} % <- here I need the real/actual page
}

\begin{document}

\frontmatter
\chapter{Primus}
\par\lipsum
\chapter{Secundus}
\par\lipsum
\mainmatter
\chapter{First}
\par\lipsum
\chapter{Second}
\par\lipsum

\end{document}

2

In OpTeX, we can use \gpageno. Be aware that page counters (both \pageno and \gpageno) are not always reliable in the text body due to the asynchronous page breaking. Directly behind \chap it should work.

\def\lipsum{\lorem[1-12]}
\let\printchap_ori=\_printchap
\def\_printchap#1{%
   \printchap_ori{#1}
   \par\noindent Page:~\folio
   \par\noindent Absolute:~\the\numexpr\gpageno+1\relax
   \par
}

\pageno=-1
\nonum\chap Primus
\lipsum
\nonum\chap Secundus
\lipsum
\vfil\break \pageno=1
\chap First
\lipsum
\chap Second
\lipsum

\bye
0

You want to add hooks to \mainmatter and to \chapter to write annotations in an auxiliary file.

\documentclass{book}
\usepackage[a6paper]{geometry} % just to get smaller pictures
\usepackage{lipsum}

\newwrite\qpdfcommands
\immediate\openout\qpdfcommands=\jobname-qpdf.commands

\newcommand{\getlastpagenumber}{%
  \inteval{\value{page}\ifodd\value{page}+1\fi}%
}

\AddToHook{cmd/mainmatter/before}{%
  \xdef\pagesinthefrontmatter{\getlastpagenumber}
}
\AddToHook{cmd/chapter/before}{%
  \ifnum\value{chapter}>0
    \immediate\write\qpdfcommands{%
      qpdf --empty --pages \jobname.pdf
      \inteval{\firstpageofprevious+\pagesinthefrontmatter}-%
      \inteval{\getlastpagenumber+\pagesinthefrontmatter}
      -- \jobname-\arabic{chapter}.pdf%
    }%
    \xdef\firstpageofprevious{\inteval{\getlastpagenumber+1}}%
  \else
    \gdef\firstpageofprevious{1}%
  \fi
}
\AtEndDocument{%
  \immediate\write\qpdfcommands{%
    qpdf --empty --pages \jobname.pdf
    \inteval{\firstpageofprevious+\pagesinthefrontmatter}-z
    -- \jobname-\arabic{chapter}.pdf%
  }%
  \immediate\closeout\qpdfcommands
}

\begin{document}

\huge

\frontmatter

This is page \thepage

\clearpage

This is page \thepage

\clearpage

This is page \thepage

\mainmatter

\chapter{First}

\lipsum[1-10]

\chapter{Second}

\lipsum[11-30]

\chapter{Third}

\lipsum[31-60]

\end{document}

Try it and you see that the written file will contain

qpdf --empty --pages qpex.pdf 5-28 -- qpex-1.pdf
qpdf --empty --pages qpex.pdf 29-70 -- qpex-2.pdf
qpdf --empty --pages qpex.pdf 71-z -- qpex-3.pdf

I leave as an exercise how to cope with \backmatter.

3
  • Your example is interesting. But not exactly what I am searching for. Think of it as the following task: Extract the single chapters/sections from the whole pdf. I've done this with different approaches. One of them marks the start and end of the chapter and writes a qpdf command with the pages to a file. But this works only if there is only one page counter/numbering. As soon as there are more numberings (either in frontmatter with roman-numbering or by setting the page nr in some parts) I need the absolute pages.
    – wolfrevo
    Nov 1, 2023 at 17:15
  • @wolfrevo Look at the present version
    – egreg
    Nov 1, 2023 at 17:39
  • FYI: Workaround to use "relative" pages in a pagelabel with qpdf: github.com/qpdf/qpdf/issues/1060#issuecomment-1793765046
    – wolfrevo
    Nov 7, 2023 at 21:47

You must log in to answer this question.

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