When the .aux file doesn't exist, \@abspage@last is set to 1073741823 and of course you don't want to produce that many pages.
So, let's try and “fix” by telling LaTeX that if \@abspage@last equals \maxdimen we set \lastpagenumber to, say, zero.
\documentclass{article}
\usepackage{pgffor}
\makeatletter
\newcommand{\lastpagenumber}{%
\ifnum@abspage@last=\maxdimen
\expandafter 0%
\else
\expandafter@abspage@last
\fi
}
\makeatother
\begin{document}
\foreach \i in {1,...,\lastpagenumber}{(\i)}
\clearpage
\mbox{LAST}
\end{document}
Now, if the .aux file doesn't exist (or, for some reason, it doesn't set a value for \@abspage@last), you get two pages. The first page has “(1)(0)” (because of how \foreach works) and the second one has “LAST”.
OK, now the .aux file exists and contains
\gdef \@abspage@last{2}
We run again LaTeX to get “(1)(2)” in the first page and “LAST” in the second one.
Perhaps even better, as it avoids one expansion step when using \lastpagenumber:
\documentclass{article}
\usepackage{pgffor}
\makeatletter
\newcommand{\lastpagenumber}{}
\AtBeginDocument{%
\ifnum@abspage@last=\maxdimen
\def\lastpagenumber{0}%
\else
\let\lastpagenumber@abspage@last
\fi
}
\makeatother
\begin{document}
\foreach \i in {1,...,\lastpagenumber}{(\i)}
\clearpage
\mbox{LAST}
\end{document}
I do \newcommand{\lastpagenumber}{} for safety: if some package you load defines \lastpagenumber, you get an error and you know you cannot use this command.
Next, after the .aux file is read in (provided it exists), we check whether \@abspage@last is \maxdimen; in this case we define \lastpagenumber to expand to 0, otherwise we make it equal to \@abspage@last.
\expandafterdoes here, can you explain it to me, thanks! – ljguo Sep 26 '22 at 09:17\elseor\fi. That's a common trick in TeX, see https://tex.stackexchange.com/q/107753/4427 – egreg Sep 26 '22 at 09:20\def\lastpagenumber{0}, So the value0is not important, it can be any integer number? (2) Should it be\global\def\lastpagenumber{0}, as it is in the scope ofAtBeginDocument? (3) Doesavoids one expansionrefer to\let\lastpagenumber\@abspage@last? (4) Is it possible to `\expandafter\let\lastpage@abspage@last \fi', if so, is this a good idea? – lyl Sep 26 '22 at 10:10\AtBeginDocumentis executed it's not in any scope. (3) Yes. (4) That's completely wrong code. – egreg Sep 26 '22 at 10:13\AtBeginDocument{...} into a macro(named \getlastpagenumber, for exmple, should\global\defbe used? (4) I mean similar like `\expandafter (\let\lastpage@abspage@last) \fi' (I know this is wrong syntax), I use it to express my idea. Is it possible? – lyl Sep 26 '22 at 10:20\IfFileExists{\jobname .aux}{\makeatletter\let\lastpagenumber\@abspage@last\makeatother}{}and expected it to work, but it doesn't. Why? – Niranjan Sep 26 '22 at 11:22.auxfile doesn't guarantee that\@abspage@lasthas a sensible value. – egreg Sep 26 '22 at 11:55.auxapproach would be bad in any case. The problem that I was facing was because of the strange working of\IfFileExistswhich I have reported: https://github.com/latex3/latex3/issues/1132 – Niranjan Sep 26 '22 at 11:59\fifirst, then expand\let\lastpage\@abspage@last? – lyl Sep 27 '22 at 00:24