在Latex中使用两种颜色自定义目录

Dhe*_*raj 2 latex tableofcontents latex-environment

在我的文档中,我想在目录中使用两种颜色来区分两位作者之间的工作.例如,

  • 蓝色的章节,章节和小节由作者X书写,红色由作者Y书写.

在一些章节中,章节和小节由两位作者撰写.例如,在A章(蓝色)中,

  • 第1节(蓝色)由X编写.
  • 第1.1节(红色)和第2节(红色)由Y撰写.

最后,这些颜色必须仅在目录中更改,而不是在文档的内容中更改.

我该如何定制呢?有人能帮助这样的事吗?

提前致谢.

Wer*_*ner 8

随着tocloft你在TOC中的每个条目类型的格式控制.对于章节,有\cftchapfont章节\cftsecfont和章节\cftsubsecfont.

下面提供\authoredby{<name>}了在ToC中插入一个用于更改颜色的条目.另外,可以使用作者颜色来定义\defineauthorcolor{<name>}{<colour>}.

在此输入图像描述

\documentclass{book}
\usepackage{tocloft,xcolor}

\newcommand{\defineauthorcolor}[2]{%
  \colorlet{author#1}{#2}% Create an author colour
  \expandafter\def\csname authoredby#1\endcsname{% Create author colour settings
    \renewcommand{\cftchapfont}{\bfseries\color{author#1}}% Chapter colour
    \renewcommand{\cftsecfont}{\color{author#1}}% Section colour
    \renewcommand{\cftsubsecfont}{\color{author#1}}}% Subsection colour
}
\makeatletter
\newcommand{\authoredby}[1]{\addtocontents{toc}{\protect\@nameuse{authoredby#1}}}%
\makeatother

\defineauthorcolor{A}{red}% Author A will be coloured red
\defineauthorcolor{B}{blue}% Author B will be coloured blue

\begin{document}

\tableofcontents

\authoredby{A}
\chapter{A chapter}
\authoredby{B}
\section{A section}
\subsection{A subsection}
\authoredby{A}
\section{Another section}
\subsection{Another subsection}
\authoredby{B}
\subsection{Yet another subsection}

\chapter{Another chapter}
\section{First section}
\section{Second section}
\section{Last section}

\authoredby{A}
\chapter{Last chapter}

\end{document}
Run Code Online (Sandbox Code Playgroud)

.toc对上述小例子看起来是这样的:

\@nameuse {authoredbyA}
\contentsline {chapter}{\numberline {1}A chapter}{3}
\@nameuse {authoredbyB}
\contentsline {section}{\numberline {1.1}A section}{3}
\contentsline {subsection}{\numberline {1.1.1}A subsection}{3}
\@nameuse {authoredbyA}
\contentsline {section}{\numberline {1.2}Another section}{3}
\contentsline {subsection}{\numberline {1.2.1}Another subsection}{3}
\@nameuse {authoredbyB}
\contentsline {subsection}{\numberline {1.2.2}Yet another subsection}{3}
\contentsline {chapter}{\numberline {2}Another chapter}{5}
\contentsline {section}{\numberline {2.1}First section}{5}
\contentsline {section}{\numberline {2.2}Second section}{5}
\contentsline {section}{\numberline {2.3}Last section}{5}
\@nameuse {authoredbyA}
\contentsline {chapter}{\numberline {3}Last chapter}{7}
Run Code Online (Sandbox Code Playgroud)

每次使用都\authoredby{<name>}插入\@nameuse{authoredby<name>}到ToC中以相应地切换颜色.请注意,此解决方案适用于任意数量的作者(不仅限于两个).

该解决方案应与工作book,reportarticle文档类.