2

I successfully added the prefix "Part" to my toc following this answer. However, it is not included in the hyperlink to that part of the document. Here is a MWE:

\documentclass[a4paper,twoside,openright,12pt]{book}
\usepackage[paper=a4paper]{geometry}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[onehalfspacing]{setspace}

% adding 'parts' to toc
\usepackage{titletoc}

\titlecontents{part}
[0pt]{\sffamily\bfseries\large\protect\addvspace{15pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{\partname~}
{}
[\addvspace{0.7ex}\titlerule\addvspace{1.5ex}]%remove rule if you like

% setting hyperref colors
\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=red}

\begin{document}

    \setcounter{tocdepth}{2}
    \tableofcontents
    \part{This is one part}
    \chapter{With its first chapter}
    \chapter{And its second}
    \part{This is the second part}
    \chapter{And another chapter}
    \section{with a little section}
\end{document}

Which produces the following toc: enter image description here As can be seen by the colors the link does start at the roman number and not at the prefix. Is there any way to include the prefix in the link as well?

  • hyperrefdoes not know that there is a prefix. – user31729 Jan 10 at 14:23
2

Here are three solutions.

With etoc package this solution don't use titletoc but etoc.

\documentclass[a4paper,twoside,openright,12pt]{book}
\usepackage[paper=a4paper]{geometry}
\usepackage[english]{babel}
\usepackage{hyperref}
\usepackage[onehalfspacing]{setspace}

\usepackage{etoc}

\newcommand{\mytableofcontents}{%
{\etocsetstyle{part}{}{\addvspace{15pt}\hrule\addvspace{1.5ex}}
{{\sffamily\bfseries\large\noindent\etoclink{\partname}~\etocnumber\quad\etocname\par}\addvspace{0.7ex}\hrule\addvspace{1.5ex}}{}
\etocsetstyle{chapter}{}{}
{\etocsavedchaptertocline{\numberline{\etocnumber}\etocname}{\etocpage}}{}
\etocsetstyle{section}{}{}
{\etocsavedsectiontocline{\numberline{\etocnumber}\etocname}{\etocpage}}{}
\etocsetstyle{subsection}{}{}
{\etocsavedsubsectiontocline{\numberline{\etocnumber}\etocname}{\etocpage}}{}
\etocstandarddisplaystyle
\etocsetnexttocdepth{2}
\tableofcontents}}
\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=red}

\begin{document}

    \mytableofcontents
    \part{This is one part}
    \chapter{With its first chapter}
    \chapter{And its second}
    \part{This is the second part}
    \chapter{And another chapter}
    \section{with a little section}
\end{document}

In tne next two solutions \partname had been removed from \titlecontents{part}

Patching \contentsline

\documentclass[a4paper,twoside,openright,12pt]{book}
\usepackage[paper=a4paper]{geometry}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[onehalfspacing]{setspace}

\usepackage{titletoc}

\titlecontents{part}
[0pt]{\sffamily\bfseries\large\protect\addvspace{15pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{}
{}
[\addvspace{0.7ex}\titlerule\addvspace{1.5ex}]%remove rule if you like

% setting hyperref colors
\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=red}
\usepackage{etoolbox}
\makeatletter 
\patchcmd{\contentsline}{{\Hy@tocdestname}{#2}\hyper@linkend}{{\Hy@tocdestname }{\expandafter\ifx\csname #1\endcsname\part\partname~\fi#2}\hyper@linkend}{}{\err} 
\makeatother

\begin{document}
    \setcounter{tocdepth}{2}
    \tableofcontents
    \part{This is one part}
    \chapter{With its first chapter}
    \chapter{And its second}
    \part{This is the second part}
    \chapter{And another chapter}
    \section{with a little section}
\end{document}

Patching \addcontentsline

\documentclass[a4paper,twoside,openright,12pt]{book}
\usepackage[paper=a4paper]{geometry}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[onehalfspacing]{setspace}

\usepackage{titletoc}

\titlecontents{part}
[0pt]{\sffamily\bfseries\large\protect\addvspace{15pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{}
{}
[\addvspace{0.7ex}\titlerule\addvspace{1.5ex}]%remove rule if you like

% setting hyperref colors
\hypersetup{colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=red}
\usepackage{etoolbox}
\newcommand{\intocpartname}{}
\patchcmd{\addcontentsline}{{#2}{#3}}{{#2}{\expandafter\ifx\csname #2\endcsname\part\protect\intocpartname\fi#3}}{}{\err} 

\begin{document}
    \setcounter{tocdepth}{2}
    \renewcommand{\intocpartname}{\partname~}
    \tableofcontents
    \part{This is one part}
    \chapter{With its first chapter}
    \chapter{And its second}
    \part{This is the second part}
    \chapter{And another chapter}
    \section{with a little section}
\end{document}
  • @CaptainNabla you're welcome :-) – touhami Jan 11 at 8:09

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.