Foreach and different color compilation error

by LMT-PhD   Last Updated July 12, 2019 13:23 PM - source

if you execute this code there will be a compilation error, but if you replace red!\rnb!blue by red!20!blue it works fine.

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

Why is that?

Tags : color foreach


Answers 1


I am not decided whether this is a duplicate of e.g. this question. Here there is slight additional complication that you need to replace \x by x in the arguments of the plots, otherwise everything that has been said here applies.

\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}
marmot
marmot
July 12, 2019 13:10 PM

Related Questions


Changing color in foreach

Updated June 21, 2015 00:10 AM

How to color multiple plots in rainbow colors?

Updated August 15, 2015 17:10 PM

`\definecolor` within `\foreach` loop

Updated March 26, 2017 12:23 PM


Color names in foreach loop

Updated May 27, 2018 15:23 PM