我怎样才能将目录中的 Fancy 样式与 `\usepackage{hyperref}` 的使用结合起来?

我怎样才能将目录中的 Fancy 样式与 `\usepackage{hyperref}` 的使用结合起来?

因此,我对我的 Toc 进行了特殊的风格设计(你可以在这里找到源代码:精美的目录) 并且当我使用\usepackage{hyperref}Toc 主题时它不再起作用,但是一旦我删除\usepackage{hyperref}它就可以正常工作。我该如何在这两者之间进行管理?

答案1

出于好奇,我测试了答案提供的一个例子花式目录

添加之后\usepackage{hyperref}一切\begin{document}正常。

记住

(1)除特殊场合外,hyperref应为最后装载的包裹。

(2)如果您已经编译了该文件,添加hyperref并编译,同时保持相同的文件名,则目录将消失,直到您再次编译。

b

\documentclass[10pt,a4paper,openany]{book}
\usepackage{tocloft}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}

\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{array}

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\makeatletter


\tcbset{%
    chapternumberbox/.style={%
        colframe=black,
        colback={Gold1},
        colupper={darkgray},
        fontupper={\bfseries\normalsize},
        halign=center,
        valign=center,
        size=small,
        nobeforeafter,
        equal height group=chaptertoc,
        left skip=0pt,
        right skip=0pt,
    },
    chapternamebox/.style={%
        left skip=0pt,
        right skip=0pt,
        enhanced jigsaw,
        valign=center,
        interior style={left color={Gold2!70},right color={Gold3!15!white}},
        colframe={black},
        boxrule={1pt},
        colupper={black},
        size=small,
        fontupper={\bfseries\normalsize},
        nobeforeafter,
        equal height group=chaptertoc,
    }
}

\newtcolorbox{chapternumberboxinternal}[1][]{%
    chapternumberbox,
    #1
}

\newtcolorbox{chapternamebox}[1][]{%
    chapternamebox,
    #1,
}

\NewDocumentCommand{\chapternumberbox}{O{}m}{%
    \begin{chapternumberboxinternal}[#1,width={\cftchapnumwidth}]%-\kvtcb@left@rule-\kvtcb@right@rule}]
        #2%
    \end{chapternumberboxinternal}%
}



\NewDocumentCommand{\mychapternamebox}{O{}+mm}{%
    \begin{chapternamebox}[#1,width={\dimexpr\tocwidth-\cftchapnumwidth+\marginparsep}]
        \begin{tabular}{@{}p{\kvtcb@leftupper}@{}@{}p{\dimexpr\tocwidth-\cftchapnumwidth-\@pnumwidth-\kvtcb@leftupper-\marginparsep}@{}@{}R{\dimexpr\@pnumwidth}@{}@{}p{\kvtcb@rightupper}@{}}
            &  #2&#3
        \end{tabular}
    \end{chapternamebox}%
}


\usepackage{xpatch}
\usepackage{xprintlen}

\newlength\tocwidth
%
\renewcommand{\cftchapnumwidth}{2.5cm}
\setlength{\cftchapindent}{0pt}


%
\AtBeginDocument{%
    \setlength{\tocwidth}{\linewidth}
}

%

\xpatchcmd{\@chapter}{%
    \addcontentsline{toc}{chapter}%
    {\protect\numberline{\thechapter}#1}%
}{%
    \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
    \addcontentsline{toc}{chapter}{\chapternumberbox{\protect\chaptername\ \thechapter}\protect\mychapternamebox[]{#1}{\thepage}}%%
    \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
}{}{}
\makeatother

\usepackage{hyperref} % <<<<<<<<<<<<<<<<

\begin{document}
    
    \tableofcontents
    
    \chapter{First Chapter}
    \section{First Section}
    \section{Second Section}
    \chapter{Second Chapter}
    \section{First Section}
    \section{Second Section}
    \chapter[Theory on Brontosaurs]{Third Chapter with an excessively long title that should be avoided!}
\end{document}

相关内容