LaTeX forum ⇒ Page LayoutHow 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?

Postby casperyc » Sun Jan 12, 2020 12:42 am

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage{fancyhdr}
  4. \usepackage{lastpage}
  5. \pagestyle{fancy}
  6. \fancyhead{}
  7. \fancyfoot[C]{Page \thepage\ of \pageref{LastPage}}
  8.  
  9. \usepackage{kantlipsum}
  10.  
  11. \begin{document}
  12.  
  13. \kant
  14.  
  15. \setcounter{page}{1}
  16. \kant[1-14]
  17.  
  18. \setcounter{page}{1}
  19. \kant[1-22]
  20.  
  21. \setcounter{page}{1}
  22. \kant[1-5]
  23.  
  24. \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,


Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. 1 of 2
  2. 2 of 2
  3. 1 of 7
  4. 2 of 7
  5. 3 of 7
  6. 4 of 7
  7. 5 of 7
  8. 6 of 7
  9. 7 of 7
  10. 1 of 4
  11. 2 of 4
  12. 3 of 4
  13. 4 of 4


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

Thanks.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9550
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Jan 12, 2020 1:43 am

Hi,

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

Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \documentclass{article}
  2.  
  3. \usepackage{fancyhdr}
  4. \usepackage{atenddvi}
  5. \usepackage{lastpage}
  6. \pagestyle{fancy}
  7. \fancyhead{}
  8. \fancyfoot[C]{\stepcounter{pageaux}Page \thepageaux\ of \ref{\currentauxref}}
  9.  
  10. \usepackage{kantlipsum}
  11.  
  12. \usepackage[user]{zref}
  13.  
  14. \newcounter{pageaux}
  15. \def\currentauxref{PAGEAUX1}
  16. \makeatletter
  17. \newcommand{\resetpageaux}{%
  18. \clearpage
  19. \edef\@currentlabel{\thepageaux}\label{\currentauxref}%
  20. \xdef\currentauxref{PAGEAUX\thepage}%
  21. \setcounter{pageaux}{0}}
  22. \AtEndDvi{\edef\@currentlabel{\thepageaux}\label{\currentauxref}}
  23. \makeatother
  24.  
  25. \begin{document}
  26.  
  27. \kant
  28. \resetpageaux
  29.  
  30. \kant[1-14]
  31.  
  32. \resetpageaux
  33.  
  34. \kant[1-22]
  35. \resetpageaux
  36.  
  37. \kant[1-5]
  38. \end{document}


Stefan
LaTeX.org admin

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

Postby casperyc » Sun Jan 12, 2020 2:24 am

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 ??.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9550
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Jan 12, 2020 2:32 am

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
LaTeX.org admin

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

Postby casperyc » Sun Jan 12, 2020 2:59 am

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
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \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
Code: [Select all] [Expand/Collapse] [Download] ({untitled.tex})
  1. \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...

User avatar
Stefan Kottwitz
Site Admin
Posts: 9550
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Sun Jan 12, 2020 3:03 am

Remove that \setcounter{page}{1} as it's done in the macro.

Stefan
LaTeX.org admin

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

Postby casperyc » Sun Jan 12, 2020 3:21 am

I should have tried that to start with ... as simple as that ...

All sorted.

Thanks.


Return to “Page Layout”

Who is online

Users browsing this forum: No registered users and 3 guests