软件包 pgfkeys 错误:我不知道密钥‘/tikz/’

软件包 pgfkeys 错误:我不知道密钥‘/tikz/’

当我设置为 1 时,当我想编译下面的 LaTeX 代码时会出现错误。\var否则我没有问题。我得到的错误是:Package pgfkeys Error: I do not know the key '/tikz/ '

你能告诉我我做错了什么吗?

非常感谢您的回复。

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

答案1

我不会使用数组来存储颜色,而是使用 PGFPlotscycle list功能,它更加灵活。对于您的情况,您可以设置

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

然后在环境中使用\addplot +而不是。 中定义的样式将按照调用顺序应用于绘图。 此方法还允许您设置除颜色之外的其他属性。 如果您希望第三个绘图为黑色、粗体和虚线,您可以说\addplotaxiscycle list

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


如果您确实想坚持您的方法,您应该使用\pgfplotsinvokeforeach{<list} { <code where the list item is available as #1 > }而不是\foreach,这将解决代码中的扩展问题。


cycle list方法代码

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

pgfplotsinvokeforeach方法代码

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

相关内容