![在报告的目录中添加“Chap”字样后,目录中整个章节的颜色会改变吗?](https://linux22.com/image/459156/%E5%9C%A8%E6%8A%A5%E5%91%8A%E7%9A%84%E7%9B%AE%E5%BD%95%E4%B8%AD%E6%B7%BB%E5%8A%A0%E2%80%9CChap%E2%80%9D%E5%AD%97%E6%A0%B7%E5%90%8E%EF%BC%8C%E7%9B%AE%E5%BD%95%E4%B8%AD%E6%95%B4%E4%B8%AA%E7%AB%A0%E8%8A%82%E7%9A%84%E9%A2%9C%E8%89%B2%E4%BC%9A%E6%94%B9%E5%8F%98%E5%90%97%EF%BC%9F.png)
参考包含 MWE 的良好工作解决方案如何在课堂报告的目录中的章节编号前添加单词“Chap”?在报告类的目录中,在章节号前添加单词“Chap”,如果我想将目录中的整个“Chap..”行都涂成蓝色,我该怎么做?只有单词“Chap”是蓝色的,而不是其标题。我希望整个“Chap”目录行都变成蓝色。
使用 \color{blue} 修改下面目录中启用“章节...”字样的行
\newlength\mylength
\renewcommand\cftchappresnum{\color{blue}{Chapter~}}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
更新 1:(2022/12/28) 使用 hyperref 时不起作用
查看我的 MWE
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
colorlinks=true,
citecolor=PineGreen,
linkcolor=red,
urlcolor=blue}
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{\bf\color{red}Appendix~}% changed <<<<
\protect\renewcommand{\protect\cftchapfont}{\bf\color{red}}
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
\newlength\mylength
\renewcommand\cftchappresnum{\bf\color{blue}Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\renewcommand{\cftchapfont}{\bf\color{blue}}
\renewcommand\cftchapaftersnum{:}
\begin{document}
\tableofcontents
\chapter{Intro}
\chapter{Test}
\appendix
\chapter{Conclusions}
\end{document}
更新 2(2022 年 12 月 29 日)
好的,我已经能够使用来自@David Purton 的回复将目录中的整个章节行变为蓝色,但现在其他条目(例如致谢、摘要、参考书目)不属于章节,但需要以相同的格式(想想目录中的缩进和间距)添加到目录中,\addcontentsline{toc}{chapter}{}
我希望这些条目变为红色(附录目录行变为红色就可以了)。
查看我的新 MWE
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
colorlinks=true,
citecolor=PineGreen,
linkcolor=red,
urlcolor=blue}
\usepackage[
style=authoryear,
natbib=true,
backend=biber,
dashed=false,
isbn = false,
doi = false,
url = false,
language=auto,
sorting=nyt,
maxbibnames=99,
maxcitenames=2,
uniquelist=false,
uniquename=false]{biblatex}
\addbibresource{biblatex-examples.bib}
% Change ToC settings for Appendix
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\renewcommand\protect\cftchapfont{\protect\bfseries}
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
% Toc for chapter
\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\renewcommand\cftchapfont{\hypersetup{linkcolor=blue}\bfseries}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\renewcommand\cftchapaftersnum{:}
\begin{document}
\section*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}
\section*{Acknowledgements}
\addcontentsline{toc}{chapter}{Acknowledgements}
\tableofcontents
\thispagestyle{empty}
\chapter{Intro}
Lorem \citet{sigfridsson}
\section{A section}
\subsection{A subsection}
\chapter{Test}
\addcontentsline{toc}{chapter}{Bibliography}
\printbibliography
\appendix
\chapter{Conclusions}
\end{document}
答案1
更新以回答您修改后的问题
这不再调整\hypersetup
。相反,我重新定义\numberline
为在以下条件成立时更改目录中编号条目的颜色:
\cftchappresnum
等于‘Chapter~’(不包括附录)- 数字的宽度是 >,
\mylength
这确保我们处于章节级别。
只有编号的章节是蓝色。其他都是红色。
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
colorlinks=true,
citecolor=PineGreen,
linkcolor=red,
urlcolor=blue}
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\renewcommand\cftchapaftersnum{:}
\makeatletter
\newcommand{\testpresnum}{Chapter~}
\renewcommand{\numberline}[1]{%
\ifx\cftchappresnum\testpresnum
\ifdim\@tempdima>\mylength\color{blue}\fi
\fi
\hb@xt@\@tempdima{\@cftbsnum #1\@cftasnum\hfil}\@cftasnumb}
\makeatother
\begin{document}
\section*{Abstract}
\addcontentsline{toc}{chapter}{Abstract}
\section*{Acknowledgements}
\addcontentsline{toc}{chapter}{Acknowledgements}
\tableofcontents
\chapter{Intro}
\section{A section}
\section{A subsection}
\chapter{Test}
\appendix
\chapter{Conclusions}
\section{A section}
\end{document}
答案2
使用\renewcommand{\cftchapfont}{\color{blue}}
尝试一下这个代码。
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage{xcolor}% added <<<<<<<<<<
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{\color{red}Appendix~}% changed <<<<
\protect\renewcommand{\protect\cftchapfont}{\color{red}}%% added <<<<<<<<<<
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}}%
}{}{}
\newlength\mylength
\renewcommand\cftchappresnum{\color{blue}Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\renewcommand{\cftchapfont}{\color{blue}} % added <<<<<<<<<<
\begin{document}
\tableofcontents
\chapter{Intro}
\chapter{Test}
\appendix
\chapter{Conclusions}
\end{document}
答案3
hyperref
覆盖您插入的任何颜色规范。因此,在下面的示例中,我使用优先hyperref
级通过特定放置的命令来设置颜色\hypersetup{linkcolor=<color>}
。
\documentclass{report}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[titletoc]{appendix}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[hyperfootnotes=false,english,colorlinks]{hyperref}
\hypersetup{
colorlinks=true,
citecolor=PineGreen,
urlcolor=blue}
% Change ToC settings for Appendix
\apptocmd{\appendix}
{\addtocontents{toc}{%
\protect\addtolength\protect\cftchapnumwidth{-\mylength}%
\protect\renewcommand{\protect\cftchappresnum}{Appendix~}%
\protect\renewcommand{\protect\cftchapfont}{\bfseries}%
\protect\settowidth\mylength{\bfseries\protect\cftchappresnum\protect\cftchapaftersnum}%
\protect\addtolength\protect\cftchapnumwidth{\mylength}%
\protect\hypersetup{linkcolor=red}}% Force red link colour
}{}{}
% Default ToC settings
\newlength\mylength
\renewcommand\cftchappresnum{Chapter~}
\settowidth\mylength{\bfseries\cftchappresnum\cftchapaftersnum}
\addtolength\cftchapnumwidth{\mylength}
\renewcommand{\cftchapfont}{\bfseries}
\renewcommand\cftchapaftersnum{:}
\begin{document}
\hypersetup{linkcolor=blue}% All hyperlinks will be blue (this will be for chapters)
\tableofcontents
\hypersetup{linkcolor=red}% If you don't have anything after the appendices you want linked in blue, this is not necessary
\chapter{Intro}
\chapter{Test}
\appendix
\chapter{Conclusions}
\end{document}
由于颜色变化被发送到.toc
,因此您需要对.toc
与该位置相关的任何更改进行至少两次编译。