我成功地将前缀“Part”添加到我的目录中,如下所示这个答案。但是,它没有包含在文档该部分的超链接中。以下是 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}
答案1
以下是三种解决方案。
带有 etoc 包titletoc
但是这个解决方案不使用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}
接下来的两个解决方案 \partname
已被删除\titlecontents{part}
修补\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}
修补\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}