Using the arrayjob
package, I am able to extract strings from an array and, for example, get TikZ to print that text:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{arrayjob}
\newarray\colours
\readarray{colours}{red&orange&yellow&green&blue&purple}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,5} {
\draw (0,\i) node{\colours(\i)};
}
\end{tikzpicture}
\end{document}
However, I want to colour lines with variable colours in the same way. Naively, I try the following code, which does not work:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{arrayjob}
\newarray\colours
\readarray{colours}{red&orange&yellow&green&blue&purple}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,5} {
\draw[\colours(\i)] (0,\i)--++(1,0);
}
\end{tikzpicture}
\end{document}
This is a toy example to illustrate what I want. There are obvious ways to do this in this small example, but not my application. I want to extract the colour from an array by index and use that colour to colour the lines.
EDIT:
Thanks to SebGlav, I got something working. I'll leave that as an answer down below. There are three good solutions in the answers now, thank you everyone!
Best Answer
As suggested, using the array functionality of TikZ you can get: