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



\setcounter{local}{#1 - 1}
\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);

\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);

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


\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);

    \scoped[every node/.style={fill=yellow}]

enter image description here

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 :


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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