[Tex/LaTex] Get page numbering & page count of a given style

memoirpage-numbering

Given a set of page styles, for example plain and butter, in the memoir class how can get the page number and count for that given style?

This seems like something that would already have been solved, but I cannot seem to find a solution that someone else has already worked out.

A simple example:

\documentclass{memoir}
\copypagestyle{butter}{plain}
\begin{document}
   \pagestyle{plain}
   Page \thepage{} of \thelastpage. % should be "1 of 2" but is "1 of 1"
   \newpage
   Page \thepage{} of \thelastpage. % should be "2 of 2" but is "1 of 1"
   \newpage
   \pagestyle{butter}
   \setcounter{page}{1}             % reset page to 1
   Page \thepage{} of \thelastpage. % correctly "1 of 1"
\end{document}

I gather the lastpage counter uses the last page of the document, being 1 in this case because we reset it.

How could one go about getting the page count of plain pages?

In the degenerate case where one wanted to ignore the butter pages in the total page count, and there are two styles, is there a simple way one could one get the count of plain pages? (aside perhaps from putting the butter pages before the plain ones)

Best Answer

You could make use of the method of the lastpage package and write the current page number to the .aux file.

\documentclass{memoir}
\copypagestyle{butter}{plain}

\makeatletter
% first arg is label for current page number.
\def\savepage#1{%
        \immediate\write\@auxout{\string
        \newlabel{#1}{{\thepage}{}}}%
}

\makeatother

\begin{document}

   \pagestyle{plain}
   % rather than \thelastpage reference via \ref
   Page \thepage{} of \ref{lastplainpage} (1st plain page)
   \newpage

   Page \thepage{} of \ref{lastplainpage} (2nd plain page)
   % save current value of page counter as "lastplainpage"
   \savepage{lastplainpage}
   \newpage

   \pagestyle{butter}
   \pagenumbering{arabic}% reset page to 1
   Page \thepage{} of \ref{lastbutterpage} (1st butter page)
   \newpage

   Page \thepage{} of \ref{lastbutterpage} (2nd butter page)
   % save current value of page counter as "lastbutterpage"
   \savepage{lastbutterpage}

\end{document}