In Change color in addplot in for loop of pgfplots It was discussed how to loop over nonexistent colours in \addplot and axis I would however like to loop over existing colours, but:
\documentclass[border=5pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{greyscale1}{rgb}{0.1,0.1,0.1}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach \file in {1}
\addplot[color=greyscale\file] table [col sep=comma] {d.dat};;
\end{axis}
\end{tikzpicture}
\end{document}
returns the error:
Package xcolor Error: Undefined color `greyscale\file '.
If instead I do:
\documentclass[border=5pt,tikz]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{greyscale1}{rgb}{0.1,0.1,0.1}
\begin{document}
\begin{tikzpicture}
\foreach \file in {1}
\draw[color=greyscale\file] (0,2) -- (1,2);
\end{tikzpicture}
\end{document}
I get:
Edit:
d.dat looks like
0,2
1,2


\pgfplotsinvokeforeach– Thorbjørn E. K. Christensen Oct 02 '18 at 11:57\foreach, i.e. there are several other situations in which you may want to do\edef\temp{\noexpand\addplot ...} \tempbut here you do not need it. – Oct 02 '18 at 12:01