0

I notice my ToC is breaking in odd places. It breaks immediately after a Chapter head unless the whole chapter can fit on the current page. Also if a Chapter has too many items the list runs off the bottom on the page instead of breaking. How do I fix this?

In MWE as posted, the first page of the TOC is only part-filled and page breaks after the Chapter 2 chapter heading item, and the second page is more tightly packed. If you alter the number of sections in Chapter 2 the page split varies in position. So, some sort of 'balancing' of the amount of content is happening - though not in a good way.

This doesn't occur without the back-link code - but I want that feature!

I think the code solution here might be what I need to add, but I don't know where/how: Allow page breaks in the ToC after parts

MWE below:

\documentclass{book}
\usepackage[pdfpagemode={UseOutlines},bookmarks=true,bookmarksopen=true,
bookmarksopenlevel=0,bookmarksnumbered=true,hypertexnames=true, plainpages=false,
colorlinks,linkcolor={blue},citecolor={green},urlcolor={red},
pdfstartview={FitV},unicode,breaklinks=true,pdfpagelabels]{hyperref}

% back-link code from https://tex.stackexchange.com/questions/251904/hyperlinks-from-chapter-and-section-headings-back-to-the-table-of-contents/

\makeatletter
% configure chapter back-links
\let\hyperchapter\chapter
\def\chapter{\@ifstar\starchapter\mychapter}
\def\starchapter{\hyperchapter*}
\newcommand{\mychapter}[2][\@empty]% #1=optional (toc and top of page), #2=title
{\ifx#1\@empty \hyperchapter[#2]{\hyperlink{toc.chapter.\thechapter}{#2}}
    \else \hyperchapter[#1]{\hyperlink{toc.chapter.\thechapter}{#2}}
    \fi}
% configure section back-links
\let\hypersection\section
\def\section{\@ifstar\starsection\mysection}
\def\starsection{\hypersection*}
\newcommand{\mysection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersection[#2]{\hyperlink{toc.section.\thesection}{#2}}
    \else \hypersection[#1]{\hyperlink{toc.section.\thesection}{#2}}
    \fi}
% configure subsection back-links
\let\hypersubsection\subsection
\def\subsection{\@ifstar\starsubsection\mysubsection}
\def\starsubsection{\hypersubsection*}
\newcommand{\mysubsection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersubsection[#2]{\hyperlink{toc.subsection.\thesubsection}{#2}}
    \else \hypersubsection[#1]{\hyperlink{toc.subsection.\thesubsection}{#2}}
    \fi}
% configure subsubsection back-links
\let\hypersubsubsection\subsubsection
\def\subsubsection{\@ifstar\starsubsubsection\mysubsubsection}
\def\starsubsubsection{\hypersubsubsection*}
\newcommand{\mysubsubsection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersubsubsection[#2]{\hyperlink{toc.subsubsection.\thesubsubsection}{#2}}
    \else \hypersubsubsection[#1]{\hyperlink{toc.subsubsection.\thesubsubsection}{#2}}
    \fi}
% close out
\makeatother

\begin{document}

%%%---Set up clickable section headers----------
\let\hypercontentsline=\contentsline
\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}}
%%%---End set up clickable section headers----------

\tableofcontents

\chapter{The First}
\section{ant}
\subsection{bee}
\subsubsection{cow}
\section{dog}
\subsection{eel}
\subsubsection{fly}
\chapter{The Second}
\section{ant1}
\subsection{bee1}
\subsubsection{cow1}
\section{dog1}
\subsection{eel1}
\subsubsection{fly1}
\section{ant2}
\subsection{bee2}
\subsubsection{cow2}
\section{dog2}
\subsection{eel2}
\subsubsection{fly2}
\section{ant3}
\subsection{bee3}
\subsubsection{cow3}
\section{dog3}
\subsection{eel3}
\subsubsection{fly3}
\section{ant4}
\subsection{bee4}
\subsubsection{cow4}
\section{dog4}
\subsection{eel4}
\subsubsection{fly4}
\section{ant41}
\subsection{bee41}
\subsubsection{cow41}
\section{dog41}
\subsection{eel41}
\subsubsection{fly41}
\section{ant42}
\subsection{bee42}
\subsubsection{cow42}
\section{dog42}
\subsection{eel42}
\subsubsection{fly42}
\section{ant43}
\subsection{bee43}
\subsubsection{cow43}
\section{dog43}
\subsection{eel43}
\subsubsection{fly43}
\chapter{The Third}
\section{ant5}
\subsection{bee5}
\subsubsection{cow5}
\section{dog5}
\subsection{eel5}
\subsubsection{fly5}
\section{ant6}
\subsection{bee6}
\subsubsection{cow6}
\section{dog6}
\subsection{eel6}
\subsubsection{fly6}
\section{ant7}
\subsection{bee7}
\subsubsection{cow7}
\section{dog7}
\subsection{eel7}
\subsubsection{fly7}
\section{ant8}
\subsection{bee8}
\subsubsection{cow8}
\section{dog8}
\subsection{eel8}
\subsubsection{fly8}

\end{document}
1
  • Please post a MWE of your requirement...
    – MadyYuvi
    Sep 5, 2019 at 5:15

1 Answer 1

2
[...]
\subsubsection{fly}

\addtocontents{toc}{\protect\newpage}
\chapter{The Second}
[...]
6
  • 1
    Got it inserted but now have a different problem, CH1 items only on TOC page 1, plus it seems I have to add this manually for each chapter? In my real doc the content is split across lots of included docs so hard to know where to put code. Basically, I just want the TOC to paginate like norrmal text as it does without the back-link code.
    – mwra
    Sep 5, 2019 at 7:34
  • It should be done at the very end of your document when you are finished. Then you can insert it.
    – user187802
    Sep 5, 2019 at 7:38
  • Sorry, I don't understand - end in position or when fished writing. Plus doesn't the above only affect Chapter "The Second". I think what's needed is a similar applied to all items at all 4 levels of TOC item. True?
    – mwra
    Sep 5, 2019 at 7:53
  • Ah, added the extra code line to MWE just before \end{document} and now each chapter starts on a new TOC page. Then added more items to Ch2 and now Ch2 head only is back onToC page 1. Odd!
    – mwra
    Sep 5, 2019 at 8:17
  • No, at "the end" means: finish your document, then look into the TOC if it needs some manual modifications.
    – user187802
    Sep 5, 2019 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .