6

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}

enter image description here

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!

4 Answers4

9

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

\documentclass{standalone}
\usepackage{tikz}
%\usepackage{arrayjob}
%\newarray\colours
%\readarray{colours}{red&orange&yellow&green&blue&purple}
\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}

enter image description here

vi pa
  • 3,394
5

pgfkeys can be your array

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

Symbol 1
  • 36,855
3

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}

3

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.

enter image description here

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

Black Mild
  • 17,569