5

How do I get the page numbers of first and last page for a chapter? It seems I cannot do it via a \label, as in the MWE below, as, if I get it right, they can be set only once for the whole document. I intend to eventually use these numbers in a footer.

\documentclass{memoir}
\usepackage{lipsum}

\begin{document}

\chapter{chapter one}

\label{firstpage}
[First chapter is from page \pageref{firstpage} to page \pageref{lastpage}.]

\lipsum
\label{lastpage}
\chapter{chapter two}

\label{firstpage}
[Second chapter is from page \pageref{firstpage} to page \pageref{lastpage}.]

\lipsum
\label{lastpage}

\end{document}
muk.li
  • 3,620
  • 2
    Why can't you just use different names for the labels (e.g. ch1:firstpage, ch2:firstpage, etc.)? – Null Mar 12 '15 at 18:08
  • @Null: Because then I'd have to change the pagestyle for each chapter, which I'm trying to avoid. Eventually these numbers are to appear in the footer of each page. – muk.li Mar 12 '15 at 18:40

3 Answers3

4

You can redefine \chapter to do the work. You'll need a \finishchapter at the end of the last chapter.

There may be problems in case a chapter ends filling up a page, so mayby removing the

 \label{\thechapter @lastpage}

line from the code below and adding manually \finishchapter after the last word of a chapter might be better.

\documentclass{memoir}
\usepackage{xparse}

\usepackage{lipsum}

\let\memoirchapter\chapter

\RenewDocumentCommand{\chapter}{soom}{%
  \label{\thechapter @lastpage}%
  \IfBooleanTF{#1}
   {\IfValueTF{#2}{\memoirchapter*[#2]{#4}}{\memoirchapter*{#4}}}%
   {\IfValueTF{#2}
     {\IfValueTF{#3}{\memoirchapter[#2][#3]{#4}}{\memoirchapter[#2]{#4}}}%
     {\memoirchapter{#4}}%
   }%
  \label{\thechapter @firstpage}%
}
\NewDocumentCommand{\chapterfirstpage}{}{%
  \pageref{\thechapter @firstpage}%
}
\NewDocumentCommand{\chapterlastpage}{}{%
  \pageref{\thechapter @lastpage}%
}
\NewDocumentCommand{\finishchapter}{}{%
  \label{\thechapter @lastpage}%
}

\begin{document}

\chapter{chapter one}

[First chapter is from page \chapterfirstpage{} to page \chapterlastpage{}.]

\lipsum

\chapter{chapter two}

[Second chapter is from page \chapterfirstpage{} to page \chapterlastpage{}.]

\lipsum[1-12]

\chapter{chapter three}

[Third chapter is from page \chapterfirstpage{} to page \chapterlastpage{}.]

\lipsum[1-30]

\finishchapter

\end{document}

Note that the normal behavior is to disregard empty pages.

egreg
  • 1,121,712
3

The following example automatically updates the chapter's first/last page references using a standard page style (called mystyle). The chapter page references are stored in counters, updated at the start/end of every chapter using some macros from refcount - it allows one to store references in counters.

enter image description here

\documentclass{memoir}
\usepackage{lipsum,refcount}

\newcounter{chapfirstpage}
\newcounter{chaplastpage}

\makepagestyle{mystyle}
\makeoddfoot{mystyle}{}{\thepage{} [Chapter range: \thechapfirstpage--\thechaplastpage]}{}
\makeevenfoot{mystyle}{}{\thepage{} [Chapter range: \thechapfirstpage--\thechaplastpage]}{}

\aliaspagestyle{chapter}{mystyle}% 'chapter' page style should be the same as 'mystyle'
\aliaspagestyle{plain}{mystyle}% 'plain' page style should be the same as 'mystyle'

\let\oldchapter\chapter% Store \chapter
\renewcommand{\chapter}{% Update \chapter
  \ifnum\value{chapter}=0\else% If _not_ the first chapter
    \label{ch\thechapter:lastpage}% Insert an end-of-chapter \label
  \fi
  \clearpage
  \addtocounter{chapter}{1}%
  \label{ch\thechapter:firstpage}% Insert a start-of-chapter \label
  \setcounterpageref{chapfirstpage}{ch\thechapter:firstpage}%
  \setcounterpageref{chaplastpage}{ch\thechapter:lastpage}%
  \addtocounter{chapter}{-1}%
  \oldchapter% ...follow regular \chapter
}

\AtEndDocument{\label{ch\number\value{chapter}:lastpage}}% Issue end-of-chapter (end-of-document) \label
                                                         % for last chapter

\begin{document}

\chapter{chapter one}\lipsum
\chapter{chapter two}\lipsum

\end{document}

Of course, you can use your own page styles... I've blatantly overwritten both the chapter and plain page styles so as to make sure it's listed in all page footers.

Note that you'll have to create your own page references to access this mid-document. In a pinch you can use \pageref{ch<num>:firstpage}/\thechapfirstpage and \pageref{ch<num>:lastpage}/\thechaplastpage to extract them, but the automation is geared at updating the page style information, not the document content information.

Also note that, depending on your document construction, my use of

\AtDocumentEnd{...}

to insert the last page reference for ch<num>:lastpage might not be sufficient (perhaps due to pending float placement at the end of the document. However, it'll work for relatively elementary cases. Of course, the atveryend package could be used to circumvent this problem, or using memoir's own lastpage or lastsheet "reference" directly.

Werner
  • 603,163
  • How I can I add hyperlink to the page ranges? Say, if I click the last \thechaplastpage, it will take me to that particular page... – hola Apr 01 '19 at 21:56
1

I found a solution by creating a pair of macros which I can redefine at the beginning of every chapter. This should work, but it would be nice to have something more elegant.

\documentclass{memoir}
\usepackage{lipsum}
\newcommand{\chapterfirstpage}{}
\newcommand{\chapterlastpage}{}
\begin{document}

\chapter{chapter one}
\renewcommand{\chapterfirstpage}{\pageref{ch1.firstpage}}
\renewcommand{\chapterlastpage}{\pageref{ch1.lastpage}}

\label{ch1.firstpage}
[First chapter is from page \chapterfirstpage{} to page \chapterlastpage{}.]

\lipsum
\label{ch1.lastpage}
\chapter{chapter two}

\renewcommand{\chapterfirstpage}{\pageref{ch2.firstpage}}
\renewcommand{\chapterlastpage}{\pageref{ch2.lastpage}}
\label{ch2.firstpage}
[Second chapter is from page \chapterfirstpage{} to page \chapterlastpage{}.]

\lipsum
\label{ch2.lastpage}

\end{document}
muk.li
  • 3,620