我问了这个问题KOMA:目录章节条目中的页面范围
答案链接到目录中按章节划分的页面范围
但我没有得到以下代码的结果......
\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}
还打印了小节的页面范围...
答案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
根据 Andrew Swann 的评论,我进行了更改,效果很好
\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}