Global variable Array
You could use pgffor
for that.
\documentclass[a4paper,twoside,12pt]{article}
\usepackage{pgffor}
\usepackage{xcolor}
\begin{document}
\def\ArrayNames{{"koala","duck","marmot","penguin","bear"}}
\def\ArrayColors{{"gray","yellow","blue","red","brown"}}
\begin{enumerate}
\foreach \X in {0,...,4}
{\pgfmathsetmacro{\myname}{\ArrayNames[\X]}
\pgfmathsetmacro{\mycolor}{\ArrayColors[\X]}
\item \textcolor{\mycolor}{\myname}}
\end{enumerate}
\end{document}
Or with an external file (which I create here for the convenience of others in the MWE, but you may drop the filecontents stuff as long you have a data file).
\documentclass[a4paper,twoside,12pt]{article}
\usepackage{filecontents}
\begin{filecontents*}{myarrays.tex}
\def\ArrayNames{{"koala","duck","marmot","penguin","bear"}}
\def\ArrayColors{{"gray","yellow","blue","red","brown"}}
\end{filecontents*}
\usepackage{pgffor}
\usepackage{xcolor}
\begin{document}
\input{myarrays.tex}
\begin{enumerate}
\foreach \X in \ArrayNames
{\foreach \Y [count=\Z starting from 0]in \X
{\pgfmathsetmacro{\mycolor}{\ArrayColors[\Z]}
\pgfmathsetmacro{\myname}{\ArrayNames[\Z]}
\item \textcolor{\mycolor}{\myname}}}
\end{enumerate}
\end{document}
And of course you can install any mapping. Here is an example (in which I assume you already have created myarrays.tex
, if not uncomment the corresponding lines).
\documentclass[a4paper,twoside,12pt]{article}
% \usepackage{filecontents}
% \begin{filecontents*}{myarrays.tex}
% \def\ArrayNames{{"koala","duck","marmot","penguin","bear"}}
% \def\ArrayColors{{"gray","yellow","blue","red","brown"}}
% \end{filecontents*}
\usepackage{pgffor}
\usepackage{xcolor}
\begin{document}
\input{myarrays.tex}
\pgfkeys{/pgf/declare function={mymap(\x)=int(mod(1+\x*\x,4));}}
\begin{enumerate}
\foreach \X in \ArrayNames
{\foreach \Y [count=\Z starting from 0]in \X
{\pgfmathsetmacro{\mycolor}{\ArrayColors[mymap(\Z)]}
\pgfmathsetmacro{\myname}{\ArrayNames[\Z]}
\item \textcolor{\mycolor}{\myname}}}
\end{enumerate}
\end{document}
As you can see, this function is chosen such that it distinguishes between birds on the one hand and mammals and beings on the other hand.
Trivial with listofitems
.
\documentclass{article}
\usepackage{listofitems,xcolor}
\newcommand\ArrayNames{Name1,Name2,N3,N4,N5,N6,N7,N8,N9,Name10}
\newcommand\ArrayColors{red,blue,cyan,cyan!50!red,red!50,
purple,green,yellow,blue!50,magenta}
\readlist*\arrayname{\ArrayNames}
\readlist*\arraycolor{\ArrayColors}
\begin{document}
\begin{enumerate}
\foreachitem\x\in\arrayname{\item \textcolor{\arraycolor[\xcnt]}{\x}}
\end{enumerate}
\textcolor{\arraycolor[4]}{\arrayname[9]}
\textcolor{\arraycolor[5]}{\arrayname[8]}
\end{document}
You can have files with the following structure
\colorlist{
{red}
{green}
{blue}
{green!40!yellow}
{-green!40!yellow}
{rgb:-green!40!yellow,3;green!40!yellow,2;red,1}
{red>wheel,12}
}
It would also be possible to have the simpler structure
red
green
blue
green!40!yellow
- green!40!yellow rgb:-green!40!yellow,3;green!40!yellow,2;red,1 red>wheel,12
if preferred. Then in the document you call
\assigncolors{<filename>}{<list of names for the colors>}
Here's the code, using filecontents
for making the example self-contained; the name for the color list file can be whatever you prefer.
\begin{filecontents}{\jobname.colors}
\colorlist{
{red}
{green}
{blue}
{green!40!yellow}
{-green!40!yellow}
{rgb:-green!40!yellow,3;green!40!yellow,2;red,1}
{red>wheel,12}
}
\end{filecontents}
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\assigncolors}{mm}
{% #1 = file name, #2 = list of names
\clist_set:Nn \l__tisaigon_colornames_clist { #2 }
\file_input:n { #1 }
}
\NewDocumentCommand{\colorlist}{m}
{
\int_zero:N \l__tisaigon_colornames_int
\tl_map_inline:nn { #1 }
{
\int_incr:N \l__tisaigon_colornames_int
\colorlet
{
\clist_item:Nn \l__tisaigon_colornames_clist { \l__tisaigon_colornames_int }
}
{
##1
}
}
}
\clist_new:N \l__tisaigon_colornames_clist
\int_new:N \l__tisaigon_colornames_int
\ExplSyntaxOff
\assigncolors{\jobname.colors}{
color1,
color2,
color3,
header,
body,
footer,
fancy,
}
\begin{document}
\textcolor{color1}{Abc}
\textcolor{color2}{Abc}
\textcolor{color3}{Abc}
\textcolor{header}{Abc}
\textcolor{body}{Abc}
\textcolor{footer}{Abc}
\textcolor{fancy}{Abc}
\end{document}