3

Is it possible to give to each chapter a different color coming from something like a color array?

I basically know how to colorize text, but for that purpose the color value is fixed. What I want to do is build up something like an array of colors and then use the color specified in cell 1 for chapter 1, second chapter color in cell 2, etc.

Is something like that doable in Latex?

navige
  • 452

2 Answers2

6

Random colors:

\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}

enter image description here

Cycle thru list

\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}

enter image description here

I used KOMAScript to alter the appearance of the chapters, just to make thing easy for me. Nevertheless the shown techniques should work with any approach to alter the sectioning.

bloodworks
  • 10,178
2

You could use

\csname colorchap\Roman{chapter}\endcsname

as your color command in your chapter heading code and then you need

\newcommand\colorchapA{\color{red}}
\newcommand\colorchapB{\color{blue}}
\newcommand\colorchapC{\color{green}}

to set up as many colors as you need.

David Carlisle
  • 757,742