Set tikz labels with a for-loop from an array

tikz-pgf

I want to draw a bunch of nodes in tikz, where the i-th node should be labeled with the i-th string (either math or text) in an array.

For example, if I have the array

\def\array{{$X$, $Y$, $e^{x+z}$, $p$, $t$, $q$}}

I'd like to generate

\node[label={$X$}] (0) at (0, 0) {};
\node[label={$Y$}] (1) at (3, 0) {};
\node[label={$e^{x+z}$}] (2) at (6, 0) {};
\node[label={$p$}] (3) at (9, 0) {};
\node[label={$t$}] (4) at (12, 0) {};
\node[label={$q$}] (5) at (15, 0) {};

but using a for-loop instead, as in my actual use case the array is much larger. I'd be tempted to do something like:

\foreach \i in {0, ..., 5} {
    \node[label={\array[\i]}] (\i) at (3\i, 0) {};

but this does not work. What is the proper way to do this?

I could see in other questions that the evaluate function can be useful in case the labels are to be evaluated as arithmetic, but this is not my case.

More in general the context would simply be something like:

\documentclass[article]
\usepackage{tikz}
\begin{document}
\begin{picture}
    \begin{tikzpicture}
        \def\array{{$X$, $Y$, $e^{x+z}$, $p$, $t$, $q$}}
            \foreach \i in {0, ..., 5} {
                \node[label={\array[\i]}] (\i) at (3\i, 0) {};
            }
    \end{tikzpicture}
\end{picture}
\end{document}

Best Answer

You can step through your array and also have a counter to compute the positions:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
     \def\array{$X$, $Y$, $e^{x+z}$, $p$, $t$, $q$}
     \foreach [count=\i] \x in \array {
            \node[label={\x}] (\i) at (3\i, 0) {};
     }
  \end{tikzpicture}
\end{document}