6

With the code

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}

\begin{tikzpicture}

\foreach \i in {1,...,10} {
    \pgfmathparse{\i * 10-10}
 \draw[red!\pgfmathresult!blue, thick]
(0,\i * .2) -- (1,\i * .2)
;
}

\end{tikzpicture}

\end{document}

one can index colors, but only "between" red and blue. How can I select, out of a list of arbitrary colors the items (they don't have to be 10, 3 will do, so for sake of concreteness, green, red, blue) and read them off in each value of my \foreach statement?

enter image description here

I tried to index them using a list of words, as stays in this question but TikZ doesn't display the colors.

1 Answer 1

11

You can use \foreach \i/\c in {1/green, 2/red, 3/blue} or \foreach \c [count=\i] in {green, red, blue}, which both yield the same result.

In the first example you separate the values and colors with a forward slash /. Since you have the consecutive integers 1, 2 and 3, you can use the count option, which in that case is more intuitive in my eyes.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}

\begin{document}

\begin{tikzpicture}

\foreach \c [count=\i] in {green, red, blue} {
    \pgfmathparse{\i * 10-10}
 \draw[\c, thick]
(0,\i * .2) -- (1,\i * .2)
;
}

\end{tikzpicture}

\end{document}

enter image description here

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.