# Positioning a node with respect to a group of nodes

I have a set of nodes that is generated by a macro, and I would like to position a node with respect to the north, west, south of this set of nodes. In my example I would for instance like to position the node labelled by (A) at the west of the group of nodes generated by the macro \graphcircuit and the problem is that I neither know the size of the generated figure (i.e., it depends of the list that is passed to the macro), nor have some kind of global node I could refer at.

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

\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}
\node[font=\normalsize] (A) at (0,0) {(A)};
\end{tikzpicture}
\end{document}

• "Your code" is the code from my answer tex.stackexchange.com/a/219364/9335. I think that you have to : 1) cite your source, 2) post an image of the result, and if possible of what you want to obtain. – Kpym Dec 26 '14 at 15:21

You can fit the whole graph into a node and refer to it. And you don't need the math library for this.

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}

\newcommand{\graphcircuit}[1]{
\begin{scope}
\def\myfitarray{}
\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);}
\foreach \x in {1,...,\n}{%
\expandafter\xdef\expandafter\myfitarray\expandafter{\myfitarray (n-\x)}}
\node[inner sep=2pt,fit=\myfitarray] (groupnode) {};
\end{scope}
}

\begin{document}
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}
\node[font=\normalsize,anchor=south] (A) at (groupnode.north) {(A)};
\node[font=\normalsize,anchor=east] (B) at (groupnode.west) {(B)};
\begin{scope}[shift={(4,5)}]% Another one
\graphcircuit{1:5,7:2,3:0,4:0,9:1}
\node[anchor=north] (C) at (groupnode.south) {(C)};
\node[anchor=north west] (D) at (groupnode.south east) {(D)};
\end{scope}

\end{tikzpicture}
\end{document}


Based on the OP's code, the first node can be used as the global reference point. Here the label (A) located 1 cm (which is defined by \smacro) to the right of the first node is used for the global reference for the group of nodes. Once this is set, one can use above, below, right, left= xx cm of A and its variants to locate the remaining nodes. However, north, west, south is preferred in this example, the solution uses polar coordinate to place the remaining nodes.

Code

\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{math,calc,positioning}

\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);
}
\def\s{1}      % determine the location of the global reference
\begin{document}
\centering
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}
\node[font=\normalsize,xshift=\s cm] (A) at (n-1) {(A)};
\node[font=\normalsize] (B) at (180:\r+\s) {(B)};
\node[font=\normalsize] (C) at (90:\r+\s)  {(C)};
\node[font=\normalsize] (D) at (-90:\r+\s) {(D)};
\end{tikzpicture}

\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1,1:1,2:2,3:3}

\node[font=\normalsize,xshift=\s cm] (A) at (n-1) {(A)};
\node[font=\normalsize] (B) at (180:\r+\s) {(B)};
\node[font=\normalsize] (C) at (90:\r+\s)  {(C)};
\node[font=\normalsize] (D) at (-90:\r+\s) {(D)};
\end{tikzpicture}

\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1,1:1,2:2,3:3,1:1,2:2,3:3}

\node[font=\normalsize,xshift=\s cm] (A) at (n-1) {(A)};
\node[font=\normalsize] (B) at (180:\r+\s) {(B)};
\node[font=\normalsize] (C) at (90:\r+\s)  {(C)};
\node[font=\normalsize] (D) at (-90:\r+\s) {(D)};
\end{tikzpicture}
\end{document}


SOLUTION 2: Here is another solution using local bounding box.

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

\newcommand{\graphcircuit}[2]{
\begin{scope}[local bounding box=#2]
\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);
\end{scope}
}

\begin{document}
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}{groupnode}
\foreach \a in {north, east, south, west}
\node[circle, fill=red, inner sep=1pt] at (groupnode.\a) [label={\a:\a}]{};
\end{tikzpicture}
\end{document}


SOLUTION 1: Here is a solution without fit library, that draw the graph inside a node. Personally I prefer the fit solution of percusse and my second solution. I give this one for the sake of completeness.

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

\newcommand{\graphcircuit}[2]{
\node[name=#2]{
\begin{tikzpicture}
\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);
\end{tikzpicture}
};}

\begin{document}
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}{groupnode}
\foreach \a in {north, east, south, west}
\node[circle, fill, inner sep=1pt] at (groupnode.\a) [label={\a:\a}]{};
\end{tikzpicture}
\end{document}