# Write section-wise page range (i.e. including last page of section) to TOC (using etoc)

This question might be a bit exotic: I am writing a script for students where I reset the page count for every section and add \thesection to the page mark, e.g. page number 1.1-2. All pages are hence labeled unambiguously while I am still able to insert pages to section without affecting the subsequent page numbers.

This, however, has the disadvantage that the students cannot tell from the page numbering whether they actually have all the pages of a section. My wish, therefore, is to show not only the first page number of a section in the TOC, but also the last page number. In the MWE below, the TOC should reveal that Section 1.1 starts on page 1.1-1 and ends on page 1.1-5.

Is this possible? I am using the etoc package anyway, so a solution that integrates with local TOCs would be ideal.

I apologize for my MWE not being so minimal. I thought it best to showcase something close to my use case.

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{calc, setspace}
\setstretch{1.15}
\setlength{\parindent}{0pt}
\usepackage{etoc}
\etocsetnexttocdepth{subsection}
\setcounter{tocdepth}{2}

\renewcommand*{\thepage}{\thesection-\arabic{page}}

\newlength{\mypnumwidth}\settowidth{\mypnumwidth}{D-0.D-00}
\etocsetstyle{chapter}{}{}
{\bfseries\etocifnumbered{\makebox[5mm][l]{\etocnumber}}{}\etocname\hfill
\makebox[\mypnumwidth][r]{\etocpage}\par}{}
\etocsetstyle{section}{\smallskip}{\smallskip}
{\mdseries\hspace{5mm}%
\etocifnumbered{\makebox[8mm][l]{\etocnumber}\etocname%
\makebox[\mypnumwidth][r]{\etocpage}}{%
\etocname}%
\par}{\bigskip}
\nobreak\etocskipfirstprefix\smallskip\begin{spacing}{1}\small}
{$\;${\footnotesize\textbullet}\allowbreak$\;$}{\etocname}{\end{spacing}\medskip\leftskip0mm\rightskip0mm}

\begin{document}
%\tableofcontents

\etocsettocstyle{\subsection*{Contents}}{}%
\localtableofcontents

\cleardoublepage

\cleardoublepage

\end{document}

I checked the suggested topics

but if they contain information that helps me, I was not able to extract it. The last one comes really close, but integrating it with etoc myself is unfortunately beyond me. So I would appreciate any input. Thanks a lot!

\documentclass{scrbook}

\usepackage{blindtext}
\usepackage{calc, setspace}
\setstretch{1.15}
\setlength{\parindent}{0pt}
\usepackage{etoc}
% \etocsetnexttocdepth{subsection}% better to use this where
% really needed, not here
\setcounter{tocdepth}{2}% 2 = subsection anyway

\renewcommand*{\thepage}{\thesection-\arabic{page}}

\newlength{\mypnumwidth}\settowidth{\mypnumwidth}{9.9-99 to 9.9.99}

\etocsetstyle{chapter}
{}
{}
{\bfseries
\etocifnumbered
{\makebox[5mm][l]{\etocnumber}}
{}%
\etocname\hfill
\makebox[\mypnumwidth][r]{\etocpage}\par
}
{}

\etocsetstyle{section}
{\smallskip}
{\smallskip}
{\mdseries
\hspace{5mm}%
\etocifnumbered
{\makebox[8mm][l]{\etocnumber}%
\etocname
\makebox[\mypnumwidth][r]
}
{\etocname}%
\par
}
{\bigskip}

\etocsetstyle{subsection}
{\leftskip=13mm
\rightskip=\mypnumwidth
\nobreak\etocskipfirstprefix
\smallskip
\begin{spacing}{1}\small
}
{$\;${\footnotesize\textbullet}\allowbreak$\;$}
{\etocname}
{\end{spacing}\medskip
\leftskip0mm
\rightskip0mm }

\makeatletter
\newcommand\PageRangeOf[1]{\@nameuse{PageRange#1}}

\newcommand\StorePageRange{%
\write\@auxout
{\noexpand\expandafter\gdef\noexpand\csname
PageRange\thesection\noexpand\endcsname
{\thesection-1 to \thesection-\the\value{page}}}%
}%
\makeatother

% \usepackage{hyperref}% testing

\begin{document}
%\tableofcontents

\etocsettocstyle{\subsection*{Contents}}{}
\localtableofcontents

\cleardoublepage
\StorePageRange

\cleardoublepage
\StorePageRange

\end{document}

Close each section with a \StorePageRange. (this must not at very end of document occur after a \clearpage IIRC).

With some auxiliary macros we could configure the typesetting like using colors for page numbers etc...

• \StorePageRange stores the date in pre-formatted form with no alteration as "page number range". A \StoreLastPage could be done rather, and the you can use \textbf{\LastPageOf{\etocthenumber}} for example... etc... – user4686 Sep 21 '18 at 18:21
• The \write\@auxout could also use \global\noexpand\@namedef{PageRange\thesection}{\thesection-1 to \thesection-\the\value{page}} which may be less complicated than the \csname... \endcsname with three \noexpand. This is equivalent. (attention to not insert extra spaces) – user4686 Sep 21 '18 at 21:46
• Fantastic, thanks a lot! By the way, elsewhere it was suggested to use macros like \csdef from etoolbox to handle the construction of command sequences.Then the \write\@auxout reduces to \csgdef{LastPage\thesection}{\the\value{page}}. – FlorianL Sep 22 '18 at 10:50
• Indeed \csgdef is "protected" macro so the aux file remains tidy and clean, and a bit more terse than with \global\noexpand\@namedef, \csgdef there repalceing \global\@namedef. I you need etoolbox for other things why not, but if you don't it is not needed to load a whole package for sparing two tokens in a macro definition... (the aux file is always read with @ of catcode letter anyhow). It may be though that etoolbox is loaded by other packages already in use in your document... – user4686 Sep 22 '18 at 11:16