# How can I color the header, section, and subsection name according to the current section value?

I'm trying to make a document with multiple "themes." This involves changing the color of the headers, section names, and headers based on what the current section is. The colors I want to use are custom colors.

Now, I know how to make this the same for the whole document, but I was trying to find a way that would automatically change it automatically depending on the current section value. I thought that it would be really easy with the ifthen package, as it seemed to be made for this (I could see if the section was equal to a value). However, it did not go as expected. I have a MWE below. Here I changed only the subsection name for clarity. I tried to make the subsections of Section One red and the subsections of Section Two blue. However, it seems like everything is blue. I did something similar for the header, but didn't go anywhere. Could anyone kindly point me in the right direction with this?

\documentclass[twoside]{article}

% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}

% Here begins my attempt
\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{blue}}{\thesection}{1em}{}
% My attempted ifthenelse statement. I put the if, then, and else arguments on different lines for clarity
\ifthenelse
{\equal{\thesection}{Section One}}
{\titleformat{\subsection}{\Large\normalfont\color{red}}{\thesubsection}{1em}{}}
{\titleformat{\subsection}{\Large\normalfont\color{blue}}{\thesubsection}{1em}{}}
% Sections begin on new page
\newcommand{\sectionbreak}{\clearpage}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\sectionmark}[1]{\markboth{#1}{}}

% For colors
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\section{Section One}

\subsection{Subsection One}
\blindtext[5]

\subsection{Subsection Two}
\blindtext[5]

\section{Section Two}

\subsection{Subsection One}
\blindtext[5]

\subsection{Subsection Two}
\blindtext[5]

\end{document}


Maybe using a colour series may simplify the problem. With \color{foo!![\thesection]} the colour is choosen based on the section number.

\documentclass[twoside]{article}

% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}

% Here begins my attempt
\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{foo!![\thesection]}}{\thesection}{1em}{}
\titleformat{\subsection}{\Large\normalfont\color{foo!![\thesection]}}{\thesubsection}{1em}{}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\sectionmark}[1]{\markboth{\color{foo!![\thesection]}#1}{}}

% For colors
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\definecolorseries{foo}{rgb}{step}{red}[rgb]{1,0,1}% 1/24
\resetcolorseries[2]{foo}

\begin{document}

\section{Section One}

\subsection{Subsection One}
\blindtext[2]

\clearpage
\section{Section Two}

\subsection{Subsection One}
\blindtext[2]

\end{document}


Or based on https://tex.stackexchange.com/a/370470/36296 a variant with fixed colours:

\documentclass[twoside]{article}

% For blind text
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{zz0}{rgb}{.1,.8,.3}
\definecolor{zz1}{rgb}{.8,.2,.2}
\definecolor{zz2}{rgb}{.7,.9,.1}
\definecolor{zz3}{rgb}{.6,.4, 0}
\definecolor{zz4}{rgb}{.5,.1,.9}
\definecolor{zz5}{rgb}{.1, 0,.8}
\definecolor{zz6}{rgb}{.7,.2,.6}
\definecolor{zz7}{rgb}{.9,.3,.8}
\definecolor{zz8}{rgb}{0 ,.4,.1}
\definecolor{zz9}{rgb}{.7,.5,.2}
\definecolor{zz10}{rgb}{.1,.6,.3}
\definecolor{zz11}{rgb}{.6,.8,.5}

% Here begins my attempt
%\usepackage{ifthen}
\usepackage{titlesec}
% Coloring the section names (didn't mess with this, but I am using this as the basis of coloring the section and subsection names)
\titleformat{\section}{\LARGE\normalfont\center\color{zz\arabic{section}}}{\thesection}{1em}{}
\titleformat{\subsection}{\Large\normalfont\color{zz\arabic{section}}}{\thesubsection}{1em}{}
\newcommand{\sectionbreak}{\clearpage}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\sectionmark}[1]{\markboth{\color{zz\arabic{section}}#1}{}}

\begin{document}

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\section{Section}
\subsection{Subsection}
\blindtext[2]

\end{document}

• Thanks for the answer! However, as far as I am aware, this only works for sections (not headers) and you don't seem to be able to choose the colors yourself as far as I can tell (which is important in my case). Nevertheless, thank you for your answer! – Skeleton Bow May 18 '17 at 16:33