具有自动分页 TOC 的书中 titlesec 和 parttitle 之间的干扰

具有自动分页 TOC 的书中 titlesec 和 parttitle 之间的干扰

这个问题主要涉及交叉引用目录中的超链接和分页符

John Kormylo 提供的答案和我们进一步的讨论导致了这样的代码更改,以至于我无法继续在评论中提出以下问题:

1)我想\parttitle在使用时添加一些标题titlesec(我尝试了使用 titlesec 包在 header\footer 中显示 \parttitle沒有效果)。

下面的尝试主要基于如何在 LaTeX 中获取零件名称?

但是,我的代码中的计数器似乎有误,因为第一部分的页眉上没有显示任何内容,第二部分的页眉上显示了“第一部分”标题,第三部分的页眉上显示了“第二部分”标题,...

章节计数器在各部分的开头重置(使用 \part 时超链接不正确。我该如何纠正?) 似乎没有影响。

2)我之前的帖子中也有一个未解答的问题(当然很难):

如何在目录中自动分页?(手动解决方案位于将目录拆分为两页

此处的代码应自动禁止目录中的分页符:

  • 级别与子级别之间。
  • 某一级别的目录条目是否可以位于单个目录页面上。

提前感谢您的回答。

\documentclass[12pt,a4paper]{book}
\raggedbottom
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titling}
\author{the author}
\usepackage[clearempty,explicit,pagestyles]{titlesec}
\renewpagestyle{plain}{\sethead[\MakeUppercase\chaptertitle][][\theauthor]{\theauthor}{}{\MakeUppercase\chaptertitle}\headrule\footrule\setfoot[\thepage][][\theauthor]{\theauthor}{}{\thepage}}
\pagestyle{plain}
\titleformat{\part}{\Huge\bfseries}{\thepart}{2\wordsep}{\hyperlink{part.\theHpart.cross}{#1}}
\titleformat{\section}{\Large\bfseries}{\thesection}{2\wordsep}{\hyperlink{section.\theHsection.cross}{#1}}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{2\wordsep}{\hyperlink{subsection.\theHsubsection.cross}{#1}}
\titlespacing{\chapter}{0pt}{0pt}{\baselineskip}
\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip}
\titlespacing{\subsection}{0pt}{\baselineskip}{\baselineskip}
\usepackage[hidelinks,linktoc=all]{hyperref}
\renewcommand{\theHchapter}{\thepart.\thechapter}
\renewcommand{\theHsection}{\thepart.\thesection}
\renewcommand{\theHsubsection}{\thepart.\thesubsection}
\let\originalcontentsline\contentsline
\renewcommand{\contentsline}[4]{\originalcontentsline{#1}{#2}{\strut\raisebox{\baselineskip}{\hypertarget{#4.cross}{}}#3}{#4}}

%## UNSUCCESSFUL ATTEMPT TO GET THE (GOOD) PART TITLE IN THE HEADER ##
\let\originalpart\part
\renewcommand{\part}[1]{
\cleardoublepage
\renewpagestyle{plain}{\sethead[][][]{\theauthor}{}{\thepart\ \MakeUppercase\parttitle}\headrule\footrule\setfoot[][][]{\theauthor}{}{\thepage}}
\originalpart{#1}\renewcommand{\parttitle}{#1}
\renewpagestyle{plain}{\sethead[\thepart.\thechapter\ \MakeUppercase\chaptertitle][][\theauthor]{\theauthor}{}{\thepart.\thechapter\ \MakeUppercase\chaptertitle}\headrule\footrule\setfoot[\thepage][][\theauthor]{\theauthor}{}{\thepage}}
\pagestyle{plain}}

%## UNSUCCESSFUL ATTEMPT TO GET AUTOMATIC PAGE BREAKS IN THE TOC ##
%\usepackage{etoc}
%\etocsetstyle{part}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[2em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
%\etocsetstyle{chapter}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[4em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
%\etocsetstyle{section}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[6em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}
%\etocsetstyle{subsection}{\nopagebreak\etocskipfirstprefix}{\nopagebreak{\penalty 9999}}{\makebox[8em][r]{\etocnumber}\hspace{2\wordsep}\etocname\dotfill\etocpage\par}{\nopagebreak{\penalty -9999}}

\begin{document}
\begin{titlepage}
\begin{center}
{\Huge\bfseries Title\\}
\vspace{2\baselineskip}
{\huge\bfseries Subtitle\\}
\vspace{8\baselineskip}
{\LARGE Subsubtitle\\}
\vspace{2\baselineskip}
{\Large Subsubsubtitle\\}
\vspace{8\baselineskip}
{\large Author}
\end{center}
\end{titlepage}
\tableofcontents
\cleardoublepage
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{chapter.\theHchapter.cross}{#1}}

\part{The First Part}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\section{A section}

\subsection{A subsection}

\subsection{A subsection}

\section{A section}

Some text.

\section{A section}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\part{The Second Part}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\part{The Third Part}

\chapter{A chapter}

\section{A section}

\clearpage
\end{document}

答案1

要修复标题,您需要做的就是\parttitle在运行之前重新定义\originalpart

为了实现自动化,不在所需的部分中间中断,我必须重新定义\section以在每个部分之前自动添加\needspace(needspace 包)。 \sectiontotal计算每个条目所需的空间2\baselineskip

我认为双倍行距是故意的。

\documentclass[12pt,a4paper]{book}
\raggedbottom
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{needspace}
\usepackage{titling}
\author{the author}
\usepackage[clearempty,explicit,pagestyles]{titlesec}
\renewpagestyle{plain}{\sethead[\MakeUppercase\chaptertitle][][\theauthor]{\theauthor}{}{\MakeUppercase\chaptertitle}\headrule\footrule\setfoot[\thepage][][\theauthor]{\theauthor}{}{\thepage}}
\pagestyle{plain}
\titleformat{\part}{\Huge\bfseries}{\thepart}{2\wordsep}{\hyperlink{part.\theHpart.cross}{#1}}
\titleformat{\section}{\Large\bfseries}{\thesection}{2\wordsep}{\hyperlink{section.\theHsection.cross}{#1}}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{2\wordsep}{\hyperlink{subsection.\theHsubsection.cross}{#1}}
\titlespacing{\chapter}{0pt}{0pt}{\baselineskip}
\titlespacing{\section}{0pt}{\baselineskip}{\baselineskip}
\titlespacing{\subsection}{0pt}{\baselineskip}{\baselineskip}
\usepackage[hidelinks,linktoc=all]{hyperref}
\renewcommand{\theHchapter}{\thepart.\thechapter}
\renewcommand{\theHsection}{\thepart.\thesection}
\renewcommand{\theHsubsection}{\thepart.\thesubsection}
\let\originalcontentsline\contentsline
\renewcommand{\contentsline}[4]{\originalcontentsline{#1}{#2}{\strut\raisebox{\baselineskip}{\hypertarget{#4.cross}{}}#3}{#4}}

%## UNSUCCESSFUL ATTEMPT TO GET THE (GOOD) PART TITLE IN THE HEADER ##
\let\originalpart\part
\renewcommand{\part}[1]{%
  \savesubsectiontotal
  \cleardoublepage
  \renewpagestyle{plain}{\sethead[][][]{\theauthor}{}{\thepart\ \MakeUppercase\parttitle}%
    \headrule\footrule\setfoot[][][]{\theauthor}{}{\thepage}}%
  \renewcommand{\parttitle}{#1}%
  \originalpart{#1}%
  \renewpagestyle{plain}{\sethead[\thepart.\thechapter\ \MakeUppercase\chaptertitle][][\theauthor]{\theauthor}{}{\thepart.\thechapter\ 
  \MakeUppercase\chaptertitle}\headrule\footrule\setfoot[\thepage][][\theauthor]{\theauthor}{}{\thepage}}%
  \pagestyle{plain}}

%## force page break if not enough room for subsections
\makeatletter
\newcommand{\savesubsectiontotal}{\ifnum\value{section}>0
  \immediate\write\@auxout{\string\newsubsectiontotal{\theHsection}{\arabic{subsection}}}%
\fi}
\newcommand{\subsectiontotal}[1]% \#1 = \theHsection
{\bgroup
  \dimen0=2\baselineskip
  \@ifundefined{subsectiontotal.#1}{}{%
    \count1=\csname subsectiontotal.#1\endcsname\relax
    \advance\count1 by 1
    \multiply\dimen0 by \count1}%
  \xdef\sectionspace{\the\dimen0}%
\egroup}

\let\originalchapter=\@chapter
\renewcommand{\@chapter}[2][]{% instead of \chapter*
  \savesubsectiontotal
  \originalchapter[#1]{#2}%
}
\makeatother

\newcommand{\sectionspace}{0pt}% reserve global name

\newcommand{\newsubsectiontotal}[2]% #1 = \theHsection, #2 = \arabic{subsection}
  {\expandafter\xdef\csname subsectiontotal.#1\endcsname{#2}}

\AtEndDocument{\savesubsectiontotal}


\let\originalsection=\section
\renewcommand{\section}[1]{% actually \section has two arguments, one optional
  \savesubsectiontotal
  \stepcounter{section}%
  \addtocontents{toc}{\protect\subsectiontotal{\theHsection}% compute needed space for next section
                      \protect\needspace{\protect\sectionspace}}%
  \addtocounter{section}{-1}% restore section counter
  \originalsection{#1}%
}
%## end of addition

\begin{document}
\begin{titlepage}
\begin{center}
{\Huge\bfseries Title\\}
\vspace{2\baselineskip}
{\huge\bfseries Subtitle\\}
\vspace{8\baselineskip}
{\LARGE Subsubtitle\\}
\vspace{2\baselineskip}
{\Large Subsubsubtitle\\}
\vspace{8\baselineskip}
{\large Author}
\end{center}
\end{titlepage}
\tableofcontents
\cleardoublepage
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{chapter.\theHchapter.cross}{#1}}

\part{The First Part}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\section{A section}

\subsection{A subsection}

\subsection{A subsection}

\section{A section}

Some text.

\section{A section}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

Some text.

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\part{The Second Part}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\part{The Third Part}

\chapter{A chapter}

\section{A section}

\clearpage
\end{document}

相关内容