8

Can you give me some advice for how to obtain the following format of the TOC?

  1. Chapter’s heading 1-7

  2. Chapter’s heading 9-10

  3. Chapter’s heading 11-15

  • 2
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Did you try building some minimal working example (MWE) ? if so please post – texenthusiast Nov 14 '13 at 16:31
  • Have you seen this? Probably it may solve your problem. – crixstox Nov 14 '13 at 17:08
  • does the 7 mean that page 8 is blank, or that the last \section from the first \chapter is on page 7? – user4686 Nov 21 '13 at 15:32
  • It is means that last line of chapter 1 is appearing on page 7. It may be possible that the document will be of two-sided and page 8 may be blank – Ganesh Sharma Dec 13 '13 at 13:15
20

My solution consists of appropriate labeling (in the very beginning and the very end of each chapter), defining a macro \chaprange that retrives the pages from a pair of labels formatting them to <p1>-<p2> (as requested), and in redefining \addcontentsline. Moreover, there are three macros controlling the page numbering pattern:

  1. \chaprangeon activates the functionality;
  2. \chaprangeoff temporary disables the functionality;
  3. \breakchaprange disables the functionality if the last chapter is not the content ending your document.

The Code

\documentclass{book}
\usepackage{lipsum}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\if@chap@enddc
  \@chap@enddctrue
\let\ltx@@chapter\@chapter
\def\@chapter[#1]#2{%
  \ltx@@chapter[#1]{#2}
  \expandafter\label{chap:\thechapter}}%
\let\ltx@toc\tableofcontents
\renewcommand\tableofcontents{%
  \ltx@toc
  \let\ltx@chapter\chapter
  \renewcommand{\chapter}{%
    \expandafter\label{prenextchap:\thechapter}
    \ltx@chapter}}%
\let\ltx@enddocument\enddocument
\renewcommand\enddocument{%
  \if@chap@enddc\expandafter\label{prenextchap:\thechapter}\fi
  \ltx@enddocument}%
\newcommand\chaprange{%
  \expandafter\pageref{chap:\thechapter}--\expandafter\pageref{prenextchap:\thechapter}}%
\let\ltx@addcontentsline\addcontentsline
\newcommand{\CR@addcontentsline}[3]{%
  \edef\@tempa{\detokenize{chapter}}
  \edef\@tempb{\detokenize{#2}}
  \ifx\@tempa\@tempb
    \let\CR@thepage\chaprange
  \else
    \let\CR@thepage\thepage
  \fi
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\CR@thepage}}}%
\newcommand\chaprangeon{\let\addcontentsline\CR@addcontentsline}
\newcommand\chaprangeoff{\let\addcontentsline\ltx@addcontentsline}
\newcommand\breakchaprange{%
  \expandafter\label{prenextchap:\thechapter}
  \let\addcontentsline\ltx@addcontentsline
  \@chap@enddcfalse}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\g@addto@macro\mainmatter{\chaprangeon}
\g@addto@macro\backmatter{\breakchaprange}
\makeatother

\begin{document}
\tableofcontents

\mainmatter

\chapter{Title 1}
\lipsum

\section{Subtitle 1}
\lipsum

\chapter{Title 2}
\lipsum\lipsum

\chapter{Title 3}
\lipsum\lipsum\lipsum

\backmatter
\addcontentsline{toc}{chapter}{References}
\null

\appendix
\chapter{Additional Content}
\end{document}

Important: This code requires three compilation cycles to work properly.

Example output

output

Updates

  • Aug/28/'15 Solution available as package chaprange (v0.99a: first test version)
  • Aug/08/'15 Changed the names of the user macros and fixed a few minor details in the example.
  • July/25/'15 The Code now works only on chapters without any need of patching sections, subsections, etc.
  • Oct/30/'14 Consider this followup-question if you are using sectioning commands of deeper levels than sections and have problems with the correct formatting.

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.