将未编号部分添加到目录时,书签级别不正确

将未编号部分添加到目录时,书签级别不正确

下列的此解决方案,我已成功将未编号章节添加到目录 (ToC) 中并将字体设为斜体。但是,我的未编号章节由未编号部分组成,我也想将其以斜体添加到目录中。我目前拥有的代码(如下)成功完成了此操作。我的问题是,未编号部分生成的书签(如在我的 PDF 阅读器中看到的)处于章节级别,而不是部分级别。如何确保书签生成在正确的级别?

\documentclass{report}

\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\let\l@chapterstar\l@chapter
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}
\makeatother

\newcommand{\cftchapstarfont}{\cftchapfont\itshape}
\newcommand{\cftchapstarpagefont}{\cftchappagefont\itshape}

\makeatletter
\let\l@sectionstar\l@section
\patchcmd{\l@sectionstar}{\cftsecfont}{\cftsecstarfont}{}{}
\patchcmd{\l@sectionstar}{#2}{\cftsecstarpagefont #2}{}{}
\makeatother

\newcommand{\cftsecstarfont}{\cftsecfont\itshape}
\newcommand{\cftsecstarpagefont}{\cftsecpagefont\itshape}   

\begin{document}

\tableofcontents

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapterstar}{Unnumbered chapter}

\section*{Unnumbered section}
\addcontentsline{toc}{sectionstar}{Unnumbered section}

\end{document}

答案1

添加

\def\toclevel@chapterstar{0}

\def\toclevel@sectionstar{1}

回到序言。

例子:

\documentclass{report}
\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\let\l@chapterstar\l@chapter
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}
\def\toclevel@chapterstar{0}% <- added
\makeatother

\newcommand{\cftchapstarfont}{\cftchapfont\itshape}
\newcommand{\cftchapstarpagefont}{\cftchappagefont\itshape}

\makeatletter
\let\l@sectionstar\l@section
\patchcmd{\l@sectionstar}{\cftsecfont}{\cftsecstarfont}{}{}
\patchcmd{\l@sectionstar}{#2}{\cftsecstarpagefont #2}{}{}
\def\toclevel@sectionstar{1}% <- added
\makeatother

\newcommand{\cftsecstarfont}{\cftsecfont\itshape}
\newcommand{\cftsecstarpagefont}{\cftsecpagefont\itshape}   

\begin{document}
\tableofcontents
\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapterstar}{Unnumbered chapter}
\section*{Unnumbered section}
\addcontentsline{toc}{sectionstar}{Unnumbered section}
\end{document}

在此处输入图片描述

相关内容