4

I try below code to iterate the color array. Currently it only show one color.

\documentclass{standalone}
\usepackage{tikz}

\newcommand{\colours}{{"00FF00","0000FF","4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}

\foreach \name [count=\i] in \colours {
  \pgfmathsetmacro{\colour}{\colours[\i-1]}
  \definecolor{mycolor}{HTML}{\colour}
  \fill[mycolor] (\i,0) --++ (1,0) --++ (0,1) --++ (-1,0) --cycle;
}

\end{tikzpicture}
\end{document}

enter image description here

What I want is show all colors defined in the array.

#Update with pgfplots

If put the same into a pgfplots, I saw only the last cycle has been draw on graph:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\newcommand{\colours}{{"4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}[font=\tiny]
    \pgfmathsetmacro\num{dim(\colours)}
    \begin{axis}[
        width  = 6cm,
        height = 4cm,
        ymajorgrids = true,
        xmin=0,xmax=8,
        ymin=0,ymax=1,
        enlarge x limits=.1
    ]
   \pgfplotsinvokeforeach{1,...,\num}{
      \pgfmathsetmacro{\i}{int(#1)}
      \typeout{ \i }
      \pgfmathsetmacro{\colour}{\colours[\i-1]}
      \definecolor{mycolor\i}{HTML}{\colour}
      \fill[mycolor\i] (axis cs:\i,0) -- (axis cs:{\i+1},0) -- (axis cs:{\i+1},1) -- (axis cs:\i,1) -- cycle;
  }
\end{axis}
\end{tikzpicture}
\end{document}

That's more weird: enter image description here

1
  • It would be simpler, if you removed one pair of braces and the quotation marks. The problem is that with two pairs of braces, you essentially have only one element in the list. Feb 27 at 14:16

2 Answers 2

6

pgffor only sees one element in your list: {...}.

If you switch to a syntax without the {} around the list, pgffor can see the individual elements:

\documentclass{standalone}
\usepackage{tikz}

\newcommand{\colours}{00FF00,0000FF,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,0000ff}
\begin{document}
\begin{tikzpicture}

\foreach \name [count=\i] in \colours {
  \definecolor{mycolor}{HTML}{\name}
  \fill[mycolor] (\i,0) rectangle ++(1,1);
}

\end{tikzpicture}
\end{document}

enter image description here


If you would like to keep your syntax, you could wrap another loop around it, which will "unpack" the first layer of {...}:

\documentclass{standalone}
\usepackage{tikz}

\newcommand{\colours}{{"00FF00","0000FF","4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}

\foreach \macro in \colours {
\foreach \name [count=\i] in \macro {
  \pgfmathsetmacro{\colour}{\colours[\i-1]}
  \definecolor{mycolor}{HTML}{\colour}
  \fill[mycolor] (\i,0) --++ (1,0) --++ (0,1) --++ (-1,0) --cycle;
}
}

\end{tikzpicture}
\end{document}
4
  • good answer, one weird thing is when I try to repalce the \name with \colours[\i-1], it only show one color again! ` \definecolor{mycolor}{HTML}{\colours[\i-1]}`
    – lucky1928
    Feb 27 at 14:46
  • @lucky1928 If you want use the \colours[\i-1] syntax, you should better use @PaulGaborit's answer Feb 27 at 14:52
  • Thanks, more weird thing is if use the same in pgfplots, only the last cycle draw, I have updated into my question.
    – lucky1928
    Feb 27 at 19:43
  • @lucky1928 maybe ask a new question to get eyes of some pgfplots experts on this? Feb 27 at 22:52
3

If you want to keep your array, you can use the dim function:

\documentclass{standalone}
\usepackage{tikz}
\newcommand{\colours}{{"00FF00","0000FF","4f81bd","c0504d","9bbb59","8064a2","4bacc6","f79646","0000ff"}}
\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro\nbcolours{dim(\colours)}
  \foreach \i in {1,...,\nbcolours} {
    \pgfmathsetmacro{\colour}{\colours[\i-1]}
    \definecolor{mycolor}{HTML}{\colour}
    \fill[mycolor] (\i,0) --++ (1,0) --++ (0,1) --++ (-1,0) --cycle;
  }
\end{tikzpicture}
\end{document}

enter image description here

2
  • Great, does that means the array define actually is 2 dim? 1xn, but how \colours[\i-1] can access the elements?
    – lucky1928
    Feb 27 at 15:33
  • @lucky1928 The array is only one dimension. The PGF mathematical engine (See pgf manual, part "Mathematical and Object-Oriented Engines" or section "Syntax for Mathematical Expressions: Operators") defines the [] operators to access array. Feb 27 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .