将书籍文档类中每个新部分的章节计数器重置为 0

将书籍文档类中每个新部分的章节计数器重置为 0

出于某些人可能认为是误导的原因,有些情况下我想将 构造documentclass{book}parts并从 1 开始对chapters每个进行编号part。只要不需要使用 ,这种方法就可以正常工作hyperref。显而易见的做法是\setcounter{chapter}{0}在 之前调用\part{...}。经过多次尝试使用 使其工作hyperref,首先使用minitoc包,现在使用etoc,我意识到我一直在追逐一个白日梦。hyperref与这两个包中的任何一个都可以很好地交互,但是,如下面的 MWE 所示,只是根据误导性信息采取行动。在他的其中一个答案中egreg表明\numberwithin{chapter}{part}可能是解决问题的方法,但我发现这只是部分如此。如果不是因为前段时间在 的TeX.SX某个地方,我看到一个答案表明 可能chapters不是由 引用,chapter counter而是由一对 引用,我会在这个阶段承认失败。通过在 Exchange 上搜索各种和part counter/chapter counter来寻找那个特定答案的漫长搜索无果而终。是时候放弃所有希望了吗? tagsUsers

\documentclass[a4paper, 11pt, oneside]{book}
% MWE 21June16
% COMMENTS: Resetting chapter counter to 0 for each new part.
%=========================================
\usepackage{lipsum}
\usepackage{shorttoc}
\usepackage{etoc}
\usepackage{hyperref}
%\usepackage{amsmath}
%\numberwithin{chapter}{part}

\begin{document}
%===========
\frontmatter
%===========
\setcounter{tocdepth}{3}  % main table of contents, if used
\setcounter{secnumdepth}{3} 
\shorttableofcontents{Summary Contents}{0}
%\tableofcontents
\chapter{Preface}
.... some text ....

\chapter{Introduction}
.... some text ....
%===========
\mainmatter
%===========
\part{Part ONE}
\etocsettocdepth{1}
\localtableofcontents

\chapter{Chap. One}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter One.
\section{Sec. Alpha}
General comments pertaining to all subsections of Part ONE/Chapter One/Section Alpha.
\subsection{Subsec. one}
General comments to all subsubsections of Part ONE/Chapter One/Section Alpha/Subsection one.
\subsubsection{Subsubsec. 1}
\lipsum[1]
\subsubsection{Subsubsec. 2}
\lipsum[1]
\subsection{Subsec. two}
\lipsum[1]
\section{Sec. Bravo}
General comments pertaining to all subsections of Part ONE/Chapter One/Section Bravo.
\subsection{Subsec. three}
\lipsum[1]
\subsection{Subsec. four}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Two}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter .
\section{Sec. Charlie}
General comments pertaining to all subsections of Part ONE/Chapter Two/Section Charlie.
\subsection{Subsec. five}
General comments to all subsubsections of Part ONE/Chapter Two/Section Charlie/Subsection five.
\subsubsection{Subsubsec. 3}
\lipsum[1]
\subsubsection{Subsubsec. 4}
\lipsum[1]
\subsection{Subsec. six}
\lipsum[1]
\section{Sec. Delta}
General comments pertaining to all subsections of Part ONE/Chapter Two/Section Delta.
\subsection{Subsec. seven}
\lipsum[1]
\subsection{Subsec. eight}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Three}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter .
\section{Sec. Echo}
General comments pertaining to all subsections of Part ONE/Chapter Three/Section .
\subsection{Subsec. nine}
General comments to all subsubsections of Part ONE/Chapter Three/Section /Subsection .
\subsubsection{Subsubsec. 5}
\lipsum[1]
\subsubsection{Subsubsec. 6}
\lipsum[1]
\subsection{Subsec. ten}
\lipsum[1]
\section{Sec. Foxtrot}
General comments pertaining to all subsections of Part ONE/Chapter Three/Section .
\subsection{Subsec. eleven}
\lipsum[1]
\subsection{Subsec. twelve}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\setcounter{chapter}{0}
\part{Part TWO}
\etocsettocdepth{1}
\localtableofcontents

\chapter{Chap. Four}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. Golf}
General comments pertaining to all subsections of Part TWO/Chapter Four/Section .
\subsection{Subsec. thirteen}
General comments to all subsubsections of Part TWO/Chapter Four/Section /Subsection .
\subsubsection{Subsubsec. 7}
\lipsum[1]
\subsubsection{Subsubsec. 8}
\lipsum[1]
\subsection{Subsec. fourteen}
\lipsum[1]
\section{Sec. Hotel}
General comments pertaining to all subsections of Part TWO/Chapter Four/Section .
\subsection{Subsec. fifteen}
\lipsum[1]
\subsection{Subsec. sixteen}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Five}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. India}
General comments pertaining to all subsections of Part TWO/Chapter Five/Section .
\subsection{Subsec. seventeen}
General comments to all subsubsections of Part TWO/Chapter Five/Section /Subsection .
\subsubsection{Subsubsec. 9}
\lipsum[1]
\subsubsection{Subsubsec. 10}
\lipsum[1]
\subsection{Subsec. eighteen}
\lipsum[1]
\section{Sec. Juliett}
General comments pertaining to all subsections of Part TWO/Chapter Five/Section .
\subsection{Subsec. nineteen}
\lipsum[1]
\subsection{Subsec. twenty}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Six}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. Kilo}
General comments pertaining to all subsections of Part TWO/Chapter Six/Section .
\subsection{Subsec. twenty-one}
General comments to all subsubsections of Part TWO/Chapter Six/Section /Subsection .
\subsubsection{Subsubsec. 11}
\lipsum[1]
\subsubsection{Subsubsec. 12}
\lipsum[1]
\subsection{Subsec. twenty-two}
\lipsum[1]
\section{Sec. Lima}
General comments pertaining to all subsections of Part TWO/Chapter Six/Section .
\subsection{Subsec. twenty-three}
\lipsum[1]
\subsection{Subsec. twenty-four}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\part{Part THREE}
\etocsettocdepth{1}
\localtableofcontents

etc. etc.

\end{document}

答案1

使用核心宏将计数器放在chapter重置列表中part很容易LaTeX\@addtoreset

\makeatletter
\@addtoreset{chapter}{part}
\makeatother

或者使用chngcntr包和\counterwithin*{chapter}{part}

\usepackage{chngcntr}
\counterwithin*{chapter}{part}

在这种情况下,我使用\counterwithin*来防止更改\thechapter。有关更多信息,请参阅chngcntr文档。如果要使用零件编号作为后缀,则目录中的数字宽度必须扩大!

笔记

必须这样做 hyperref已加载,因为hyperref已将抓地力放在计数器和锚点上,并且执行了正确的设置!否则会导致错误的锚点和大量警告!

\documentclass[a4paper, 11pt, oneside]{book}
% MWE 21June16
% COMMENTS: Resetting chapter counter to 0 for each new part.
%=========================================
\usepackage{lipsum}
\usepackage{shorttoc}
\usepackage{etoc}
\usepackage{hyperref}
\makeatletter
\@addtoreset{chapter}{part}
\makeatother

% Or
% \usepackage{chngcntr}
% \counterwithin{chapter}{part}

\begin{document}
%===========
\frontmatter
%===========
\setcounter{tocdepth}{3}  % main table of contents, if used
\setcounter{secnumdepth}{3} 
\shorttableofcontents{Summary Contents}{0}
%\tableofcontents
\chapter{Preface}
.... some text ....

\chapter{Introduction}
.... some text ....
%===========
\mainmatter
%===========
\part{Part ONE}
\etocsettocdepth{1}
\localtableofcontents

\chapter{Chap. One}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter One.
\section{Sec. Alpha}
General comments pertaining to all subsections of Part ONE/Chapter One/Section Alpha.
\subsection{Subsec. one}
General comments to all subsubsections of Part ONE/Chapter One/Section Alpha/Subsection one.
\subsubsection{Subsubsec. 1}
\lipsum[1]
\subsubsection{Subsubsec. 2}
\lipsum[1]
\subsection{Subsec. two}
\lipsum[1]
\section{Sec. Bravo}
General comments pertaining to all subsections of Part ONE/Chapter One/Section Bravo.
\subsection{Subsec. three}
\lipsum[1]
\subsection{Subsec. four}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Two}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter .
\section{Sec. Charlie}
General comments pertaining to all subsections of Part ONE/Chapter Two/Section Charlie.
\subsection{Subsec. five}
General comments to all subsubsections of Part ONE/Chapter Two/Section Charlie/Subsection five.
\subsubsection{Subsubsec. 3}
\lipsum[1]
\subsubsection{Subsubsec. 4}
\lipsum[1]
\subsection{Subsec. six}
\lipsum[1]
\section{Sec. Delta}
General comments pertaining to all subsections of Part ONE/Chapter Two/Section Delta.
\subsection{Subsec. seven}
\lipsum[1]
\subsection{Subsec. eight}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Three}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part ONE/Chapter .
\section{Sec. Echo}
General comments pertaining to all subsections of Part ONE/Chapter Three/Section .
\subsection{Subsec. nine}
General comments to all subsubsections of Part ONE/Chapter Three/Section /Subsection .
\subsubsection{Subsubsec. 5}
\lipsum[1]
\subsubsection{Subsubsec. 6}
\lipsum[1]
\subsection{Subsec. ten}
\lipsum[1]
\section{Sec. Foxtrot}
General comments pertaining to all subsections of Part ONE/Chapter Three/Section .
\subsection{Subsec. eleven}
\lipsum[1]
\subsection{Subsec. twelve}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\part{Part TWO}
\etocsettocdepth{1}
\localtableofcontents

\chapter{Chap. Four}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. Golf}
General comments pertaining to all subsections of Part TWO/Chapter Four/Section .
\subsection{Subsec. thirteen}
General comments to all subsubsections of Part TWO/Chapter Four/Section /Subsection .
\subsubsection{Subsubsec. 7}
\lipsum[1]
\subsubsection{Subsubsec. 8}
\lipsum[1]
\subsection{Subsec. fourteen}
\lipsum[1]
\section{Sec. Hotel}
General comments pertaining to all subsections of Part TWO/Chapter Four/Section .
\subsection{Subsec. fifteen}
\lipsum[1]
\subsection{Subsec. sixteen}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Five}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. India}
General comments pertaining to all subsections of Part TWO/Chapter Five/Section .
\subsection{Subsec. seventeen}
General comments to all subsubsections of Part TWO/Chapter Five/Section /Subsection .
\subsubsection{Subsubsec. 9}
\lipsum[1]
\subsubsection{Subsubsec. 10}
\lipsum[1]
\subsection{Subsec. eighteen}
\lipsum[1]
\section{Sec. Juliett}
General comments pertaining to all subsections of Part TWO/Chapter Five/Section .
\subsection{Subsec. nineteen}
\lipsum[1]
\subsection{Subsec. twenty}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\chapter{Chap. Six}
\begingroup
\etocsettocdepth{3}
\let\clearpage\relax
\localtableofcontents
\endgroup
\newpage

\addcontentsline{toc}{section}{Introduction}
\section*{Introduction}
Introducing the topic of Part TWO/Chapter .
\section{Sec. Kilo}
General comments pertaining to all subsections of Part TWO/Chapter Six/Section .
\subsection{Subsec. twenty-one}
General comments to all subsubsections of Part TWO/Chapter Six/Section /Subsection .
\subsubsection{Subsubsec. 11}
\lipsum[1]
\subsubsection{Subsubsec. 12}
\lipsum[1]
\subsection{Subsec. twenty-two}
\lipsum[1]
\section{Sec. Lima}
General comments pertaining to all subsections of Part TWO/Chapter Six/Section .
\subsection{Subsec. twenty-three}
\lipsum[1]
\subsection{Subsec. twenty-four}
\lipsum[1]
\addcontentsline{toc}{section}{Summary}
\section*{Summary}
Summarizing all topics discussed in this chapter and looking ahead.

\part{Part THREE}
\etocsettocdepth{1}
\localtableofcontents

etc. etc.

\chapter{Foo chapter}

\end{document}

相关内容