Can you give me some advice for how to obtain the following format of the TOC?
Chapter’s heading 1-7
Chapter’s heading 9-10
Chapter’s heading 11-15
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityCan you give me some advice for how to obtain the following format of the TOC?
Chapter’s heading 1-7
Chapter’s heading 9-10
Chapter’s heading 11-15
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:
\chaprangeon
activates the functionality;\chaprangeoff
temporary disables the functionality;\breakchaprange
disables the functionality if the last chapter is not the content ending your document.\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.
chaprange
(v0.99a: first test version)
7
mean that page8
is blank, or that the last\section
from the first\chapter
is on page7
?