# Problem with tikz foreach

I am using a foreach loop to add plots to an axis environment and want each plot to have its own color. To achieve this I tried the following:

\foreach \x/\barcolor in {1/red, 2/green, 3/blue, 4/gray}{
}


But somehow there is a problem with the \barcolor variable. If I use fill=blue the loop runs perfectly fine, but obviously all bars are blue. Unfolding the loop and manually adding the four plot is working fine too:

\addplot[fill=red] table[x=key,y expr={\thisrowno{1}/\thisrowno{6}}] {data.dat};


I cannot figure the problem with my loop and hope you can help me.

• Welcome to TeX.SX! I suspect you need \begingroup\edef\x{\endgroup\noexpand\addplot[fill=\barcolor]}\x instead of \addplot[fill=\barcolor] – egreg May 11 '15 at 12:17
• Wow, that worked. Thank you very much for this comment. Can your please add an answer and give some explanations to your code. – Rambo Ramon May 11 '15 at 12:22
• or \addplot[fill/.expanded=\barcolor] – percusse May 11 '15 at 12:25
• @percusse Which is much nicer! – egreg May 11 '15 at 12:27
• But this does not seem to wor for me. Can you explain the problem of my original code and what was your attempt to fix it? – Rambo Ramon May 11 '15 at 12:30

You could use a cycle list for the colors.

Example:

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{filecontents*}{data.dat}
key 1 2 3 4 5 6
1   1 2 3 4 5 6
2   2 3 4 5 6 7
3   7 8 2 1 3 8
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
enlarge x limits={abs=30pt},
cycle list={red,green,blue,gray}
]
\foreach \x in {1,...,4}{
\addplot+[fill,draw=black] table [x=key, y expr={\thisrowno{\x}/\thisrowno{6}}] {data.dat};}
\end{axis}
\end{tikzpicture}
\end{document}


Result:

The cycle list defines the plot specifications for each \addplot command without explicit given keys. The first plot uses the first entry in the list, the second uses the second etc.

If you are using \addplot+[...] the specifications of the cycle list are appended by the explicit given keys. In the example above the color for every plot is specified by the cycle list and the plot options are appended by fill and draw=black.

If you use explicit specifications without the + the cycle list is not used. Thats means \addplot[...] does not use the cycle list even if the brackets are empty.

• That is a nice solution too and probably I settle for this one. Can you share some insights on what is happening behind the scenes? I am not exaclty sure how the cycle list is connected to the \addplot+ command. For this example it may be pretty obvious, but I want to make sure to not miss an important detail. – Rambo Ramon May 11 '15 at 13:30
• @Rambo See my edited answer. – esdd May 11 '15 at 23:28

I just want to present my personal conclusion of the discussion in the comment section of the question (and the link I posted there).

The following code is a working example which fits my needs:

\foreach \x/\barcolor in {1/red, 2/green, 3/blue, 4/gray}{

I think this is a cleaner version of the proposal of egreg, because I was a bit confused by the reuse of \x for two different things.