自动修复丑陋的分页符目录

自动修复丑陋的分页符目录

我的目录中的分页符丑陋,这是我遇到的问题。章节的最后一部分在我的目录中有分页符,所以我用以下方法修复了它:\addtocontents{toc}{\protect\pagebreak}

然而,我对这个修复并不满意。虽然它没有出现问题,但 Latex 中没有可以修复此问题的自动命令吗?

有人知道这个修复是否可以自动完成吗?

这是我的代码:

\documentclass[a4paper,11.5pt,twoside, openleft]{book}
\makeatletter
% copied from latex.ltx added \nopagebreak
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
\nopagebreak%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5%
                        \kern-\p@\kern\p@}%
     \par}%
  \fi}

%copy from book but change final highpenalty to nobreak
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil
      \nobreak\hb@xt@\@pnumwidth{\hss #2%
                                 \kern-\p@\kern\p@}\par
%%%%%%%%%%%%%%%%%%%      \penalty\@highpenalty
    \nopagebreak%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    \endgroup
  \fi}

\makeatother




\renewcommand\cftchappagefont{\Large\bfseries}  %BOld fonts for contents
\renewcommand\cftchapfont{\Large\bfseries} %Table of contents Chapter style
\renewcommand{\cftsecfont}{\bfseries} %Table of contents section style
\setlength{\cftbeforetoctitleskip}{-1cm}
\setlength\cftaftertoctitleskip{1cm}

\begin{document}

答案1

这将在第 3 章的目录中途中断,如果没有重新定义,则不会阻止在节级标题之前分页(如果提供了测试文件,它将保存注释,以澄清正在使用书籍定义)

\documentclass{book}


\makeatletter
% copied from latex.ltx added \nopagebreak
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
\nopagebreak%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5%
                        \kern-\p@\kern\p@}%
     \par}%
  \fi}

%copy from book but change final highpenalty to nobreak
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil
      \nobreak\hb@xt@\@pnumwidth{\hss #2%
                                 \kern-\p@\kern\p@}\par
%%%%%%%%%%%%%%%%%%%      \penalty\@highpenalty
    \nopagebreak%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    \endgroup
  \fi}

\makeatother

\begin{document}

{\raggedbottom
\tableofcontents
}

\chapter{zzzz}
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\chapter{zzzz}
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\chapter{zzzz}
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\chapter{zzzz}
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}`<
\subsection{abc}abc
\subsection{abc}abc
\section{Zz}
\subsection{abc}abc
\subsection{abc}abc
\end{document}

相关内容