Where does \thepage kick in?

Your mistake...

Here's an example having 24 pages (you can increase this quite a bit) with the same result - \thepage keeps printing 1:

enter image description here

\documentclass{article}

\usepackage[nopar]{lipsum}

\def\x{\lipsum[1]~\thepage~}
\def\xx{\x\x\x\x\x\x\x\x\x\x}

\begin{document}
\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx
\end{document}

Why is this? It's because the first (only) sign of a paragraph break is at the end of the document (actually, issued during \end{document} as a result of \clearpage). TeX considers the above 100 \lipsum[1]~\thepage~ constructions as a single paragraph, and there are only certain locations where TeX initiates the page builder (see section 27.2 Activating the page builder of TeX by Topic, or p 110 of The TeX Book):

The page builder comes into play in the following circumstances.

  • Around paragraphs: after the \everypar tokens have been inserted, and after the paragraph has been added to the vertical list. See the end of this chapter for an example.
  • Around display formulas: after the \everydisplay tokens have been inserted, and after the display has been added to the list.
  • After \par commands, boxes, insertions, and explicit penalties in vertical mode.
  • After an output routine has ended.

How do you get an appropriate page number? Use \labels, and specifically, \pageref:

enter image description here

\documentclass{article}

\usepackage[nopar]{lipsum}

\def\x{\lipsum[1]~\thepage~}
\def\xx{\x\x\x\x\x\x\x\x\x\x}

\begin{document}
\xx\xx\xx\xx\xx\xx\xx\xx\xx\xx%
\label{mylabel}\pageref{mylabel}
\end{document}

This works because \labels are designed to function around the page builder as it requires the page reference capability.


the page number is incremented by the output routine when it is adding headers and footers after having decided where to make a page break. Note that this happens independently of the execution of the macros that are making up the vertical galley that will eventually be split into pages.

The paragraph builder considers a whole paragraph, typesetting into one long horizontal list before splitting that list into lines, which means there is no way of testing in a macro directly which line of a paragraph you are on, as al macros are executed before linebreaking is considered. Similarly the page breaker cuts in and considers splitting off a chunk of the constructed vertical list by which time any macros will have been expanded long before. If you have short paragraphs and not much stretch the page breaker probably only ever makes one page and so thepage is only "wrong" for the last few lines of a paragraph held over from the previous page, but in general it can be any number pf pages. If you have enough text to make a 10-page paragraph then all macros within that paragraph will have been expanded before any line is constructed (and so before any page is shipped out) so \thepage would print 1 even if it is printed on page 10, like:

\documentclass{article}

\def\a{one two three four five six seven }
\def\b{\a red blue green\a\a pink yellow }
\def\c{\b\b\a\a\b\b\b aples oranges pears }
\def\d{\c\c\a\a\b\b\b\b\c\c\c\c\c}

\begin{document}
\d\d\d\d\d\d\fbox{\thepage}
\end{document}

enter image description here