[Tex/LaTex] Passing an array type argument for new command

foreachloopsmacrostikz-pgf

I've looked around and can't seem to find anywhere, but I was wondering if there is somewhere you can do something like the following:

\newcommand{\loopy}[1]{
    \begin{tikzpicture}
        \foreach \x in #1 {
            \foreach \y in \x {
                \node at (\y_1,\y_2);
            }
        }
    \end{tikzpicture}
}

\loopy{{{1,2},{2,3}}}

Then the 'loopy' command would go through and plot nodes at (1,2) and (2,3). I'm not sure this is even possible or how you would go about doing it?

Additionally, would there be a way to see the number of elements within a number? So say I pass in

\loopy{{{1,2,3},{2,3}}}

Is there a way to know that the first set has 3 elements and the second set has 2?

Edit (Adding more description)

The above example was to simplistic, so I'll make a more 'complex' example to show kinda what I'm trying to do:

\newcommand{\loopy}[1]{
    \begin{tikzpicture}
        \foreach \x in #1 {
            \if \x has 2 elements {
                \foreach \y in \x {
                    \node at (\y_1,\y_2);
                }
            }
            \else if \x has 3 elements {
                \foreach \y in \x {
                    \node at (\y_1,\y_2) {\y_3};
                }
            }
            \else if \x has 4 elements {
                \foreach \y in \x {
                    \node at (\y_1,\y_2);
                    \node at (\y_3,\y_4);
                }
            }
        }
    \end{tikzpicture}
}

\loopy{{{1,2},{1,2,3},{2,3,4,5}}}

Best Answer

The question seems a bit ill-defined, but one can indeed parse a general list in a nested way, and do different things based on the list length of each successive rank-1 list. Here

\loopy{1,2: 2,3,7: 2,4,4,5}

will place a 2 at location (1,2), since it was a 2-element list. It will place a 3_7 at location (2,3), since 7 was the third element of the list starting with the coordinates (2,3). Finally, it will place a 4_1 and 4_2 at (2,4) and (4,5) since the 4 element list began with 2,4 and concluded with 4,5.

\documentclass{article}
\usepackage{listofitems,tikz}
\newcommand{\loopy}[1]{%
  \setsepchar{:/,}%
  \readlist\mylist{#1}%
    \begin{tikzpicture}
        \foreachitem\x\in\mylist[]{%
          \ifnum\listlen\mylist[\xcnt]=2\relax
            \node at (\mylist[\xcnt,1],\mylist[\xcnt,2]){$2$};
          \else
            \ifnum\listlen\mylist[\xcnt]=3\relax
              \node at (\mylist[\xcnt,1],\mylist[\xcnt,2]){$3_{\mylist[\xcnt,3]}$};
            \else
              \ifnum\listlen\mylist[\xcnt]=4\relax
                \node at (\mylist[\xcnt,1],\mylist[\xcnt,2]){$4_1$};
                \node at (\mylist[\xcnt,3],\mylist[\xcnt,2]){$4_2$};
              \fi
            \fi
          \fi
        }
   \end{tikzpicture}
}
\begin{document}
\loopy{1,2: 2,3,7: 2,4,4,5}
\end{document}

enter image description here