# Impossible to use array value

I am trying to use the value of an array in a function. But it does not work. Any ideas?

See below my LaTeX code

\documentclass{article}
\usepackage{color}
\usepackage{arrayjob}

\newarray\Color

\begin{document}

\Color(2)
\textcolor{\the\Color(2)}{moi}

\end{document}

-
Are you using xcolor too for \textcolor? –  percusse Jun 26 '12 at 20:53

It can't work. The command \textcolor does expand its argument looking for a color, but unfortunately the extraction of a value from \Color requires actions that are not allowed when doing "pure expansion".

As the manual explains that you have to do in an indirect way:

\checkColor(2)\textcolor{\cachedata}{moi}


The operation \checkColor extracts the value and places it as the expansion of \cachedata and now the expansion by \textcolor is possible.

Note that you should be using arrayjobx instead of arrayjob, which has serious problems with LaTeX.

## A different strategy with LaTeX3 macros

\documentclass{article}
\usepackage{color,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\xarraynew}{m O{&} m}
{
\xarray_define:nnn {#1} {#2} {#3}
}
\cs_new_protected:Npn \xarray_define:nnn #1 #2 #3
{
\seq_new:c { g_xarray_ #1 _seq }
\seq_gset_split:cnn { g_xarray_ #1 _seq } { #2 } { #3 }
}
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }

\DeclareExpandableDocumentCommand{\xarrayget}{m m}
{
\xarray_get:nn { #1 } { #2 - 1 }
}
\cs_new:Npn \xarray_get:nn #1 #2
{
\seq_item:cn { g_xarray_ #1 _seq } { #2 }
}
\ExplSyntaxOff

\xarraynew{Color}{blue&red&orange&violet}

\begin{document}

\textcolor{\xarrayget{Color}{2}}{moi}

\end{document}


The user level commands are \xarraynew that takes two arguments: the array name and the data. It's possible also to specify a different delimiter, so

\xarraynew{Color}[,]{blue,red,orange,violet}


would be equivalent.

You can extract a value with \xarrayget, which takes again as argument the array name and the position.

-
Ah just seconds ..... :) –  percusse Jun 26 '12 at 21:03
Thanks a lot for your response. I am surprising that Latex3 is working on my configuration. I though that I had only Latex2. –  latex alpha Jun 27 '12 at 20:07
@latexalpha It's really expl3, which will become LaTeX3 in some years. –  egreg Jun 27 '12 at 20:08

As indicated by @egreg, you need to use expandable definitions. I would use a simple expandable lookup as follows:

\documentclass{article}
\usepackage{color}

\def\Colorarray{{1}{blue}{2}{red}{3}{orange}{4}{violet}}

\def\getcolor#1{\expandafter\xgetcolor\Colorarray{#1}{}\getcolor{#1}}

\def\xgetcolor#1#2#3\getcolor#4{%
\ifnum#4=#1 #2\else\xgetcolor#3\getcolor{#4}\fi}

\begin{document}

\textcolor{\getcolor{2}}{moi}

\end{document}

-
Your proposition working fine. But I do not understand it at all –  latex alpha Jun 27 '12 at 20:23
I understand your suggestion using tracingall. Thanks a lot –  latex alpha Jun 27 '12 at 21:09