# How to use \x from \foreach loop in the parameters of \draw in tikz

In order to make it simple, I want to draw concentric circles fading out with something like

\foreach \x in {1,1.5,...,4}
\draw[color=black!{100-10*\x}!white] (0,0) circle (\x);


The problem is I don't know if there is a way to make some maths in the parameters of the \draw function. Is there a way ?

(I could solve the problem by doing something like

\foreach \x in {10,15,...,40}
\draw[color=white!\x!black] (5,0) circle ({0.1*\x});


But I really want to know if its possible to do it the other way)

Also, why something like

\foreach \x in {10,15,...,40}
\draw[color=white!\x!black] (5,0) circle ({0.1*\x} cm); %note the cm


does not work?

-
– Jubobs Aug 7 '14 at 7:49
@Jubobs Thanks! – mwoua Aug 13 '14 at 18:35

You can do some thing like this:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \x in {1,1.5,...,4}
\pgfmathtruncatemacro{\tmp}{100-\x*20}
\draw[color=blue!\tmp!green] (0,0) circle (\x);
\end{tikzpicture}
\end{document}


Calculate the value outside, store it in a macro (possibly truncate if you want as I did) and then use the macro.

Applying the same strategy to your last query:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \x in {10,15,...,40}
\pgfmathsetmacro{\tmp}{0.1*\x}
\draw[color=white!\x!black] (5,0) circle (\tmp cm);
\end{tikzpicture}
\end{document}


Another option as suggested by Torbjørn T. is to use evaluate:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach [evaluate=\x as \y using 100-20*\x] \x in {1,1.5,...,4}
\draw[color=blue!\y!green] (0,0) circle (\x);
\end{tikzpicture}
\end{document}

-
In your last example every second circle is the same size as the previous because you've used \pgfmathtruncatemacro, use \pgfmathsetmacro instead. You could also add a solution with evaluate, i.e. \foreach [evaluate=\x as \y using 100-10*\x] \x in {1,1.5,...,8} \draw[color=black!\y!white,very thick] (0,0) circle (\x); – Torbjørn T. Aug 7 '14 at 5:51
@TorbjørnT. My bad. I didn't notice it. Now corrected. I have added evaluate option too. Many thanks for both suggestions :-) – Harish Kumar Aug 7 '14 at 7:03

With \multido you can define more than one variable:

\documentclass{article}
\usepackage{tikz,multido}
\begin{document}
\begin{tikzpicture}
\multido{\r=1+0.5,\i=80+-5}{8}{%
\draw[color=blue!\i!green] (0,0) circle (\r);}
\end{tikzpicture}
\end{document}

-