5

I am trying to change the color within a foreach statement. Here a way is described how to do it depending on the foreach variable. However, I want to use HTML color code, so I decided to use arrayjob. It seems that \definecolor cannot resolve \Color(\the\value{i}). I get the error:

! Incomplete \iffalse; all text was ignored after line 30.
<inserted text> 
                \fi 

Minimalbeispiel:

\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{arrayjob}
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}

\newarray{\Color}
\readarray{Color}
{
006400&%Dunkelgrün
008000&%Grün
228B22&%Waldgrün
2E8B57&%Seegrün
}
\newcounter{i}
\setcounter{i}{1}

\pagestyle{empty}
%%% Info %%%
    \begin{tikzpicture}[x=1cm,y=1cm]
    \foreach \x in {0,9,18}
    {
        \foreach \y in {0,6,12}
        {
            \definecolor{currentcolor}{HTML}{\Color(\the\value{i})}%006400
            \draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
        }
    }
    \end{tikzpicture}
    \newpage
\end{document}
Tomson
  • 53

1 Answers1

5

The arrayjob package should not be used with LaTeX, because it redefines \array; there is the substitute arrayjobx, but it's not useful for this application, because the construction \Color(\the\value{i}) is the set of instructions to print 006400, not the string itself.

You can use the array facilities built in PGF.

\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{xcolor}
\usepackage{tikz}

\begin{document}

\newcommand{\Colors}{{%
"000000",%Schwarz
"8FBC8F",%Dunkles Schiefergrau
"708090",%Schiefergrau
"778899",%Helles Schiefergrau
"B0C4DE",%Helles Stahlblau
"696969",%Mattes Grau
"808080",%Grau
"A9A9A9",%Dunkelgrau
"C0C0C0"%Silber
}}

\newcounter{i}
\setcounter{i}{0}

\pagestyle{empty}
%%% Info %%%

\begin{tikzpicture}[x=1cm,y=1cm]
  \foreach \x in {0,9,18}
    {
     \foreach \y in {0,6,12}
       {
        \pgfmathsetmacro{\thecurrentcolor}{\Colors[\value{i}]}
        \definecolor{currentcolor}{HTML}{\thecurrentcolor}%006400
        \draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
        \stepcounter{i}
       }
    }
\end{tikzpicture}

\end{document}

A different implementation, nearer to how arrayjobx works, is with expl3 and xparse.

\documentclass[12pt]{article}
\usepackage[a4paper, landscape, bottom=17.9mm, left=9.6mm, top=17.9mm, right=9.6mm, nohead, nofoot]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
 { % #1 is the array name, #2 the comma separated values
  \seq_clear_new:c { l_tomson_array_#1_seq }
  \seq_set_split:cnn { l_tomson_array_#1_seq } { , } { #2 }
  \cs_if_exist:cF { #1 }
   {
    \cs_new:cpn { #1 } { \tomson_array_use:nn { #1 } }
   }
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\cs_new:Nn \tomson_array_use:nn
 { % #1 is the array name, #2 is an integer
  \seq_item:cn { l_tomson_array_#1_seq } { #2 }
 }
\ExplSyntaxOff

\definearray{Colors}{
  000000, % Schwarz
  8FBC8F, % Dunkles Schiefergrau
  708090, % Schiefergrau
  778899, % Helles Schiefergrau
  B0C4DE, % Helles Stahlblau
  696969, % Mattes Grau
  808080, % Grau
  A9A9A9, % Dunkelgrau
  C0C0C0  % Silber
}

\newcounter{i}
\setcounter{i}{1}

\begin{document}

\pagestyle{empty}
%%% Info %%%

\begin{tikzpicture}[x=1cm,y=1cm]
  \foreach \x in {0,9,18}
    {
     \foreach \y in {0,6,12}
       {
        \definecolor{currentcolor}{HTML}{\Colors{\value{i}}}% 006400
        \draw[step=1mm,line width=0.1pt,color=currentcolor] (\x-4.255,\y-2.705) grid (\x+4.255,\y+2.705);
        \stepcounter{i}
       }
    }
\end{tikzpicture}

\end{document}

When you do \definearray{Colors}{...} you also define the macro \Colors that takes as argument an integer and extracts the corresponding item from the array.

egreg
  • 1,121,712
  • Thanks a lot you for your proper reply! The array facilities built in PGF are sufficient for my task. The stepcounter{i} needs to be in place of course. – Tomson Dec 20 '15 at 08:46
  • The first example throws an error (the second, more complicated example compiles fine). Do you reproduce the problem? – PatrickT May 27 '16 at 14:41
  • 1
    @PatrickT Something seems to have changed. The counter should be set to 0. – egreg May 27 '16 at 14:47
  • Thank you. I find the first approach quite clear. The relatively new foreach [count=\i] construction is also very clear. I must confess you lost me with your second example. Thanks again! – PatrickT May 27 '16 at 14:55