2

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}

% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\textsc{\leftmark}}\quad\quad\thepage}
\fancyhead[EL]{\thepage\quad\quad{\textsc{\leftmark}}}
\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}

1 Answer 1

1

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

% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\color{foo!![\thesection]}\textsc{\leftmark}}\quad\quad\color{foo!![\thesection]}\thepage}
\fancyhead[EL]{\color{foo!![\thesection]}\thepage\quad\quad{\textsc{\color{foo!![\thesection]}\leftmark}}}
\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}

enter image description here


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}

% For headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[OR]{{\color{zz\arabic{section}}\textsc{\leftmark}}\quad\quad\color{zz\arabic{section}}\thepage}
\fancyhead[EL]{\color{zz\arabic{section}}\thepage\quad\quad{\textsc{\color{zz\arabic{section}}\leftmark}}}
\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}
11
  • 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! May 18, 2017 at 16:33
  • @SkeletonBow Please see my edit, I added colour to the headers May 18, 2017 at 16:44
  • Oh, that's really complete. That totally does it (I couldn't have figured this out otherwise!). Thank you. May 18, 2017 at 16:45
  • @SkeletonBow You're welcome! May 18, 2017 at 16:45
  • Just one comment, if you don't mind: it seems as though I am not able to apply any custom sequence of colors, like yellow, orange, green, red, and then cyan. Would I need to use colors separated by a definite rgb amount to use this? May 18, 2017 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .