Theses, Books, Title pagesHow to make the parts of document clickable in the table of contents?

Classicthesis, Bachelor and Master thesis, PhD, Doctoral degree
Post Reply
programtica
Posts: 1
Joined: Wed Sep 28, 2022 2:24 pm

How to make the parts of document clickable in the table of contents?

Post by programtica »

How can I make the part of the document clickable from table of contents in book class?

Despite of the fact I was able to make the chapters and sections clickable (as You can see in the attached picture), when it comes to say about parts, brought me a trouble... :cry:
Picture with current situation
Picture with current situation
problem.png (181.14 KiB) Viewed 19310 times
Here is the current code:

Code: Select all

\documentclass[12pt, a4paper]{book}

%\usepackage[utf8]{inputenc} - not in lualatex!
\usepackage[polish]{babel}
\usepackage{polski}
\usepackage{indentfirst}

\usepackage[margin={2.5cm,2.5cm,2.5cm,2.5cm}]{geometry}

\usepackage[nottoc]{tocbibind}
\usepackage{tocloft}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{%
    \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%%
}{%
    \addtocontents{toc}{\bfseries\protect\centering\Large\thepart.\hspace{1em}#1\par}%
}{}{}
\makeatother
\usepackage[bookmarks=true]{hyperref}
\usepackage{bookmark}
\usepackage{lipsum}

\makeatletter
\def\contentsline#1#2#3#4{%
    \begingroup
    \Hy@safe@activestrue
    \edef\x{\endgroup
        \def\noexpand\Hy@tocdestname{#4}%
    }\x
    \csname l@#1\endcsname{%
        %\hyper@linkstart{link}{\Hy@tocdestname}{#2}\hyper@linkend
        #2%
    }{%
        %\hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
        #3%
    }%
}
\makeatother

\makeatletter
% Update ToC hyperlinks for Chapters
\patchcmd{\l@chapter}% <cmd>
{{\cftchapfont #1}}% <search>
{\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftchapfont #1}}% <replace>
{}{}% <success><failure>
\patchcmd{\cftchapfillnum}{\par}{\hyper@linkend\par}{}{}
% Update ToC hyperlinks for Sections
\patchcmd{\l@section}% <cmd>
{{\cftsecfont #1}}% <search>
{\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftsecfont #1}}% <replace>
{}{}% <success><failure>
\patchcmd{\cftsecfillnum}{\par}{\hyper@linkend\par}{}{}


%-| | - for parts - here doesn't work... perhaps...
%\patchcmd{\l@part}% <cmd>
%{{\cftpartfont #1}}% <search>
%{\hyper@linkstart{link}{\Hy@tocdestname}{}{\cftpartfont #1}}% <replace>
%{}{}% <success><failure>
%\patchcmd{\cftpartfillnum}{\par}{\hyper@linkend\par}{}{}

\makeatother

\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchappresnum}{\bfseries}
\renewcommand\cftsecpagefont{\bfseries\normalsize}
\renewcommand{\cftchapleader}{\normalfont\cftdotfill{\cftchapdotsep}}
\renewcommand\cftchappagefont{\bfseries\normalsize}
\setlength{\cftbeforechapskip}{0.05cm}
\setlength{\cftchapnumwidth}{1cm}
\renewcommand{\cftsecfont}{\normalfont\normalsize}
\renewcommand{\cftchapfont}{\normalfont\large}
\renewcommand{\cftpartfont}{\bfseries\Huge}

\makeatletter\@addtoreset{chapter}{part}\makeatother%
\renewcommand{\thesection}{\thechapter.\arabic{section}.}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@makechapterhead}% <cmd>
{\@chapapp\space\thechapter\par\nobreak\vskip 20\p@}% <search>
{\Huge\space}% <replace>
{}{}% <success><failure>
\makeatother

\begin{document}
    \tableofcontents
    \part{Część pierwsza}
    \chapter{Rozdział 1}
    \lipsum[1]
    \section{Sekcja 1}
    \lipsum[2]
    \chapter{Rozdział 2}
    \lipsum[2]
    \section{Sekcja 2}
    \lipsum[3]
    \chapter{Rozdział 3}
    \lipsum[3]
    \section{Sekcja 3}
    \lipsum[4]
    \addtocontents{toc}{\vspace{0.5\baselineskip}}
    \part{Część druga}
    \chapter{Rozdział 1}
    \lipsum[1]
    \chapter{Rozdział 2}
    \lipsum[2]
    \chapter{Rozdział 3}
    \lipsum[3]
    
    \bookmarksetup{startatroot}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply