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.
Asked
Active
Viewed 968 times
2
1 Answers
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:

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
\documentclass{...}and ending with\end{document}. – Aug 08 '14 at 18:12\labelcommands 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:58lastpagegives 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