2

I've been trying to output a page range, including the first and last page numbers of the chapter (e.g. "p. 20 - 39"). I've been using the \pageref{LastPage} command for the last page of the chapter, but can't find a way to output the first page (ideally a command such as \pageref{FirstPage}). Any help would be appreciated.

cgnieder
  • 66,645
  • 3
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Aug 08 '14 at 18:12
  • We usually use two \label commands related to the chapter number. Please post an MWE and we can try an experiment or two to get it work in your example. – Malipivo Aug 09 '14 at 05:58
  • you seem to think that lastpage gives you the last page of the chapter ... it doesn’t – it gives you the last page of the whole document. (feel free to ignore if i’ve misunderstood what you said.) – wasteofspace Aug 09 '14 at 07:54

1 Answers1

1

As was said in the comments LastPage gives the last page of the document and thus may not be very helpful for what you're trying to achieve. I suggest defining a label at the beginning of each chapter and then using those labels to get the pages between them. Consider the following:

\documentclass{scrbook}

\usepackage{lastpage}
\newcounter{rangeendpage}
\newcounter{rangepages}
\newcommand{\setpagecounters}[2]{%
    \setcounter{rangeendpage}{\pageref{#2}}
    \addtocounter{rangeendpage}{-1}
    \setcounter{rangepages}{\pageref{#2}}
    \addtocounter{rangepages}{-\pageref{#1}}
    \makeatletter
    \if\therangepages0%
        \setcounter{rangeendpage}{\pageref{#2}}
        \setcounter{rangepages}{1}
    \fi
    \makeatother
}

\usepackage{blindtext}
\begin{document}
\chapter{Chapter 1}\label{ch1}
    \setpagecounters{ch1}{ch2}
    This chapter contains pages \pageref{ch1} to \arabic{rangeendpage}, which is \arabic{rangepages} in total.

    \Blindtext
    \Blindtext

    \chapter{Chapter 2}\label{ch2}
    \setpagecounters{ch2}{ch3}
    This chapter contains pages \pageref{ch2} to \arabic{rangeendpage}, which is \arabic{rangepages} in total.
    \blindtext

    \chapter{Chapter 3}\label{ch3}
    \setpagecounters{ch3}{LastPage}
    This chapter is the last and contains pages \pageref{ch3} to \arabic{rangeendpage}, which is \arabic{rangepages} in total.

\end{document}

which will produce output like this:

enter image description here

The example uses the LastPage for the very last chapter. It also takes care of the case where a chapter is only one page - see Chapter 3. Blank pages before the start of a new chapter are counted towards the previous chapter - see Chapter 1.

greyshade
  • 3,576