[Tex/LaTex] Package pgfkeys Error: I do not know the key ‘/tikz/ ‘

foreachpgfkeyspgfplots

I have an error when I want to compile the LaTeX code below when I set \var to 1.
Otherwise I have no problem.
The error I got is: Package pgfkeys Error: I do not know the key '/tikz/ '

Could you please tell what I have done wrong?

Thanks a lot for your response.

\documentclass{article}
\usepackage{pgfplots}


\begin{filecontents*}{S1data.dat}
20  1.914   1.443   1.137   2.476   23.555
40  1.734   1.381   1.173   1.805   25.001
60  1.737   1.428   1.176   2.176   25.001
80  1.797   1.813   0.94    2.518   25.001
100 1.681   1.256   1.463   2.107   25
120 1.797   1.508   1.254   1.966   25
140 1.649   1.403   1.177   1.825   25
160 1.534   1.438   1.272   2.156   24.998
180 1.541   1.412   1.283   2.408   24.999
200 1.967   1.4 1.205   2.688   49.805
220 1.584   1.257   1.269   1.872   50
240 1.727   1.361   1.292   2.144   50
260 1.479   1.596   1.207   1.726   50
280 1.426   1.349   1.259   2.08    50
300 1.629   1.456   1.321   1.798   50
320 1.872   1.447   1.31    1.574   50
340 1.682   1.503   1.173   1.687   50
360 1.543   1.242   1.22    1.743   50
380 1.823   1.472   1.307   2.005   74.754
400 1.66    1.308   1.441   1.615   75
420 1.636   1.42    1.239   1.754   75
440 1.572   1.505   1.253   1.556   75
460 1.681   1.27    1.139   1.586   75
480 1.74    1.536   1.185   1.996   75
500 1.772   1.318   1.335   2.126   75
\end{filecontents*}

\def\Colorarray{{1}{blue}{2}{red}{3}{orange}{4}{violet}}
\def\getcolor#1{\expandafter\xgetcolor\Colorarray{#1}{}test{#1}}
\def\xgetcolor#1#2#3test#4{\ifnum#4=#1 #2\else\xgetcolor#3test{#4}\fi}

\begin{document}
\pgfkeys{/pgf/number format/.cd, set decimal separator={,{\!}}, set thousands separator={}}

\def\var{1}

\begin{tikzpicture}
  \begin{axis}[title={Title}, grid=major, axis x line=bottom, axis y line=left,
  xlabel={Time}, ylabel={Page time(s)}, 
  x tick label style={font=\tiny, rotate=35},
  y tick label style={font=\tiny},
  x label style={font=\small},
  y label style={font=\small},
  legend entries={Page1,Page2,Page3,Page4},legend style={font=\tiny, at={(1.5,1)}}]
  \ifnum\var=1
          \foreach \i in {1,2,...,4} {
                \addplot[smooth,\getcolor{\i}] table[x index=0,y index=\i] {S1data.dat};
          }
    \else
        \addplot[smooth,\getcolor{1}] table[x index=0,y index=1] {S1data.dat};
        \addplot[smooth,\getcolor{2}] table[x index=0,y index=2] {S1data.dat};
        \addplot[smooth,\getcolor{3}] table[x index=0,y index=3] {S1data.dat};
        \addplot[smooth,\getcolor{4}] table[x index=0,y index=4] {S1data.dat};  
  \fi
  \end{axis}
\end{tikzpicture}

\end{document}

Best Answer

Instead of using an array to store the colours, I would use the PGFPlots cycle list functionality, which is much more flexible. In your case, you would set

\pgfplotsset{
    cycle list={
        blue,
        red,
        orange,
        violet
    }
}

and then use \addplot + instead of \addplot in your axis environment. The styles defined in the cycle list will then be applied to the plots in the order they are called. This approach also allows you to set other properties than just the colour. If you wanted the third plot to be black, thick and dashed, you could say

\pgfplotsset{
    cycle list={
        blue,
        red,
        {black, thick, dashed},
        violet
    }
}


If you do want to stick with your approach, you should use \pgfplotsinvokeforeach{<list} { <code where the list item is available as #1 > } instead of \foreach, which will fix the expansion problem in your code.


Code for the cycle list approach

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{S1data.dat}
20  1.914   1.443   1.137   2.476   23.555
40  1.734   1.381   1.173   1.805   25.001
60  1.737   1.428   1.176   2.176   25.001
80  1.797   1.813   0.94    2.518   25.001
100 1.681   1.256   1.463   2.107   25
120 1.797   1.508   1.254   1.966   25
140 1.649   1.403   1.177   1.825   25
160 1.534   1.438   1.272   2.156   24.998
180 1.541   1.412   1.283   2.408   24.999
200 1.967   1.4 1.205   2.688   49.805
220 1.584   1.257   1.269   1.872   50
240 1.727   1.361   1.292   2.144   50
260 1.479   1.596   1.207   1.726   50
280 1.426   1.349   1.259   2.08    50
300 1.629   1.456   1.321   1.798   50
320 1.872   1.447   1.31    1.574   50
340 1.682   1.503   1.173   1.687   50
360 1.543   1.242   1.22    1.743   50
380 1.823   1.472   1.307   2.005   74.754
400 1.66    1.308   1.441   1.615   75
420 1.636   1.42    1.239   1.754   75
440 1.572   1.505   1.253   1.556   75
460 1.681   1.27    1.139   1.586   75
480 1.74    1.536   1.185   1.996   75
500 1.772   1.318   1.335   2.126   75
\end{filecontents*}


\pgfplotsset{
    cycle list={
        blue,
        red,
        orange,
        violet
    }
}

\begin{document}
\pgfkeys{/pgf/number format/.cd, set decimal separator={,{\!}}, set thousands separator={}}


\begin{tikzpicture}
  \begin{axis}[title={Title}, grid=major, axis x line=bottom, axis y line=left,
  xlabel={Time}, ylabel={Page time(s)}, 
  x tick label style={font=\tiny, rotate=35},
  y tick label style={font=\tiny},
  x label style={font=\small},
  y label style={font=\small},
  legend entries={Page1,Page2,Page3,Page4},legend style={font=\tiny, at={(1.5,1)}}]
  \foreach \i in {1,2,...,4} {
    \addplot +[smooth] table[x index=0,y index=\i] {S1data.dat};
  }
  \end{axis}
\end{tikzpicture}

\end{document}

Code for the pgfplotsinvokeforeach approach

\documentclass{article}
\usepackage{pgfplots}


\begin{filecontents*}{S1data.dat}
20  1.914   1.443   1.137   2.476   23.555
40  1.734   1.381   1.173   1.805   25.001
60  1.737   1.428   1.176   2.176   25.001
80  1.797   1.813   0.94    2.518   25.001
100 1.681   1.256   1.463   2.107   25
120 1.797   1.508   1.254   1.966   25
140 1.649   1.403   1.177   1.825   25
160 1.534   1.438   1.272   2.156   24.998
180 1.541   1.412   1.283   2.408   24.999
200 1.967   1.4 1.205   2.688   49.805
220 1.584   1.257   1.269   1.872   50
240 1.727   1.361   1.292   2.144   50
260 1.479   1.596   1.207   1.726   50
280 1.426   1.349   1.259   2.08    50
300 1.629   1.456   1.321   1.798   50
320 1.872   1.447   1.31    1.574   50
340 1.682   1.503   1.173   1.687   50
360 1.543   1.242   1.22    1.743   50
380 1.823   1.472   1.307   2.005   74.754
400 1.66    1.308   1.441   1.615   75
420 1.636   1.42    1.239   1.754   75
440 1.572   1.505   1.253   1.556   75
460 1.681   1.27    1.139   1.586   75
480 1.74    1.536   1.185   1.996   75
500 1.772   1.318   1.335   2.126   75
\end{filecontents*}

\def\Colorarray{{1}{blue}{2}{red}{3}{orange}{4}{violet}}
\def\getcolor#1{\expandafter\xgetcolor\Colorarray{#1}{}test{#1}}
\def\xgetcolor#1#2#3test#4{\ifnum#4=#1 #2\else\xgetcolor#3test{#4}\fi}

\begin{document}
\pgfkeys{/pgf/number format/.cd, set decimal separator={,{\!}}, set thousands separator={}}

\def\var{1}

\begin{tikzpicture}
  \begin{axis}[title={Title}, grid=major, axis x line=bottom, axis y line=left,
  xlabel={Time}, ylabel={Page time(s)}, 
  x tick label style={font=\tiny, rotate=35},
  y tick label style={font=\tiny},
  x label style={font=\small},
  y label style={font=\small},
  legend entries={Page1,Page2,Page3,Page4},legend style={font=\tiny, at={(1.5,1)}}]
  \ifnum\var=1
          \pgfplotsinvokeforeach{1,2,...,4} {
                \addplot[smooth,\getcolor{#1}] table[x index=0,y index=#1] {S1data.dat};
          }
    \else
        \addplot[smooth,\getcolor{1}] table[x index=0,y index=1] {S1data.dat};
        \addplot[smooth,\getcolor{2}] table[x index=0,y index=2] {S1data.dat};
        \addplot[smooth,\getcolor{3}] table[x index=0,y index=3] {S1data.dat};
        \addplot[smooth,\getcolor{4}] table[x index=0,y index=4] {S1data.dat};  
  \fi
  \end{axis}
\end{tikzpicture}

\end{document}