Foreach 和不同颜色编译错误

Foreach 和不同颜色编译错误

如果你执行此代码,将会出现编译错误,但是如果你替换red!\rnb!bluered!20!blue,它就可以正常工作。

    \documentclass[border=2pt]{standalone}
    \usepackage[utf8]{inputenc}

    \usepackage{graphicx}
    \usepackage{tikz,pgfplots}

    \begin{document}

    \begin{tikzpicture}

    \def\K{1}

    \begin{axis}[axis x line=bottom, axis y line = left ,
    width=13cm, height=\axisdefaultheight,
    ymax=1.85*\K,axis lines=none,
    ytick=\empty,extra y ticks={\K}, extra y tick labels={},extra y tick style={grid=major},
    xtick=\empty,
    ]
    \foreach \z/\rnb in {0.1/10,0.2/20}{
        %\def\z{0.1}
        \def\T{1}
        \def\w{3.14159*2}
        \addplot[samples=2,domain=0:5.2*\T,thick=1pt] { \K };
        \addplot[red!\rnb!blue,samples=20,domain=0:1*\T,thick=3pt,ultra thick] {  
        ( 1 - ( (exp(-\w*\z*\x)/sqrt(1-\z^2)) *cos((\w*\x*sqrt(1-\z^2) - (atan(\z/sqrt(1-\z^2))*3.14159/180) ) *180/3.14159) )  ) *\K};
    }

    \end{axis}
    \end{tikzpicture}


    \end{document}

这是为什么?

答案1

我不知道这是否是重复的例如这个问题。这里有一点额外的复杂性,你需要在情节的论点中替换它\xx否则所说的一切都这里适用。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{tikzpicture}

\def\K{1}

\begin{axis}[axis x line=bottom, axis y line = left ,
width=13cm, height=\axisdefaultheight,
ymax=1.85*\K,axis lines=none,
ytick=\empty,extra y ticks={\K}, extra y tick labels={},extra y tick style={grid=major},
xtick=\empty,
]
\foreach \z/\rnb in {0.1/10,0.2/20}{
    %\def\z{0.1}
    \def\T{1}
    \def\w{3.14159*2}
    \edef\temp{\noexpand\addplot[samples=2,domain=0:5.2*\T,thick=1pt] { \K };
    \noexpand\addplot[red!\rnb!blue,samples=20,domain=0:1*\T,thick=3pt,ultra thick] {  
    ( 1 - ( (exp(-\w*\z*x)/sqrt(1-\z^2)) *cos((\w*x*sqrt(1-\z^2) - (atan(\z/sqrt(1-\z^2))*3.14159/180) ) *180/3.14159) )  ) *\K};}
    \temp
}

\end{axis}
\end{tikzpicture}
\end{document}

相关内容