GeneralAdditional Rule after Part ToC Entry causes bad Page Breaks

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
Bozack
Posts: 117
Joined: Wed Feb 06, 2008 4:21 pm

Additional Rule after Part ToC Entry causes bad Page Breaks

Post by Bozack »

I am working on my thesis (in the book class), and am using the tocloft package to make my table of contents pretty.

I would like to add a horizontal line beneath each Part section of the ToC, but here I run into a problem. I use this to add an \hrule after each part entry, and this looks great.

Code: Select all

\renewcommand{\cftpartafterpnum}{\cftparfillskip\\[-12pt]\hrule}
However, when the ToC goes over more than one page, it happens that the entry is on one page and the line ends up on the next page.

Here is a minimal example showing what happens:

Code: Select all

\documentclass{book}

\usepackage{tocloft}

\renewcommand{\cftpartafterpnum}{\cftparfillskip\\[-12pt]\hrule}
  
\begin{document}
  \tableofcontents
  
  \clearpage
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \part{bla}
  \section{bla}
  \section{bla}
\end{document}
This results in the bottom of the first page looking like
page1Capture.PNG
page1Capture.PNG (3.97 KiB) Viewed 19781 times
while the top of the second page looks like
page2Capture.PNG
page2Capture.PNG (6.27 KiB) Viewed 19781 times
Is there any way that the line can be forced to follow the part entry, instead of seemingly floating free on its own? Preferably the part entry should simply be moved to the next page along with the line.

I know it should be possible to fill in extra space in the ToC here and there, when I get closer to finishing the thesis and know how the sections will actually be positioned on the contents pages, but it would be really nice for this to simply happen automatically. ;)
Last edited by Bozack on Sat Dec 14, 2013 1:15 am, edited 1 time in total.
OS, LaTeX-system, editor: Arch Linux 64bit, TeXlive, Kile | Windows 10 Professional 64bit, MikTeX 4.9, TeXnicCenter 2.02 64bit

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Additional Rule after Part ToC Entry causes bad Page Breaks

Post by localghost »

Your redefinition of \cftpartafterpnum seems to be the culprit. It requires to prevent the page break between the concerned ToC entry and the following rule. And according to the explanations in the tocloft manual I think that \cftparfillskip is fairly wrong here.

The below approach works quite nice so that you can simply write your document without having to bother about your ToC.

Code: Select all

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{tocloft}
\usepackage{blindtext}  % not for use in actual document

\renewcommand{\cftpartafterpnum}{%
  \par\nopagebreak%   % begin new paragraph and prevent page break
  \vskip0.5ex\hrule%  % draw horizontal rule after small vertical skip
}

\begin{document}
  \tableofcontents

  \part{Heading on level -1 (part)}
    \blinddocument  % not for use in actual document

  \part{Heading on level -1 (part)}
    \blinddocument  % not for use in actual document

  \part{Heading on level -1 (part)}
    \blinddocument  % not for use in actual document

  \part{Heading on level -1 (part)}
    \blinddocument  % not for use in actual document
\end{document}
Click on "Open in writeLaTeX" in the head of the above code box to see the output instantly.


Thorsten
User avatar
Bozack
Posts: 117
Joined: Wed Feb 06, 2008 4:21 pm

Additional Rule after Part ToC Entry causes bad Page Breaks

Post by Bozack »

Ah, great solution! One thing that changes, though, is that now the line is a certain length below the text (as in, the actual extend of the letters) instead of being a length below the baseline of the text. This means that Part titles with "deep" letters (y,g,j, etc., anything that extends below the baseline) will have lines that are lower than otherwise, which to me is very visible:
Capture3.png
Capture3.png (3.5 KiB) Viewed 19477 times
Sorry if I'm using the wrong words here, I'm not enough of a typographer to know what things are called :oops:

Do you think there is some alternative to the \vskip that will go from baseline instead of the bottom of the word?
OS, LaTeX-system, editor: Arch Linux 64bit, TeXlive, Kile | Windows 10 Professional 64bit, MikTeX 4.9, TeXnicCenter 2.02 64bit
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Additional Rule after Part ToC Entry causes bad Page Breaks

Post by localghost »

I didn't take into account that a part heading of course can consist only of characters without descenders. So we need an invisible strut here to balance this.

Supplement the redefinition of \cftpartafterpnum with the \strut command to insert an invisible rule with a height of 0.7\baselineskip and a depth of 0.3\baselineskip. The vertical skip can then be reduced to 0.25ex in order not to have a too big gap between the heading and the rule.

Code: Select all

\documentclass[11pt]{book}
\usepackage[T1]{fontenc}
\usepackage{tocloft}
\usepackage{blindtext}  % not for use in actual document

\renewcommand{\cftpartafterpnum}{%
  \strut\par\nopagebreak%  % begin new paragraph and prevent page break
  \vskip0.25ex\hrule%      % draw horizontal rule after small vertical skip
}

\begin{document}
  \tableofcontents

  \part{Part title}
    \blinddocument  % not for use in actual document

  \part{Heading on level -1 (part)}
    \blinddocument  % not for use in actual document
\end{document}
User avatar
Bozack
Posts: 117
Joined: Wed Feb 06, 2008 4:21 pm

Re: Additional Rule after ToC Entry causes bad Page Breaks

Post by Bozack »

That is perfect! Thank you so much for your time! :D
OS, LaTeX-system, editor: Arch Linux 64bit, TeXlive, Kile | Windows 10 Professional 64bit, MikTeX 4.9, TeXnicCenter 2.02 64bit
Post Reply