# Plots and chapterspecific colors

I have a color switching function that looks like this

\definecolor{chap1}{RGB}{68, 114, 148}
\definecolor{chap2}{RGB}{10,130,124}
\definecolor{chap3}{RGB}{250,72,46}
\definecolor{chap4}{RGB}{244,163,46}

\usepackage{etoolbox}
\newcommand{\basecolor}{
\ifcase\arabic{chapter} black\or chap1\or chap2\or chap3\or chap4\else black\fi%
}

\newcommand{\chaptercolor}{\basecolor}


This lets me use \chaptercolor anywhere to get the color that has been chosen for this chaper depending on its position in the document. Works great, except for one thing.

I cannot get it to work with plots.

Take this simple plot as an example:

\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
domain=0:1, range=0:1,
axis lines=none,
]
\addplot [->] plot coordinates {(0,0) (0,1)};
\addplot [->, color=\chaptercolor, fill=\chaptercolor, draw=\chaptercolor] plot coordinates {(0,0) (1,0)};
\end{axis}
\end{tikzpicture}


This gives me the error Package xcolor Error: Undefined color  chap2'.

With all the odd symbols in it.

If I use the color chap2 directly instead it works fine. It would also be great to have the plots always use \chaptercolor maybe by using \tikzset{}, but so far that gives even worse error messages.

Any input on how to fix this, or perhaps even a different approach to cycling the colors?

Edit:
Adding an identical color with a space in front of its name solves the problem.

\definecolor{ chap1}{RGB}{68, 114, 148}
\definecolor{ chap2}{RGB}{10,130,124}
\definecolor{ chap3}{RGB}{250,72,46}
\definecolor{ chap4}{RGB}{244,163,46}
\definecolor{chap1}{RGB}{68, 114, 148}
\definecolor{chap2}{RGB}{10,130,124}
\definecolor{chap3}{RGB}{250,72,46}
\definecolor{chap4}{RGB}{244,163,46}


A little inelegant, but it's not too bad since it's only in the preamble.

• \value{chapter} rather than \arabic{chapter}, just a tip. – Manuel Mar 26 '15 at 14:54
• I can't help it, parts of that code look familiar. – Johannes_B Mar 26 '15 at 15:18
• The code is from this site and your name does ring a bell. I could very easily be of your design. If that is the case, thank you. It has worked wonders for almost everything. Except this case of course :) – Mathias Nielsen Mar 26 '15 at 15:34

I think the problem comes from \basecolor definition. It inserts an empty space before color names which is the reason why \definecolor{ chap1}... works.

\documentclass{book}
\usepackage{pgfplots}

\definecolor{chap1}{RGB}{68, 114, 148}
\definecolor{chap2}{RGB}{10,130,124}
\definecolor{chap3}{RGB}{250,72,46}
\definecolor{chap4}{RGB}{244,163,46}

\usepackage{etoolbox}
\newcommand{\basecolor}{%
\ifcase\value{chapter}
black%
\or%
chap1%
\or%
chap2%
\or%
chap3%
\or%
chap4%
\else%
black%
\fi%
}

\newcommand{\chaptercolor}{\basecolor}

\begin{document}

\foreach \i in {1,...,5}
{
\chapter{\i}
\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
domain=0:1, range=0:1,
axis lines=none,
]
\addplot [->] plot coordinates {(0,0) (0,1)};
\addplot [->, color=\chaptercolor, fill=\chaptercolor, draw=\chaptercolor] plot coordinates {(0,0) (1,0)};
\end{axis}
\end{tikzpicture}
}
\end{document}
`

• Doesn't work for me. I get the same error still, but I think you are right in that the problem is an added space here, but I cannot get rid of it. – Mathias Nielsen Mar 26 '15 at 19:47
• @MathiasNielsen I've updated my answer with a complete code. – Ignasi Mar 26 '15 at 20:24