是否可以通过类似颜色数组的东西赋予每个章节不同的颜色?
我基本上知道如何给文本着色,但为此目的,颜色值是固定的。我想要做的是建立一个类似于颜色数组的东西,然后使用单元格 1 中指定的颜色作为第 1 章的颜色,单元格 2 中指定第二章的颜色,等等。
类似的事情在 Latex 中可行吗?
答案1
随机颜色:
\documentclass{scrbook}
\usepackage{pgffor, xcolor}
\usepackage{pgfmath}
\pgfmathdeclarerandomlist{color}{{red!20!yellow}{blue}{yellow}{green!20}{red}{black!20}{orange}}% declare random list
\setkomafont{sectioning}{\pgfmathrandomitem{\randcolor}{color}\color{\randcolor}}% use random list
\begin{document}
%Showcase only!
\let\cleardoublepage\relax
\foreach \x in {A,...,H}{
\chapter{\x}}
\newpage
\end{document}
循环浏览列表
\documentclass{scrbook}
\usepackage{pgffor, xcolor}
\usepackage{pgfmath}
%The following list must be very long to make sure that pgfmath
%does not try to read a non existing element
% pgfarrys start form index 0 but chapters start form 1 so "dummy" is needed
% in order to have it started form the desired (first) color
% this could be any element cause it't not read anyway
\def\mytemparray{{"dummy" ,"blue!20!green","yellow","green!20","red","black!20","orange","red!20!yellow" ,"blue","yellow","green!20","red","black!20","orange"}}%
\setkomafont{sectioning}{\pgfmathparse{\mytemparray[\thechapter]}\color{\pgfmathresult} }
\begin{document}
%Showcase only!
\let\cleardoublepage\relax
\foreach \x in {A,...,H}{
\chapter{\x}}
\newpage
\end{document}
我使用 KOMAScript 来改变章节的外观,只是为了让事情变得简单。不过,所展示的技术应该适用于任何改变章节的方法。
答案2
你可以使用
\csname colorchap\Roman{chapter}\endcsname
作为章节标题代码中的颜色命令,然后您需要
\newcommand\colorchapA{\color{red}}
\newcommand\colorchapB{\color{blue}}
\newcommand\colorchapC{\color{green}}
根据需要设置多种颜色。