Page LayoutHow to achieve multiple “LastPage” in one document?

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

How to achieve multiple “LastPage” in one document?

Post by casperyc »

Code: Select all

\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,

Code: Select all

1 of 2
2 of 2
1 of 7
2 of 7
3 of 7
4 of 7
5 of 7
6 of 7
7 of 7
1 of 4
2 of 4
3 of 4
4 of 4
There is no need for total number of pages in my case.

Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

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

How to achieve multiple “LastPage” in one document?

Post by Stefan Kottwitz »

Hi,

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

Code: Select all

\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
LaTeX.org admin
casperyc
Posts: 50
Joined: Thu Oct 15, 2009 11:23 pm

How to achieve multiple “LastPage” in one document?

Post by casperyc »

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: 10320
Joined: Mon Mar 10, 2008 9:44 pm

How to achieve multiple “LastPage” in one document?

Post by Stefan Kottwitz »

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

How to achieve multiple “LastPage” in one document?

Post by casperyc »

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

\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

\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: 10320
Joined: Mon Mar 10, 2008 9:44 pm

How to achieve multiple “LastPage” in one document?

Post by Stefan Kottwitz »

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

How to achieve multiple “LastPage” in one document?

Post by casperyc »

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

All sorted.

Thanks.
Post Reply