I want to draw a gradient spiral with tikz and I am encountering problems. My current code:

\draw[line width=0.24mm, rotate=120, color=red] (0,0)
  \foreach \i [evaluate={\r=(\i/1212)^2;}] in {0,5,...,545} {
    -- (\i:\r)

I have seen that if I add a "[color=red!10]" before the last semicolon, it overrides the "color=red" of the first line. However, what I would like to do is to use the \i variable (or a formula including it) instead of the number 10, in something like this:

\draw[line width=0.24mm, rotate=120, color=red] (0,0)
  \foreach \i [evaluate={\r=(\i/1212)^2;}] in {0,5,...,545} {
    -- (\i:\r)

The problem is that the reference to "i" is outside the foreach loop, so I encounter a "! Missing number, treated as zero." error.

Is there any way I can use the "i" variable outside the loop? I've tried to define a variable inside the loop to use it outside with no success.

Any other way to draw a gradient spiral will be appreciated too.



I think this works, though I'm not sure it looks that good.

enter image description here

  \foreach \i [
   remember=\i as \previ(initially 0),
   evaluate=\i as \r using {(\i/1212)^2},
   evaluate=\previ as \prevr using {(\previ/1212)^2},
   evaluate=\i as \ired using {\i/5.45}] in {5,10,...,545} {
   \draw[line width=0.24mm, rotate=120, color=red!\ired] (\previ:\prevr) -- (\i:\r);
  • Perhaps you can use Path following color gradient in TikZ – Torbjørn T. May 11 '16 at 12:18
  • It's exactly what I wanted! The spiral is part of a bigger design and the gradient will involve better looking colors, but the thing is that my problem is solved. Thanks! :) – jorgefpastor May 11 '16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.