首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Latex中用两种颜色自定义目录

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

Stack Overflow用户
提问于 2014-12-18 20:58:20
回答 1查看 12.5K关注 0票数 2

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

  • 用蓝色涂上颜色的章节和分段是作者X写的,红色是作者Y写的。

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

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

最后,这些颜色只能在目录中更改,而不能在文档的内容中更改。

我怎么能这样定制呢?有人能帮上忙吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-19 05:45:59

使用tocloft,您可以控制ToC中每个条目类型的格式设置。章节有\cftchapfont,章节有\cftsecfont,子部分有\cftsubsecfont

下面提供\authoredby{<name>},它将一个条目插入到ToC中以更改颜色。此外,作者的颜色可以定义使用\defineauthorcolor{<name>}{<colour>}

代码语言:javascript
运行
复制
\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}

上面最小示例的.toc如下所示:

代码语言:javascript
运行
复制
\@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}

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

这个解决方案应该与bookreportarticle文档类一起工作。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27555572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档