2

I asked the question KOMA: Page range in TOC chapter entry

Answer linked to Chapterwise page range in the TOC

But i do not get the result for the following code...

\documentclass{scrbook}
\usepackage{lmodern}% for sans-serif plus bold plus italic


\usepackage{tocstyle}
\usetocstyle{standard}
\usetocstyle{nopagecolumn}

\settocstylefeature[0]{leaders}{\hfill}%
\settocstylefeature{pagenumberhook}{\itshape~}

\usepackage{blindtext}

\makeatletter
\newif\if@chap@enddc
  \@chap@enddctrue
\let\ltx@@chapter\@chapter
\renewcommand{\@chapter}[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
}
\def\chaprange{\expandafter\pageref{chap:\thechapter}--\expandafter\pageref{prenextchap:\thechapter}}
\let\ltx@addcontentsline\addcontentsline
\def\CR@addcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\chaprange}}
}
\def\ToggleChaprange{\let\addcontentsline\CR@addcontentsline}
\def\BypassChaprange{\let\addcontentsline\ltx@addcontentsline}
\def\BreakChaprange{%
  \expandafter\label{prenextchap:\thechapter}
  \let\addcontentsline\ltx@addcontentsline
  \@chap@enddcfalse
}
\let\ltx@section\section
\renewcommand{\section}[2][]{%
\BypassChaprange
\if\relax\detokenize{#1}\relax
  \ltx@section{#2}
\else
  \ltx@section[#1]{#2}
\fi
\ToggleChaprange
}
\makeatother

\begin{document}
\tableofcontents
\ToggleChaprange
\blinddocument
\BreakChaprange
\clearpage
\addcontentsline{toc}{chapter}{References}
\null
\appendix
\chapter{Additional Content}

\end{document}

enter image description here

Page range is printed for subsections also....

  • A simple but not pretty solution is to copy the code provided for "section" and replace each occurance of "section" by "subsection". A better solution, would be a rewrite of the code, so only the chapter contents entries are affected. – Andrew Swann Apr 8 '14 at 11:34
1
\documentclass{scrbook}
\usepackage{lmodern}
\usepackage{tocstyle}
  \usetocstyle{standard}
  \usetocstyle{nopagecolumn}
  \settocstylefeature[0]{leaders}{\hfill}%
  \settocstylefeature{pagenumberhook}{\itshape\nobreakspace}
\usepackage{etoolbox}
\usepackage{lipsum}

\makeatletter
\newif\if@chap@enddc \@chap@enddctrue
\let\ltx@@chapter=\@chapter
\renewcommand{\@chapter}[2][]{%
  \ltx@@chapter[#1]{#2}
  \expandafter\label{chap:\thechapter}}
\g@addto@macro\tableofcontents{%
  \let\ltx@chapter=\chapter
  \renewcommand\chapter{%
    \expandafter\label{prenextchap:\thechapter}
    \ltx@chapter}
  \ToggleChaprange}
\def\ToggleChaprange{\let\addcontentsline=\CR@addcontentsline}
\def\BypassChaprange{\let\addcontentsline=\ltx@addcontentsline}
\def\BreakChaprange{%
  \expandafter\label{prenextchap:\thechapter}
  \let\addcontentsline=\ltx@addcontentsline
  \@chap@enddcfalse}
\preto\enddocument{\if@chap@enddc\expandafter\label{prenextchap:\thechapter}\fi}
\let\ltx@addcontentsline=\addcontentsline
\def\CR@addcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\chaprange}}}
\def\chaprange{\expandafter\pageref{chap:\thechapter}--\expandafter\pageref{prenextchap:\thechapter}}
%Fix for sections, subsections, paragraphs and subparagraphs
\newcommand{\do@remove@chaprange}[1]{
  \expandafter\let\csname ltx@#1\expandafter\endcsname\csname #1\endcsname
  \expandafter\renewcommand\csname #1\endcsname[2][]{%
    \BypassChaprange
    \if\relax\detokenize{##1}\relax
      \csname ltx@#1\endcsname{##2}
    \else
      \csname ltx@#1\endcsname[##1]{##2}
    \fi
    \ToggleChaprange}}
\newcommand{\@remove@chaprange}[1]{%
  \let\do=\do@remove@chaprange
  \docsvlist{#1}}
\@remove@chaprange{section,subsection}%Applying the fix for sections and subsections
\makeatother

\begin{document}
\tableofcontents
\chapter{Title}
\section{Subtitle}
\subsection{Subsubtitle}
\lipsum
\chapter{Title}
\lipsum\lipsum
\chapter{Title}
\lipsum
\section{Subtitle}
\subsection{Subsubtitle}
\lipsum
\subsection{Subsubtitle}
\lipsum
%\BreakChaprange
%\clearpage
%\addcontentsline{toc}{chapter}{References}
%\null
%\appendix
%\chapter{Additional Content}
\end{document}
2

As per Andrew Swann comment i changed, it works well

\documentclass{scrbook}
\usepackage{lmodern}% for sans-serif plus bold plus italic


\usepackage{tocstyle}
\usetocstyle{standard}
\usetocstyle{nopagecolumn}

\settocstylefeature[0]{leaders}{\hfill}%
\settocstylefeature{pagenumberhook}{\itshape~}

\usepackage{blindtext}

\makeatletter
\newif\if@chap@enddc
  \@chap@enddctrue
\let\ltx@@chapter\@chapter
\renewcommand{\@chapter}[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
}
\def\chaprange{\expandafter\pageref{chap:\thechapter}~--~\expandafter\pageref{prenextchap:\thechapter}}
\let\ltx@addcontentsline\addcontentsline
\def\CR@addcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{#3}{\chaprange}}
}
\def\ToggleChaprange{\let\addcontentsline\CR@addcontentsline}
\def\BypassChaprange{\let\addcontentsline\ltx@addcontentsline}
\def\BreakChaprange{%
  \expandafter\label{prenextchap:\thechapter}
  \let\addcontentsline\ltx@addcontentsline
  \@chap@enddcfalse
}
\let\ltx@section\section
\renewcommand{\section}[2][]{%
\BypassChaprange
\if\relax\detokenize{#1}\relax
  \ltx@section{#2}
\else
  \ltx@section[#1]{#2}
\fi
\ToggleChaprange
}
\let\ltx@subsection\subsection
\renewcommand{\subsection}[2][]{%
\BypassChaprange
\if\relax\detokenize{#1}\relax
  \ltx@subsection{#2}
\else
  \ltx@subsection[#1]{#2}
\fi
\ToggleChaprange
}
\makeatother

\begin{document}
\tableofcontents
\ToggleChaprange
\blinddocument
\BreakChaprange
\clearpage
\addcontentsline{toc}{chapter}{References}
\null
\appendix
\chapter{Additional Content}

\end{document}

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.