3

The TOC entries requirements are :

  1. The TOC page breaks defined by (2) and (3) should be automatic (no need to manually add \addtocontents{toc}{\protect\newpage} as described at Split the Table of Contents in two pages )
  2. Breaks should be forbidden between a level and a sublevel.
  3. As long as (2) is satisfied, a level should have the minimal number of breaks, with the highest priority going to the deeper level, that is :
    • (3.1) as long as (2) is satisfied, a level has N breaks if it cannot stand on less than (N+1) pages without an Overfull \vbox.
    • (3.2) the (Max Depth) level breaking has no meaning anyway.
    • (3.3) for X ranging from (Max Depth-1) to (Min Depth), the unavoidable breaks of an X-depth level are :
      • (3.3.1) at lines which don't conflict with (2).
      • (3.3.2) at lines which don't add any break to any deeper level.
      • (3.3.3) at the lowest lines preventing an Overfull \vbox.
    • (3.4) there is no break between "Contents" and the following item since it would conflict with (2).
  4. The cross-ref between the level titles and the TOC should be accurate.

The problem is :

  • Conditions (3.1) and (3.3.2) don't work since it should break the TOC pages before (section 1.32) and before (part 2).
  • Condition (4) doesn't work from chapter titles to the TOC entries since you always will be directed to the first sub-level TOC entry.
  • Condition (4) doesn't work from other level titles to the TOC entries if they start a new page in the TOC (see subsection 1.32.2) without manually adding \addtocontents{toc}{\protect\newpage}.

The \titleformat{\chapter} command is placed after \begin{document} in order to "Contents" not to act as a dead link.

Indeed that link would be quite undead here since (4) doesn't work for chapter titles.

Placing \titleformat{\chapter} in the preamble won't solve the problem anyway.

I already tried the \Hy@raisedlink soultion described at Vertical position after navigating to hyperref hypertargets from hyperlinks seems too low

The result was an error maybe due to hyphenation as described at `\Hy@raisedlink` block hyphenation and perturbate line breaking

For chapters, the problem seems to be due to the way the .toc file is written since the \hypertarget {\the part.\thechapter}{} should be written before the corresponding \contentsline (.toc code given below).

Thanks in advance for your help.

PS : Not sure this working example is really minimal since I also loaded extra packages that may interfer with titles, that is :

Here is the .tex file :

\documentclass[12pt,a4paper]{book}
\raggedbottom
\setlength{\headheight}{14.49998pt}
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\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}}
\etoctocstyle{1}{Contents}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhf[LO,RE]{Author}
\fancypagestyle{plain}{\pagestyle{fancy}}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{\headrulewidth}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[clearempty,explicit]{titlesec}
\renewcommand{\partmark}[1]{\markboth{\thepart. #1}{}}
\renewcommand{\chaptermark}[1]{\markright{#1}{}}
\renewcommand{\sectionmark}[1]{}
\titleformat{\part}{\Huge\bfseries}{\thepart}{2\wordsep}{\hyperlink{\thepart}{#1}\addtocontents{toc}{\protect\hypertarget{\thepart}{}}}
\titleformat{\section}{\Large\bfseries}{\thesection}{2\wordsep}{\hyperlink{\theHsection}{#1}\addtocontents{toc}{\protect\hypertarget{\theHsection}{}}}
\titleformat{\subsection}{\large\bfseries}{\thesubsection}{2\wordsep}{\hyperlink{\theHsubsection}{#1}\addtocontents{toc}{\protect\hypertarget{\theHsubsection}{}}}
\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}
\begin{document}
\titleformat{\chapter}{\huge\bfseries}{}{0pt}{Contents\markright{#1}{}}{}
\fancyhead[LE,RO]{\MakeUppercase\rightmark}
\tableofcontents
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{\theHchapter}{#1}\addtocontents{toc}{\protect\hypertarget{\theHchapter}{}}}

\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The First Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\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}

\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}

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

\subsection{A subsection}

\subsection{A subsection}

\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}

\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}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The Second Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\end{document}

and here is the .toc file :

\@ifundefined {etoctocstyle}{\let \etoc@startlocaltoc \@gobble \let \etoc@settocdepth \@gobble \let \etoc@depthtag \@gobble }{}
\hypertarget {I}{}
\contentsline {part}{I\hspace {1em}The First Part}{5}{part.1}
\contentsline {chapter}{\numberline {1}A chapter}{7}{chapter.I.1}
\hypertarget {I.1}{}
\hypertarget {I.1.1}{}
\contentsline {section}{\numberline {1.1}A section}{7}{section.I.1.1}
\hypertarget {I.1.2}{}
\contentsline {section}{\numberline {1.2}A section}{7}{section.I.1.2}
\hypertarget {I.1.3}{}
\contentsline {section}{\numberline {1.3}A section}{7}{section.I.1.3}
\hypertarget {I.1.4}{}
\contentsline {section}{\numberline {1.4}A section}{7}{section.I.1.4}
\hypertarget {I.1.5}{}
\contentsline {section}{\numberline {1.5}A section}{7}{section.I.1.5}
\hypertarget {I.1.6}{}
\contentsline {section}{\numberline {1.6}A section}{7}{section.I.1.6}
\hypertarget {I.1.7}{}
\contentsline {section}{\numberline {1.7}A section}{7}{section.I.1.7}
\hypertarget {I.1.8}{}
\contentsline {section}{\numberline {1.8}A section}{7}{section.I.1.8}
\hypertarget {I.1.9}{}
\contentsline {section}{\numberline {1.9}A section}{7}{section.I.1.9}
\hypertarget {I.1.10}{}
\contentsline {section}{\numberline {1.10}A section}{7}{section.I.1.10}
\hypertarget {I.1.11}{}
\contentsline {section}{\numberline {1.11}A section}{7}{section.I.1.11}
\hypertarget {I.1.12}{}
\contentsline {section}{\numberline {1.12}A section}{7}{section.I.1.12}
\hypertarget {I.1.13}{}
\contentsline {section}{\numberline {1.13}A section}{7}{section.I.1.13}
\hypertarget {I.1.14}{}
\contentsline {section}{\numberline {1.14}A section}{7}{section.I.1.14}
\hypertarget {I.1.15}{}
\contentsline {section}{\numberline {1.15}A section}{8}{section.I.1.15}
\hypertarget {I.1.16}{}
\contentsline {section}{\numberline {1.16}A section}{8}{section.I.1.16}
\hypertarget {I.1.17}{}
\contentsline {section}{\numberline {1.17}A section}{8}{section.I.1.17}
\hypertarget {I.1.18}{}
\contentsline {section}{\numberline {1.18}A section}{8}{section.I.1.18}
\hypertarget {I.1.19}{}
\contentsline {section}{\numberline {1.19}A section}{8}{section.I.1.19}
\hypertarget {I.1.20}{}
\contentsline {section}{\numberline {1.20}A section}{8}{section.I.1.20}
\hypertarget {I.1.21}{}
\contentsline {section}{\numberline {1.21}A section}{8}{section.I.1.21}
\hypertarget {I.1.22}{}
\contentsline {section}{\numberline {1.22}A section}{8}{section.I.1.22}
\hypertarget {I.1.23}{}
\contentsline {section}{\numberline {1.23}A section}{8}{section.I.1.23}
\hypertarget {I.1.24}{}
\contentsline {section}{\numberline {1.24}A section}{8}{section.I.1.24}
\hypertarget {I.1.25}{}
\contentsline {section}{\numberline {1.25}A section}{8}{section.I.1.25}
\hypertarget {I.1.26}{}
\contentsline {section}{\numberline {1.26}A section}{8}{section.I.1.26}
\hypertarget {I.1.27}{}
\contentsline {section}{\numberline {1.27}A section}{8}{section.I.1.27}
\hypertarget {I.1.28}{}
\contentsline {section}{\numberline {1.28}A section}{8}{section.I.1.28}
\hypertarget {I.1.29}{}
\contentsline {section}{\numberline {1.29}A section}{8}{section.I.1.29}
\hypertarget {I.1.30}{}
\contentsline {section}{\numberline {1.30}A section}{9}{section.I.1.30}
\hypertarget {I.1.31}{}
\contentsline {section}{\numberline {1.31}A section}{9}{section.I.1.31}
\hypertarget {I.1.32}{}
\contentsline {section}{\numberline {1.32}A section}{9}{section.I.1.32}
\hypertarget {I.1.32.1}{}
\contentsline {subsection}{\numberline {1.32.1}A subsection}{9}{subsection.I.1.32.1}
\hypertarget {I.1.32.2}{}
\contentsline {subsection}{\numberline {1.32.2}A subsection}{9}{subsection.I.1.32.2}
\hypertarget {I.1.33}{}
\contentsline {section}{\numberline {1.33}A section}{9}{section.I.1.33}
\hypertarget {I.1.34}{}
\contentsline {section}{\numberline {1.34}A section}{9}{section.I.1.34}
\hypertarget {I.1.35}{}
\contentsline {section}{\numberline {1.35}A section}{9}{section.I.1.35}
\hypertarget {I.1.36}{}
\contentsline {section}{\numberline {1.36}A section}{9}{section.I.1.36}
\hypertarget {I.1.37}{}
\contentsline {section}{\numberline {1.37}A section}{9}{section.I.1.37}
\hypertarget {I.1.38}{}
\contentsline {section}{\numberline {1.38}A section}{9}{section.I.1.38}
\hypertarget {I.1.39}{}
\contentsline {section}{\numberline {1.39}A section}{9}{section.I.1.39}
\hypertarget {I.1.40}{}
\contentsline {section}{\numberline {1.40}A section}{9}{section.I.1.40}
\hypertarget {I.1.41}{}
\contentsline {section}{\numberline {1.41}A section}{9}{section.I.1.41}
\hypertarget {I.1.42}{}
\contentsline {section}{\numberline {1.42}A section}{9}{section.I.1.42}
\contentsline {chapter}{\numberline {2}A chapter}{11}{chapter.I.2}
\hypertarget {I.2}{}
\hypertarget {I.2.1}{}
\contentsline {section}{\numberline {2.1}A section}{11}{section.I.2.1}
\hypertarget {I.2.2}{}
\contentsline {section}{\numberline {2.2}A section}{11}{section.I.2.2}
\hypertarget {I.2.3}{}
\contentsline {section}{\numberline {2.3}A section}{11}{section.I.2.3}
\hypertarget {I.2.4}{}
\contentsline {section}{\numberline {2.4}A section}{11}{section.I.2.4}
\hypertarget {I.2.5}{}
\contentsline {section}{\numberline {2.5}A section}{11}{section.I.2.5}
\hypertarget {I.2.6}{}
\contentsline {section}{\numberline {2.6}A section}{11}{section.I.2.6}
\hypertarget {I.2.7}{}
\contentsline {section}{\numberline {2.7}A section}{11}{section.I.2.7}
\hypertarget {I.2.8}{}
\contentsline {section}{\numberline {2.8}A section}{11}{section.I.2.8}
\hypertarget {I.2.9}{}
\contentsline {section}{\numberline {2.9}A section}{11}{section.I.2.9}
\hypertarget {I.2.10}{}
\contentsline {section}{\numberline {2.10}A section}{11}{section.I.2.10}
\hypertarget {I.2.11}{}
\contentsline {section}{\numberline {2.11}A section}{11}{section.I.2.11}
\hypertarget {I.2.12}{}
\contentsline {section}{\numberline {2.12}A section}{11}{section.I.2.12}
\hypertarget {I.2.13}{}
\contentsline {section}{\numberline {2.13}A section}{11}{section.I.2.13}
\hypertarget {I.2.14}{}
\contentsline {section}{\numberline {2.14}A section}{11}{section.I.2.14}
\hypertarget {I.2.15}{}
\contentsline {section}{\numberline {2.15}A section}{12}{section.I.2.15}
\hypertarget {I.2.16}{}
\contentsline {section}{\numberline {2.16}A section}{12}{section.I.2.16}
\hypertarget {I.2.17}{}
\contentsline {section}{\numberline {2.17}A section}{12}{section.I.2.17}
\hypertarget {I.2.18}{}
\contentsline {section}{\numberline {2.18}A section}{12}{section.I.2.18}
\hypertarget {I.2.19}{}
\contentsline {section}{\numberline {2.19}A section}{12}{section.I.2.19}
\hypertarget {I.2.20}{}
\contentsline {section}{\numberline {2.20}A section}{12}{section.I.2.20}
\hypertarget {I.2.21}{}
\contentsline {section}{\numberline {2.21}A section}{12}{section.I.2.21}
\hypertarget {I.2.22}{}
\contentsline {section}{\numberline {2.22}A section}{12}{section.I.2.22}
\hypertarget {I.2.23}{}
\contentsline {section}{\numberline {2.23}A section}{12}{section.I.2.23}
\hypertarget {II}{}
\contentsline {part}{II\hspace {1em}The Second Part}{13}{part.2}
\contentsline {chapter}{\numberline {1}A chapter}{15}{chapter.II.1}
\hypertarget {II.1}{}
\hypertarget {II.1.1}{}
\contentsline {section}{\numberline {1.1}A section}{15}{section.II.1.1}
\hypertarget {II.1.2}{}
\contentsline {section}{\numberline {1.2}A section}{15}{section.II.1.2}
\hypertarget {II.1.3}{}
\contentsline {section}{\numberline {1.3}A section}{15}{section.II.1.3}
\hypertarget {II.1.4}{}
\contentsline {section}{\numberline {1.4}A section}{15}{section.II.1.4}
\contentsline {chapter}{\numberline {2}A chapter}{17}{chapter.II.2}
\hypertarget {II.2}{}
\hypertarget {II.2.1}{}
\contentsline {section}{\numberline {2.1}A section}{17}{section.II.2.1}
0

1 Answer 1

4

The key was to modify \contentsline to add the \hypertarget on the same line. Having \hypertarget aim one line too low is a known problem in certain environments (like tabular), which can be fixed using \raisebox or \vadjust pre{...}. See this question.

Note: hyperref modifies \addtocontents and \contentsline to include the hypertarget name. I simply added .cross to this name for the cross reference.

\documentclass[12pt,a4paper]{book}
\raggedbottom
\setlength{\headheight}{14.49998pt}
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\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}}
\etoctocstyle{1}{Contents}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhf[LO,RE]{Author}
\fancypagestyle{plain}{\pagestyle{fancy}}
\pagestyle{fancy}
\renewcommand{\footrulewidth}{\headrulewidth}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[clearempty,explicit]{titlesec}
\renewcommand{\partmark}[1]{\markboth{\thepart. #1}{}}
\renewcommand{\chaptermark}[1]{\markright{#1}{}}
\renewcommand{\sectionmark}[1]{}
\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\oldcontentsline=\contentsline
\def\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{\raisebox{\baselineskip}{\hypertarget{#4.cross}{\relax}}#3}{#4}}

\begin{document}
\fancyhead[LE,RO]{\MakeUppercase\rightmark}
\titleformat{\chapter}{\huge\bfseries}{}{0pt}{Contents\markright{#1}}
\tableofcontents
\titleformat{\chapter}{\huge\bfseries}{\thechapter}{2\wordsep}{\hyperlink{chapter.\theHchapter.cross}{#1}}

\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The First Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\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}

\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}

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

\subsection{A subsection}

\subsection{A subsection}

\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}

\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}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

%\addtocontents{toc}{\protect\newpage}
\cleardoublepage
\fancyhead[LE,RO]{\MakeUppercase\leftmark}
\part{The Second Part}
\fancyhead[LE,RO]{\thepart.\thechapter. \MakeUppercase\rightmark}

\chapter{A chapter}

\section{A section}

\section{A section}

\section{A section}

\section{A section}

\chapter{A chapter}

\section{A section}

\end{document}
11
  • Many thanks for your answer. It solves the second and the third problems, thus voted up. However I red at [link]tex.stackexchange.com/questions/145314/… and in other posts that the use of \renewcommand was preferable to the use Tex commands such as \let and \def. Is there a way to do that ? I am trying to do it but for now I am getting a "memory excess", probably a bad loop.
    – someone
    Jun 2, 2017 at 14:10
  • 1
    I was looking at the definition of \contentsline inside hyperref at the time, but \renewcommand is fine. OTOH, \let is different. You could achieve the same effect using etoolkit, but frankly that package scares me silly. Jun 2, 2017 at 23:33
  • Isn't it etoolbox instead of etookit ?
    – someone
    Jun 8, 2017 at 14:48
  • Anyway you're right about the fact that \def\contentsline#1#2#3#4 can be replaced by \renewcommand{\contentsline}[4] in order to obtain the same result. Note that in both cases the link from the TOC to the last chapter isn't very accurate. That problem can be solved by adding either \clearpage or \cleardoublepage before \end{document}. I'm still looking for a solution to the first problem : automatic page breaks in the TOC.
    – someone
    Jun 8, 2017 at 14:56
  • 1
    Using a \strut is easier and basically equivalent. (e.g. {\strut #1}) Jun 15, 2017 at 13:13

You must log in to answer this question.

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