# tikz: How to pass a list as a parameter of a tikz macro and use it in a foreach inside the macro

I am trying to write a macro for drawing some graph, but since the content of each node is specific it has somehow to be passed as a list to the macro.

Here is an example of an ad-hoc code of what I expect (see the part in the begin tikzpicture) and see the corresponding macro (but that does not contain a second argument for passing the list of information which should be print in each node).

\documentclass{book}
\usepackage{calc}
\usepackage[tikz]{bclogo}
\begin{document}

\newcounter{local}

\def\graphcircuit#1{
\setcounter{local}{#1 - 1}
\begin{scope}
\foreach \i in {1,...,#1}
\node [circle, draw] (n-\i) at (\i*360/#1-360/#1:#1*0.2) {\scriptsize$\i$};
\foreach \i [evaluate={\j=int(\i+1);}] in {1,...,\value{local}}
\draw[>=stealth,semithick,->] (n-\i) to[bend angle=18,bend right] (n-\j);
\draw[>=stealth,semithick,->] (n-#1) to[bend angle=18,bend right] (n-1);
\end{scope}
}

\begin{tikzpicture}
\foreach \i/\v in {1/{1:5},2/{7:2},3/{3:0},4/{4:0},5/{9:1}}
\node [circle, draw] (n-\i) at (\i*360/5-360/5:5*0.2) {\scriptsize$\v$};
\foreach \i [evaluate={\j=int(\i+1);}] in {1,...,4}
\draw[>=stealth,semithick,->] (n-\i) to[bend angle=18,bend right] (n-\j);
\draw[>=stealth,semithick,->] (n-5) to[bend angle=18,bend right] (n-1);
\end{tikzpicture}

\end{document}

• Perhaps an example of what you would pass to be printed in the nodes would be useful. – egreg Dec 24 '14 at 11:31
• You would pass to the macro a second parameter containing the thing to put in each node, for instance if we have five nodes: {{1:5},{7:2},{3:0},{4:0},{9:1}} – Nicolas Beldiceanu Dec 24 '14 at 11:54
• Sorry, but I can't understand. – egreg Dec 24 '14 at 12:02
• You would 1. add a second argument (#2) to the macro \graphcircuit, 2. call the macro (\graphcircuit{5}{{1:5},{7:2},{3:0},{4:0},{9:1}}), and 3. have to modify the code of the macro so that you dont print \i in the node (see first foreach) but rather the element of the list. – Nicolas Beldiceanu Dec 24 '14 at 12:08

If I understand well your question, here is a macro that draws your circular graph based on your set of node values.

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}

\newcommand{\graphcircuit}[1]{
\foreach[count=\n] \v in {#1}; % count the number of elements
\pgfmathsetmacro{\r}{\n*0.2} % set the node distance from (0,0)
\pgfmathsetmacro{\b}{90/\n} % evaluate the bend angle
\foreach[count=\i, evaluate=\i as \a using (\i-1)*360/\n] \v in {#1}
\node [circle, draw, font=\scriptsize] (n-\i) at (\a:\r) {$\v$};
\foreach \i [remember=\i as \j (initially \n)] in {1,...,\n}
\draw[semithick,-stealth] (n-\j) to[bend right=\b] (n-\i);
}

\begin{document}
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}
\scoped[every node/.style={fill=yellow}]
\graphcircuit{a,...,l};
\end{tikzpicture}
\end{document}


NOTE: I have done some adjustment in your tikz code.

EDIT: If you accept to use LuaLaTeX you can do the following to draw your circular graph :

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}

\begin{document}
\tikz \graph [simple necklace layout, node distance=0cm, nodes={circle,draw}]
{ 1:5->7:2->3:0->4:0->9:1->1:5};
\end{document}