Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Could illustrate what you want to do with some pseudocode example. Also, can TikZ's \foreach do what you want, or do you need something more complicated? –  Caramdir Nov 26 '10 at 19:16
    
added, I'm not sure that'll help but I hope it does. –  EricR Nov 26 '10 at 19:41
    
It helps, because it shows what you want to do and gives a starting point for a solution. I'm not sure though what you intend to do with the first argument (the one you specified as optional with default parameter 'FlowPipe'). –  Caramdir Nov 26 '10 at 20:48
add comment

1 Answer

up vote 4 down vote accepted

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).

share|improve this answer
    
You shouldn't need the braces around first text and second text, since they don't contain commas. –  Antal S-Z Nov 26 '10 at 21:11
1  
@Antal You are right. On the other hand they do not hurt and might make the code more readable. –  Caramdir Nov 26 '10 at 23:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.