9

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)

2 Answers 2

5

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}
2
  • Great answer. Thanks for editing to make it generic. Oct 10, 2012 at 1:25
  • No problem, happy to help.
    – Scott H.
    Oct 10, 2012 at 1:38
4

Here is a more automated modification to Scott H's answer that maintains a page style-dependent page counter.

enter image description here

\documentclass{memoir}% http://ctan.org/pkg/memoir
\usepackage[paperheight=12\baselineskip,paperwidth=100pt]{geometry}% http://ctan.org/pkg/geometry
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\makeatletter
\EveryShipout{\stepcounter{ps@\@cur@pagestyle @lastpage}}% Increment 
\newcounter{ps@savepage}% To save the page style counter
\def\savepage#1{%
  \setcounter{ps@savepage}{\value{ps@#1@lastpage}}%
  \immediate\write\@auxout{\string
  \newlabel{ps@#1@lastpage}{{\theps@savepage}{}}}%
}
% Taken from latex.ltx/ltpage.dtx
\def\pagestyle#1{%
  \@ifundefined{ps@#1}%
    \undefinedpagestyle
    {\@nameuse{ps@#1}\gdef\@cur@pagestyle{#1}%
     \@ifundefined{c@#1@lastpage}{\newcounter{ps@#1@lastpage}\AtEndDocument{\savepage{#1}}}{}}}
\def\thispagestyle#1{%
  \@ifundefined{ps@#1}%
    \undefinedpagestyle
    {\global\@specialpagetrue\gdef\@specialstyle{#1}\gdef\@cur@pagestyle{#1}%
     \@ifundefined{c@#1@lastpage}{\newcounter{ps@#1@lastpage}\AtEndDocument{\savepage{#1}}}{}}}
\newcommand{\TheLastPage}{\ref{ps@\@cur@pagestyle @lastpage}}
\makeatother

\copypagestyle{butter}{plain}
\begin{document}
  \pagestyle{plain}
  Page \thepage{} of \TheLastPage. % "1 of 2"
  \newpage
  Page \thepage{} of \TheLastPage. % "2 of 2"
  \newpage
  \pagestyle{butter}
  \setcounter{page}{1}             % reset page to 1
  Page \thepage{} of \TheLastPage. % correctly "1 of 1"
\end{document}

The \pagestyle and \thispagestyle macros are redefined to

  1. Create a page style-specific counter (if it's not already defined);
  2. Store the currently used page style in the macro \@cur@pagestyle (in order to use it later); and
  3. Write the page counters to the .aux file \AtEndDocument.

The page style-specific counter is incremented (stepped) at \EveryShipout (thanks to everyshi). Since memoir already defines \thelastpage, I've defined \TheLastPage that does the same, this time only page style-specific.

geometry was only loaded for this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.