您能否就如何获取以下目录格式给我一些建议?
章节标题 1-7
章节标题 9-10
章节标题 11-15
答案1
我的解决方案包括适当的标记(在每章的最开始和最结束处),定义一个宏\chaprange
,该宏从一对标签中检索页面,将它们格式化为<p1>-<p2>
(按要求),并重新定义\addcontentsline
。此外,还有三个宏控制页码模式:
\chaprangeon
激活功能;\chaprangeoff
暂时禁用该功能;\breakchaprange
如果最后一章是不是结束文档的内容。
代码
\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}
重要的:此代码需要三个编译周期才能正常工作。