# [OS X TeX] Tikz loop question

Gary Church gary.church1 at comcast.net
Tue Jun 12 04:35:01 EDT 2007

Hello Tikz experts!

I'm trying to do an apparently unusual thing (as I can't figure out
how to do this :-) )

I've included the "calc" package to get inline mathematical
operations. What I want to do is the the color of my plots to be
based of the value of a looping variable. Consider the code fragment
below:

\begin{tikzpicture}
\draw[help lines] (-5,-5) grid (5,15);
\draw[<->] (-5,0)--(5,0) node[right]  {$x$};
\draw[<->] (0,-5)--(0,15) node[above] {$y$};
\foreach \x in {-5,...,-1,1,2,...,5} \draw (\x cm,-2pt)--(\x cm,
2pt) node[below=4pt] {$\x$};
\foreach \y in {-5,...,-1,1,2,...,15} \draw (-2pt,\y cm)--(2pt,
\y cm) node[left=2pt] {$\y$};
\foreach \w in {1,...,5} \draw[thick,color=black!\w*10,<->] plot
[domain=-3:3]			%NOTE THIS LINE!
function{x**2 - \w} node[right,red] {$y=x^2 - \w$};
\foreach \w in {1,...,5} \draw[thick,<->] plot[domain=-3:3]
function{x**2 + \w}
node[right] {$y=x^2 + \w$};
\draw[thick,<->] plot[domain=-3:3] function{x**2}
node[right] {$y=x^2$};
\end{tikzpicture}

On the indicated line with the comment you'll see "color = black!
\w*10". My intention is that the curves whose equations are:

y = x^2 - 1, y = x^2 - 2, ..., y = x^2 - 5

are drawn in: black!10, black!20, ..., black!50 respectively.

This doesn't work as I get the error messages:

|147 error| Illegal unit of measure (pt inserted). * ...**2 - \w} node
[right,red] {$y=x^2 - \w$};
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).
|147 error| Illegal unit of measure (pt inserted).

Is what I'm trying to do possible or am I trying to be overly clever?

Thanks much,
Gary.