This question is mainly related to Hyperlinks and page breaks in a cross-referenced TOC
The answer provided by John Kormylo and our further discussion led to such a code change that I cannot continue asking the following question in comments :
1) I'd like to have \parttitle in some headers while using titlesec (I tried solutions given at Display \parttitle in header\footer with titlesec package with no effect).
The attempt below is mainly based on the accepted answer at How to get part name in LaTeX ?
However it seems that I have a wrong counter in my code since I get nothing displayed on the page header of the First Part, the "First Part" title displayed on the page header of the Second Part, the "Second Part" title displayed on the page header of the "Third Part", ...
The chapter counter reset at the beggining of parts (Incorrect hyperlinks when \part is used. How can I correct this? ) doesn't seem to have an impact.
2) I also have a (certainly hard) unanswered question in my former post :
How to get automatic page breaks in the TOC ? (The manual solution is given at Split the Table of Contents in two pages )
Here the code should automatically forbid page breaks in the TOC :
- between a level and a sub-level.
- if the TOC entries of a level can stand on a single TOC page.
Thanks in advance for your answers.
\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}
\chapter,\chapter*, and\part. Note that\thesectionidwas already defined byhyperrefunder the name\theHsection. – someone Jun 18 '17 at 13:28