I have a small problem that I can not solve, and I do not understand the cause.
\thepage counter is sometimes one page earlier than the actual page where it is invoked
I think it is because the text is formatted before being added to the output.
Here is a Minimal Working Example.
\documentclass[9pt]{extbook}
\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}
\usepackage[french]{babel}
\usepackage{geometry}
\geometry{
right=1.2cm,
left=1.8cm,
top=1.8cm,
bottom=1cm,
paperwidth=110mm,
paperheight=178mm
}
\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}
\begin{document}
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
~
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\thepage
I'm an amazing poem
\endgroup
\end{document}
You can see that at the top of pages 2, 3 and 4, \thepage shows 1, 2 and 3, instead of 2, 3 and 4.
It's even worse when you use \\ instead of obeylines :
\documentclass[9pt]{extbook}
\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}
\usepackage[french]{babel}
\usepackage{geometry}
\geometry{
right=1.2cm,
left=1.8cm,
top=1.8cm,
bottom=1cm,
paperwidth=110mm,
paperheight=178mm
}
\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}
\begin{document}
\titre{}
\begingroup
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\endgroup
\titre{}
\begingroup
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\titre{}
\begingroup
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\endgroup
\titre{}
\begingroup
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
~\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\thepage\
I'm an amazing poem\
\endgroup
\end{document}
I thank in advance the intelligent people who could explain me the reason of this problem.
Great thanks!
EDIT
Even with \label and \pageref, the problem is still here.
For example:
\documentclass[9pt]{extbook}
%pdfLaTeX
\usepackage[TS1,T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ebgaramond}
\usepackage[french]{babel}
\usepackage{geometry}
\geometry{
right=1.2cm,
left=1.8cm,
top=1.8cm,
bottom=1cm,
paperwidth=110mm,
paperheight=178mm
}
\newcommand\titre{%
\vspace{1em}%
{\centering%
AMAZING POEM\par%
}%
}
\newcounter{nbrefpage}
\setcounter{nbrefpage}{0}
\newcommand\afficherpage{%
\stepcounter{nbrefpage}%
\def\tmpnbrefpage{\arabic{nbrefpage}testpage}%
\label{\tmpnbrefpage}%
\pageref{\tmpnbrefpage}%
}
\begin{document}
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
\titre{}
\begingroup
\obeylines
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
~
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\afficherpage
I'm an amazing poem
\endgroup
\end{document}
And the result (after 2 compilations of course) is the same:
In the .aux file, we see:
\newlabel{16testpage}{{}{1}}
instead of
\newlabel{16testpage}{{}{2}}


\thepagewhere the page is known, eg in head and foot of a page style. use\labelad\pagerefto refer to page in the body – David Carlisle Jul 30 '22 at 13:44\labeland\pageref, I have the same problem. I don't understand why. – Pierre Dufays Jul 30 '22 at 13:46