It's common to use an \ifcase
construct to redefine a command depending on the value of a counter. The following example illustrates this:
\documentclass{article}
\usepackage{xcolor}
\usepackage{etoolbox}
\newcommand\sectioncolor{}
\renewcommand\sectioncolor{%
\ifcase\value{section} brown\or red\or blue\else olive\fi}
\pretocmd{\section}{\color{\sectioncolor}}{}{}
\begin{document}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\end{document}
The above redefinition of \sectioncolor
using \ifcase
implies manually feeding the colors in the \ifcase
construct. What I would like to achieve is to have a command, say \ColorList
, having as argument a comma separated list of variable length containing the colors to be used, and then redefine \sectioncolor
to use the colors of the list depending on the section counter. So for example, referring to my sample code, using
\ColorList{brown,red,blue,olive}
would be equivalent to
\renewcommand\sectioncolor{%
\ifcase\value{section} brown\or red\or blue\else olive\fi}
and
\ColorList{yellow,magenta,cyan,orange,blue}
would be equivalent to
\renewcommand\sectioncolor{%
\ifcase\value{section} yellow\or magenta\or cyan\or orange \else blue\fi}
but. of course, without having to manually feed the colors into the \ifcase
. How can this be done?
Best Answer
This is similar to @egreg's solution and avoids
\ifcase
in favour of just cycling the list, but the coding is probably a bit simpler (unless you'be already loaded expl3 for other reasons)