## LaTeX forum ⇒ Page Layout ⇒ How to achieve multiple “LastPage” in one document? Topic is solved

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
casperyc
Posts: 50
Joined: Thu Oct 15, 2009 11:23 pm

### How to achieve multiple “LastPage” in one document?

\documentclass{article} \usepackage{fancyhdr}\usepackage{lastpage}\pagestyle{fancy}\fancyhead{}\fancyfoot[C]{Page \thepage\ of \pageref{LastPage}} \usepackage{kantlipsum} \begin{document} \kant \setcounter{page}{1}\kant[1-14] \setcounter{page}{1}\kant[1-22] \setcounter{page}{1}\kant[1-5] \end{document}

In this document, say I want to restart the page counter at certain points, but I want it to recognise the LastPage before it was reset. For example,

1 of 22 of 21 of 72 of 73 of 74 of 75 of 76 of 77 of 71 of 42 of 43 of 44 of 4

There is no need for total number of pages in my case.

Thanks.

Stefan Kottwitz
Posts: 9549
Joined: Mon Mar 10, 2008 9:44 pm
Hi,

here is an application of egreg's code for a similar question:

\documentclass{article} \usepackage{fancyhdr}\usepackage{atenddvi}\usepackage{lastpage}\pagestyle{fancy}\fancyhead{}\fancyfoot[C]{\stepcounter{pageaux}Page \thepageaux\ of \ref{\currentauxref}} \usepackage{kantlipsum} \usepackage[user]{zref} \newcounter{pageaux}\def\currentauxref{PAGEAUX1}\makeatletter\newcommand{\resetpageaux}{%  \clearpage  \edef\@currentlabel{\thepageaux}\label{\currentauxref}%  \xdef\currentauxref{PAGEAUX\thepage}%  \setcounter{pageaux}{0}}\AtEndDvi{\edef\@currentlabel{\thepageaux}\label{\currentauxref}}\makeatother \begin{document} \kant\resetpageaux \kant[1-14] \resetpageaux  \kant[1-22]\resetpageaux \kant[1-5]\end{document}

Stefan

casperyc
Posts: 50
Joined: Thu Oct 15, 2009 11:23 pm
Thanks. It worked.

But not sure what the "problem" is, that I had to compile pdflate THREE times, to get the last LastPage correctly. If I only compile it twice, the last LastPage would only be ??.

Stefan Kottwitz
Posts: 9549
Joined: Mon Mar 10, 2008 9:44 pm
Storing data in the aux file and using it, requires another LaTeX run to read it in. I didn't analyze why one needs 3 runs here, but I'm aware that sometimes even more than 2 runs are required until stuff is balanced, such as auto-adjusting lengths of TOC numbering entries.

Stefan

casperyc
Posts: 50
Joined: Thu Oct 15, 2009 11:23 pm
hmmmmmm...

I have put it back into my "big" project and it still couldn't identify the currect last page for each of the input tex file. I have been tweaking the codes a bit with the original page number counter. And tring to identify where/what the problem is.

If in each of the tex files, I already have
\setcounter{page}{1}

Do I need to change the couters to pageaux? My understanding is this pageaux is used to give and store the LastPage, so I don't need to change anything. Or should I comment out all the
\setcounter{page}{1}

is those tex files (as it was done in the \resetpageaux ???)

At the moment, I have even tried with up to 6 runs of pdflate. It was not correct...

Stefan Kottwitz
Posts: 9549
Joined: Mon Mar 10, 2008 9:44 pm
Remove that \setcounter{page}{1} as it's done in the macro.

Stefan

casperyc
Posts: 50
Joined: Thu Oct 15, 2009 11:23 pm
I should have tried that to start with ... as simple as that ...

All sorted.

Thanks.