1

I´m trying to color multiple curves of a line in rainbow colors wiht tikz. I´ve found the [wave] option for color from the xcolor pagacke, but I can't get it to work inside tikz's draw command.

\documentclass{article}

\usepackage{tikz}
\usepackage{xcolor}


\begin{document}

\begin{tikzpicture}[scale=0.5]
    \draw[help lines] (-9.9,-9.9) grid (9.9,9.9);
    \draw[very thick,->] (-10,0) -- (10,0) node[above]{$x$};
    \draw[very thick,->] (0,-10) -- (0,10) node[above]{$y$};

    \foreach \x in {-9,...,9} \draw[shift={(\x,0)},scale=2] (0pt,2pt) -- (0pt,-2pt);
    \foreach \y in {-9,...,9} \draw[shift={(0,\y)},scale=2] (2pt,0pt) -- (-2pt,0pt);


    \foreach \k in {1,...,10} %\pgfmathsetmacro{\faktor}{(\k-1)*40+380}
        \draw[very thick] (0,11-\k) -- (\k,0);
\end{tikzpicture}

\end{document}

I´ve tried to do something like

\draw[color[wave]=\factor] 

but it didn't work.

Do you have any ideas?

3
  • To use a color you just write \draw[red] for example.
    – Sigur
    Commented Aug 15, 2015 at 10:39
  • Yes, but I don't want to write each color manually. I want tikz to use a different color (preferably rainbow colors) for each value out of foreach.
    – Daniel
    Commented Aug 15, 2015 at 10:45
  • I think this can help you: tex.stackexchange.com/a/134368/1952
    – Ignasi
    Commented Aug 15, 2015 at 11:14

1 Answer 1

3

Ok, I did it.

\documentclass{article}


\usepackage[rgb]{xcolor}
\usepackage{tikz}

\begin{document}

\pgfmathsetmacro{\n}{20}

\begin{tikzpicture}[scale=5/\n]
    \draw[help lines,step=\n/10] (-\n+0.1,-\n+0.1) grid (\n-0.1,\n-0.1);
    \draw[very thick,->] (-\n,0) -- (\n,0) node[above]{$x$};
    \draw[very thick,->] (0,-\n) -- (0,\n) node[above]{$y$};

    \foreach \x in {-\n,...,\n} \draw[shift={(\x,0)},scale=\n/5] (0pt,2pt) -- (0pt,-2pt);
    \foreach \y in {-\n,...,\n} \draw[shift={(0,\y)},scale=\n/5] (2pt,0pt) -- (-2pt,0pt);



    \foreach \k in {1,...,\n}{ \pgfmathsetmacro{\faktor}{(\k-1)*400/\n+380}
        \definecolor{rainbow}{wave}{\faktor}

        \draw[very thick, color=rainbow] (0,\n+1-\k) -- (\k,0);

        %\draw[very thick] (0,11-\k) -- (-\k,0);
        %\draw[very thick] (0,11-\k) -- (\k,0);
    }
\end{tikzpicture}

\end{document}

I defined makros for the colorvalue in nm and defined a new color in the wave system. I also added a counter for the number of lines being plotted and shifted the intervall to the intervall of visible light (380nm,780nm).

Apparently it isn't working if you don't add the optional parameter [rgb] in \usepackage[rgb]{xcolor]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .