手动生成的目录中章节的彩色字体

手动生成的目录中章节的彩色字体

以下手动生成目录的代码取自这里

\documentclass{book}
\pagestyle{empty}
\begin{document}
\noindent{\bfseries{\huge Contents}\hfill Page No.\vspace \bigskipamount \par }
\contentsline {chapter}{ACKNOWLEDGEMENTS}{ii}
\contentsline {chapter}{ABSTRACT}{iii}
\contentsline {chapter}{LIST OF TABLES}{iv}
\contentsline {chapter}{LIST OF FIGURES}{v}
\contentsline {chapter}{ABBREVIATIONS}{vi}
\contentsline {chapter}{NOTATIONS}{viii}
\contentsline {chapter}{\numberline {1}INTRODUCTION}{1}
\contentsline {section}{\numberline {1.1}General Background}{1}
\contentsline {section}{\numberline {1.2}Objective}{3}
\contentsline {subsection}{\numberline {1.2.1}Objective 1}{5}
\contentsline {subsection}{\numberline {1.2.2}Objective 2}{12}
\contentsline {subsection}{\numberline {1.2.3}Objective 3}{15}
\contentsline {section}{\numberline {1.3}Scheme}{16}
\end{document}

我想为章节设置彩色字体。一种方法是使用\textcolor{blue}{Colored text}。如果章节很多,这种方法就很麻烦。有没有办法全局设置章节条目的颜色?

答案1

将以下几行添加到你的序言中。

\usepackage{xcolor} % needed for the color commands
\usepackage{xpatch} % provides the command \xpatchcmd
\makeatletter % make @ a normal letter such that we can use \l@chapter as macro name
\xpatchcmd\l@chapter{#1}{\textcolor{blue}{#1}}{}{}
\makeatother % turn @ again into a non-letter

如果您还想将行右端的页码标记为蓝色,请将上面一行中的替换\textcolor为。\color\xpatchcmd

在此处输入图片描述

\documentclass{book}
\usepackage{xcolor}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\l@chapter{#1}{\textcolor{blue}{#1}}{}{}
\makeatother
\pagestyle{empty}
\begin{document}
\noindent{\bfseries{\huge Contents}\hfill Page No.\vspace \bigskipamount \par }
\contentsline {chapter}{ACKNOWLEDGEMENTS}{ii}
\contentsline {chapter}{ABSTRACT}{iii}
\contentsline {chapter}{LIST OF TABLES}{iv}
\contentsline {chapter}{LIST OF FIGURES}{v}
\contentsline {chapter}{ABBREVIATIONS}{vi}
\contentsline {chapter}{NOTATIONS}{viii}
\contentsline {chapter}{\numberline {1}INTRODUCTION}{1}
\contentsline {section}{\numberline {1.1}General Background}{1}
\contentsline {section}{\numberline {1.2}Objective}{3}
\contentsline {subsection}{\numberline {1.2.1}Objective 1}{5}
\contentsline {subsection}{\numberline {1.2.2}Objective 2}{12}
\contentsline {subsection}{\numberline {1.2.3}Objective 3}{15}
\contentsline {section}{\numberline {1.3}Scheme}{16}
\end{document}

蓝色完整线条:

在此处输入图片描述

\documentclass{book}
\usepackage{xcolor}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\l@chapter{#1}{\color{blue}#1}{}{}
\makeatother
\pagestyle{empty}
\begin{document}
\noindent{\bfseries{\huge Contents}\hfill Page No.\vspace \bigskipamount \par }
\contentsline {chapter}{ACKNOWLEDGEMENTS}{ii}
\contentsline {chapter}{ABSTRACT}{iii}
\contentsline {chapter}{LIST OF TABLES}{iv}
\contentsline {chapter}{LIST OF FIGURES}{v}
\contentsline {chapter}{ABBREVIATIONS}{vi}
\contentsline {chapter}{NOTATIONS}{viii}
\contentsline {chapter}{\numberline {1}INTRODUCTION}{1}
\contentsline {section}{\numberline {1.1}General Background}{1}
\contentsline {section}{\numberline {1.2}Objective}{3}
\contentsline {subsection}{\numberline {1.2.1}Objective 1}{5}
\contentsline {subsection}{\numberline {1.2.2}Objective 2}{12}
\contentsline {subsection}{\numberline {1.2.3}Objective 3}{15}
\contentsline {section}{\numberline {1.3}Scheme}{16}
\end{document}

答案2

尚不完善。章节编号也会改变颜色。

在此处输入图片描述

\documentclass{book}
\usepackage{xcolor}
\pagestyle{empty}

\usepackage{ifthen}

\newcommand{\contentslinex}[3]{%    
    \ifthenelse{\equal{#1}{chapter}}
    {\contentsline{#1}{\color{red}#2}{\color{black}#3}}
    {\contentsline{#1}{#2}{#3}}
}


\begin{document}
\noindent{\bfseries{\huge Contents}\hfill Page No.\vspace \bigskipamount \par }
\contentslinex {chapter}{ACKNOWLEDGEMENTS}{ii}
\contentslinex {chapter}{ABSTRACT}{iii}
\contentslinex {chapter}{LIST OF TABLES}{iv}
\contentslinex {chapter}{LIST OF FIGURES}{v}
\contentslinex {chapter}{ABBREVIATIONS}{vi}
\contentslinex {chapter}{NOTATIONS}{viii}
\contentslinex {chapter}{\numberline {1}INTRODUCTION}{1}
\contentslinex {section}{\numberline {1.1}General Background}{1}
\contentslinex {section}{\numberline {1.2}Objective}{3}
\contentslinex {subsection}{\numberline {1.2.1}Objective 1}{5}
\contentslinex {subsection}{\numberline {1.2.2}Objective 2}{12}
\contentslinex {subsection}{\numberline {1.2.3}Objective 3}{15}
\contentslinex {section}{\numberline {1.3}Scheme}{16}
\end{document

相关内容