% tocloft's part font color clashes with hyperref's linkcolor
\documentclass[a4paper,14pt]{book}
\usepackage[]{tocloft}
\usepackage{xcolor}
% I'd like to change the tocloft's cftpartfont color using:
\newcommand{\partfont}{\renewcommand{\cftpartfont}{\color{olive}}}
% But it doesn't change because I'm using:
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
colorlinks=true, % if I comment this line out,
linkcolor=red!80!black, % the cftpartfont functions right
urlcolor=green!50!black, % but in that case, for instance,
hyperfootnotes=false, % the footnotes' colorings become all black
hypertexnames, % which is undesirable
bookmarks=true % I'd like to use both hyperref's colorlinks
} % and tocloft's cftpartfont
% 5 suggestions I've tried to solve the tocloft - hyperref's colorlinks issue
% http://www.verycomputer.com/18_1efc8a75055d2fb4_1.htm (tocloft, tocbibind and hyperref together?)
% https://groups.google.com/g/comp.text.tex/c/Moj6kslHWVU (Combining Tocloft and hyperref)
% https://tex.stackexchange.com/questions/630002/hyperlinks-in-custom-list-of-via-tocloft
% https://tex.stackexchange.com/questions/239144/how-to-change-the-color-of-toc-part-font-using-tocloft
% https://www.latex4technics.com/?note=1SXK (suggests putting \tableofcontents into an environment like:
\newenvironment{MainTOC}{\partfont}{}
% None of the suggestions helped. I can't use those two options:
% tocloft's cftpartfont (assigning it a color) and hyperref's colorlinks=true
% How to use them together in this setup?
\begin{document}
\begin{MainTOC}
\tableofcontents
\end{MainTOC}
\part{first part}
\chapter{First chapter}
\part{second part}
\chapter{second chapter}
\end{document}
答案1
\DocumentMetadata
使用新的 pdf 管理(hyperref 使用改进的驱动程序) ,您可以在文档开头加载它,然后本地禁用 colorlinks。如果没有新的驱动程序,您必须重新定义内部 hyperref 命令。
请注意,选项bookmarks
和hyperfootnotes
无法在中设置hypersetup
,它们将被忽略,并且必须设置为包选项。
\DocumentMetadata
需要当前的 LaTeX
%\DocumentMetadata{} % uncomment to use the new pdfmanagement
\documentclass[a4paper,14pt]{book}
\usepackage[]{tocloft}
\usepackage{xcolor}
\makeatletter
\newcommand\disablelinkcolor{%
%\hypersetup{colorlinks=false}% with \DocumentMetadata{}
\def\HyColor@UseColor##1{}%
}
\makeatother
\newcommand{\partfont}{\renewcommand{\cftpartfont}{\disablelinkcolor\color{olive}}}
% But it doesn't change because I'm using:
\usepackage{hyperref}
\usepackage{bookmark}
\hypersetup{
colorlinks=true, % if I comment this line out,
linkcolor=red!80!black, % the cftpartfont functions right
urlcolor=green!50!black, % but in that case, for instance,
hypertexnames, % which is undesirable
} % and tocloft's cftpartfont
\newenvironment{MainTOC}{\partfont}{}
\begin{document}
\begin{MainTOC}
\tableofcontents
\end{MainTOC}
\part{first part}
\chapter{First chapter}
\label{blub}
\nameref{blub}
\part{second part}
\chapter{second chapter}
\end{document}
答案2
更聪明一点:
\colorlet{mylinkcolor}{red!80!black}
\newcommand{\partfont}{\renewcommand{\cftpartfont}{\colorlet{mylinkcolor}{olive}}}
...
\hypersetup{
...
linkcolor=mylinkcolor,
...
}
这是可行的,但是由于部分条目是在组内排版的,因此受到限制。