# TikZ: Get colour from an array and use it for drawing

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

\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

\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!

• I strongly recommend you to read very carefully this absolutely comprehensive answer from egreg about that. Don't use arrayjob with LaTeX. Jul 28 at 21:57
• Thank you @SebGlav, I figured it out. Jul 28 at 22:46

As suggested, using the array functionality of TikZ you can get:

\documentclass{standalone}
\usepackage{tikz}
%\usepackage{arrayjob}
%\newarray\colours
\def\mycolours{{"red","orange","yellow","green","blue","purple"}}

\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate=\i as \usecolor using {\mycolours[\i]}]in {1,2,5} {
\draw[color=\usecolor] (0,\i)--++(1,0);
}
\end{tikzpicture}
\end{document}


• I like this one the best. Thank you! Jul 29 at 3:42

\documentclass[tikz]{standalone}
\pgfkeys{
/benis/.cd,
1/.initial=red,
2/.initial=orange,
3/.initial=yellow,
4/.initial=green,
5/.initial=blue,
6/.initial=purple,
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {1,2,5} {
\pgfkeysgetvalue{/benis/\i}\thiscolor
\draw (\i,\i/2)node[circle,draw=\thiscolor]{\thiscolor};
}
\end{tikzpicture}
\end{document}


• +1 for a nice use of pgfkeys  Jul 29 at 19:32

Thanks to SebGlav, I got something working. I have to type the RGB codes for the colours I want, but that's fine. This code works:

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

\begin{document}
\newcommand{\colours}{{"FF0000","FFA500","FFFF00","008000","0000FF","800080"}}
\begin{tikzpicture}
\foreach \i in {1,2,5} {
\pgfmathsetmacro{\currentcolour}{\colours[\i]}
\definecolor{currentcolour}{HTML}{\currentcolour}
\draw[currentcolour] (0,\i)--++(1,0);
}
\end{tikzpicture}
\end{document}


Array operations of TeX (hence, TikZ) is quite inconvernient. A simple use of arrays of numbers, texts (including colors), points (as pairs of 2 coordinates) is as follows.

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

\begin{document}

% array of numbers a[i], i=0,1,2,3
\def\a{{6,8,11,12}}

% array of texts b[i], i=0,1,2,3
\def\b{{"blue","red","orange","teal"}}

% array of points (coordinates) c[i], i=0,1,2,3
\def\c{{"(1,2)","(1.5,1.5)","(3,2.5)","(5,3)"}}

\begin{tikzpicture}
\draw (0,.5) rectangle (6.5,3.5);
\foreach \i in {0,...,3}{
\pgfmathsetmacro{\ii}{int(\i+1)}
\pgfmathsetmacro{\tmpa}{\a[\i]}
\pgfmathsetmacro{\tmpb}{\b[\i]}
\pgfmathsetmacro{\tmpc}{\c[\i]}
\path (0,-\i) node[right,\tmpb,align=left]
{The \ii-th element is $\tmpa{}$ with \tmpb{} color\\
associated with the point $\tmpc$};
\fill[\tmpb] \tmpc node[above]{\tmpa} circle(2pt);
}
\end{tikzpicture}
\end{document}

• Yes, I tried a lot of array stuff I found online and was getting frustrated. All these comments are great. Thanks! Jul 29 at 22:20