[Tex/LaTex] Tikz array as argument

loopstikz-pgf

Problem

I'm drawing flow diagrams in Tikz, and they basically include a header, followed by a list of flow components set below an arrow. I've written functions to take arguments in Tikz before, but I'm wondering if there's any way to take in a "list" of arguments and loop through these with a for loop of some sort.

I've tried searching for similar items on google, but haven't gotten very far as I'm not exactly sure how to phrase what I'm looking for.

Pseudocode

\newcommand{\FlowPipe}[3][FlowPipe]{ %first argument is x co-ord, second is array of text
    \newcounter{i}
    \setcounter{i}{0}
    \draw[->] (#1,0) -- (#1+3,0);
    \foreach\argtext in {#2}{
         \node (#1,i) {\argtext};
         \stepcounter{i}
    }
}

Basically, for each item in the array, it should add a node at some specified distance below and put the text of that item there.

Best Answer

The TikZ \foreach macro is handy for situations like this:

\documentclass{article}  
\usepackage{tikz}

\newcommand\FlowPipe[2]{ %first argument is x co-ord, second is array of text
    \draw[->] (#1,0) -- ({#1+3},0);
    \foreach[count=\i] \argtext in {#2} {
         \node[anchor=south west] at (#1,{\i-0.9}) {\argtext};
    }
}

\begin{document}
\begin{tikzpicture}
\FlowPipe{1}{{first text},{second text},{with, comma}}
\end{tikzpicture}
\end{document}

example

See section 56 ("Repeating Things: The Foreach Statement") in the TikZ/PGF v2.10 manual. The count option was introduced in TikZ v2.10, so you might need to update TikZ/PGF. The reason for using count instead of LaTeX counters in that counters are defined globally, and I do not know how to easily get local counters (otherwise a second call to the macro will produce an already defined error).