我尝试为目录的页码添加颜色。
我正在使用该包超链接以及选项:
\hypersetup{
colorlinks,
linkcolor=wine-stain % custom color
}
输出如下:
数字仍然是黑色的。
我应该怎么做才能使数字变成与各部分定义相同的颜色?
我试过了,\color{wine-stain}{\tableofcontents}
但还是不行,因为“内容“也被彩色化了,并且小节数字仍为黑色。
答案1
发生这种情况的原因是链接只是目录中条目的标题。页码本身不是超链接。添加选项
linktoc=all
到你的一套hyperref
选项,它应该链接章节名称和页码。
\documentclass{book}
\usepackage{hyperref,xcolor}% http://ctan.org/pkg/{hyperref,xcolor}
\definecolor{wine-stain}{rgb}{0.5,0,0}
\hypersetup{
colorlinks,
linkcolor=wine-stain,
linktoc=all
}
\begin{document}
\tableofcontents
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection{Second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\section{Second section}
\subsection*{An unnumbered first subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection*{An unnumbered second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\chapter{Second chapter}
\section{First section}
\subsection*{An unnumbered first subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection*{An unnumbered second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\section{Second section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\subsection{Second subsection}
\subsubsection{First subsubsection}
\subsubsection{Second subsubsection}
\end{document}
添加linktocpage
是不够的,因为部分单元没有超链接。当然,除非这是你首先想要的。也就是说,如果使用,它会在目录中提供超链接之间的互斥切换。事实上,与目录中的超链接相关的选项包括:
linktoc=none
:目录中没有超链接;linktoc=section
:仅章节标题有超链接;linktoc=page
:仅页码超链接;linktoc=all
:章节标题和页码均超链接;linktocpage
: 如同linktoc=page
键值linktoc
没有记录,但源自以下代码hyperref.dtx
:
\chardef\Hy@linktoc@none=0 %
\chardef\Hy@linktoc@section=1 %
\chardef\Hy@linktoc@page=2 %
\chardef\Hy@linktoc@all=3 %
\ifHy@linktocpage
\let\Hy@linktoc\Hy@linktoc@page
\else
\let\Hy@linktoc\Hy@linktoc@section
\fi
\define@key{Hyp}{linktoc}{%
\@ifundefined{Hy@linktoc@#1}{%
\Hy@Warning{%
Unexpected value `#1' of\MessageBreak
option `linktoc' instead of `none',\MessageBreak
`section', `page' or `all'%
}%
}{%
\expandafter\let\expandafter\Hy@linktoc
\csname Hy@linktoc@#1\endcsname
}%
}
答案2
这是另一个选项,使用tocloft
包及其\cftXpagefont
命令系列:
\documentclass{book}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{hyperref}
\colorlet{wine-stain}{orange!80!black}% just for the example
\hypersetup{
colorlinks,
linkcolor=wine-stain % custom color
}
\renewcommand\cftchappagefont{\color{wine-stain}}
\renewcommand\cftsecpagefont{\color{wine-stain}}
\renewcommand\cftsubsecpagefont{\color{wine-stain}}
\begin{document}
\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}
\end{document}
如果领导者(填充点)也应该接收颜色,那么您还可以重新定义\cftXleader
命令系列:
\documentclass{book}
\usepackage{xcolor}
\usepackage{tocloft}
\usepackage{hyperref}
\colorlet{wine-stain}{red!80!black}
\hypersetup{
colorlinks,
linkcolor=wine-stain % custom color
}
\renewcommand\cftchappagefont{\color{wine-stain}}
\renewcommand\cftsecpagefont{\color{wine-stain}}
\renewcommand\cftsecleader{\color{wine-stain}\cftdotfill{\cftsecdotsep}}
\renewcommand\cftsubsecpagefont{\color{wine-stain}}
\renewcommand\cftsubsecleader{\color{wine-stain}\cftdotfill{\cftsubsecdotsep}}
\begin{document}
\tableofcontents
\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}
\end{document}
请注意,这两种方法都只是添加了颜色。
答案3
\RedeclareSectionCommands[
toclinefill=\color{teal}{\quad\dotfill},%Color of dots set here,\hypersetup{linktotoc=all} not effect to dotscolor in \usepackage{hyperref} about texlive2019
tocraggedpagenumber=true,
tocindent=0pt,
tocentryformat=\color{olive}{\sffamily\fontsize{12pt}{18pt}\selectfont},%This will effect, need to set \hypersetup{colorlinks=false},otherwise, \hypersetup{linkcolor=teal} will effect to "entry of Contents", like section name, subsection name etc.
tocpagenumberformat=\color{purple}{\fontsize{12pt}{18pt}\selectfont}%
]{chapter,section,subsection}