1

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

To fix the headers all you needed to do was redefine \parttitle before running \originalpart.

To automate not breaking in the middle of a section required I had to redefine \section to automatically add a \needspace (needspace package) before each section. \sectiontotal computes the space needed assuming 2\baselineskip for each entry.

I take it the double spacing is deliberate.

\documentclass[12pt,a4paper]{book}
\raggedbottom
\usepackage{chngcntr}
\counterwithin*{chapter}{part}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{needspace}
\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]{%
  \savesubsectiontotal
  \cleardoublepage
  \renewpagestyle{plain}{\sethead[][][]{\theauthor}{}{\thepart\ \MakeUppercase\parttitle}%
    \headrule\footrule\setfoot[][][]{\theauthor}{}{\thepage}}%
  \renewcommand{\parttitle}{#1}%
  \originalpart{#1}%
  \renewpagestyle{plain}{\sethead[\thepart.\thechapter\ \MakeUppercase\chaptertitle][][\theauthor]{\theauthor}{}{\thepart.\thechapter\ 
  \MakeUppercase\chaptertitle}\headrule\footrule\setfoot[\thepage][][\theauthor]{\theauthor}{}{\thepage}}%
  \pagestyle{plain}}

%## force page break if not enough room for subsections
\makeatletter
\newcommand{\savesubsectiontotal}{\ifnum\value{section}>0
  \immediate\write\@auxout{\string\newsubsectiontotal{\theHsection}{\arabic{subsection}}}%
\fi}
\newcommand{\subsectiontotal}[1]% \#1 = \theHsection
{\bgroup
  \dimen0=2\baselineskip
  \@ifundefined{subsectiontotal.#1}{}{%
    \count1=\csname subsectiontotal.#1\endcsname\relax
    \advance\count1 by 1
    \multiply\dimen0 by \count1}%
  \xdef\sectionspace{\the\dimen0}%
\egroup}

\let\originalchapter=\@chapter
\renewcommand{\@chapter}[2][]{% instead of \chapter*
  \savesubsectiontotal
  \originalchapter[#1]{#2}%
}
\makeatother

\newcommand{\sectionspace}{0pt}% reserve global name

\newcommand{\newsubsectiontotal}[2]% #1 = \theHsection, #2 = \arabic{subsection}
  {\expandafter\xdef\csname subsectiontotal.#1\endcsname{#2}}

\AtEndDocument{\savesubsectiontotal}


\let\originalsection=\section
\renewcommand{\section}[1]{% actually \section has two arguments, one optional
  \savesubsectiontotal
  \stepcounter{section}%
  \addtocontents{toc}{\protect\subsectiontotal{\theHsection}% compute needed space for next section
                      \protect\needspace{\protect\sectionspace}}%
  \addtocounter{section}{-1}% restore section counter
  \originalsection{#1}%
}
%## end of addition

\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}
  • That's a great piece of code, thanks. I have to study it in order to extend this to \chapter, \chapter*, and \part. Note that \thesectionid was already defined by hyperref under the name \theHsection. – someone Jun 18 '17 at 13:28
  • It was late and I was in a hurry. – John Kormylo Jun 18 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.