# Positioning groups of tikz nodes relative to their centre

I'm trying to draw a tikzpicture in which the centre of a group of nodes is at a given position relative to the centre of another group of nodes.

I've tried to use fit, pic, and I've looked at other answers like this, this and this. So far I've been able to position a single node relative to the centre of a pic, but I haven't been able to draw a pic with its centre at a given position.

Here's a MWE. I'd like to have my picture such that the centre of nodes d,e,f is to the right of (and vertically aligned with) the centre of nodes a,b,c. Additionally, I'd also like to add one equation below each group such that both equations are also vertically aligned.

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

\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,inner sep=1pt,minimum size=15pt}]
%% First group of nodes
\node (a) {};
\node[below left=of a] (b) {};
\node[below right=of a] (c) {};
\path[-latex] (a) edge (b)
(a) edge (c);

%% Second group of nodes
\node at (3,0) (d) {};
\node[right=of d] (e) {};
\node[right=of e] (f) {};
\path[-latex] (d) edge (e)
(e) edge (f);
\end{tikzpicture}
\end{document}


One option I've thought of is to make separate tikzpictures for each group, then making a table in the main body of the document. However, this is suboptimal for a few reasons and does not generalise, so if possible I'd like to have a solution in a single tikzpicture.

Any thoughts? Thanks!

It looks to me as if you were looking for local bounding box.

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

\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,inner sep=1pt,minimum size=15pt}]
%% First group of nodes
\begin{scope}[local bounding box=group 1]
\node (a) {};
\node[below left=of a] (b) {};
\node[below right=of a] (c) {};
\path[-latex] (a) edge (b)
(a) edge (c);
\end{scope}
\node[below=1mm of group 1,rectangle,draw=none] (E1) {$E=mc^2$};
%% Second group of nodes
\begin{scope}[local bounding box=group 2]
\node[right=1cm of group 1] (d) {};
\node[right=of d] (e) {};
\node[right=of e] (f) {};
\path[-latex] (d) edge (e)
(e) edge (f);
\end{scope}
\node[rectangle,draw=none] (E2) at (E1-|group 2.center){$E=mc^2$};
\end{tikzpicture}
\end{document}


• Thanks! This looks promising. I've had a quick search for "local bounding box" in the TikZ and PGF manual, but nothing came up. Do you have any links to read more about it? – Pedro Mediano Nov 20 '18 at 12:53
• @PedroMediano Of course, it's described on p. 1033 of the pgfmanual. (I found this just by searching for this, do you look at an older version of the manual?) – user121799 Nov 20 '18 at 15:56

Something like this?

What have I done? I've declared an auxiliary coordinate on central point between top and bottom nodes in first picture. After that I've used this coordinate as reference for first node on second group. As all nodes in second group are placed according the first one, all of them move to their correct position. And something similar with lower equations.

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

\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,inner sep=1pt,minimum size=15pt}]
%% First group of nodes
\node (a) {};
\node[below left=of a] (b) {};
\node[below right=of a] (c) {};
\path[-latex] (a) edge (b)
(a) edge (c);
\draw[densely dotted, gray!70] (a)-|(c) coordinate[pos=.75] (aux);
\fill (aux) circle (1pt);
\draw[densely dotted, gray!70,->] (aux)--++(1,0) node[above, midway] {1cm};
%% Second group of nodes
\node[right=1cm of aux] (d) {};
\node[right=of d] (e) {};
\node[right=of e] (f) {};
\path[-latex] (d) edge (e)
(e) edge (f);

\node[draw=none, rectangle, minimum size=0pt, below=.5cm of a|-c.south] (eq) {$E=mc^2$};

\node[draw=none, rectangle, minimum size=0pt, ] at (eq-|e) {$E=mc^2$};

\end{tikzpicture}
\end{document}


At the end I found something similar to what I wanted in this answer. I decided to go for a matrix of nodes in which all the nodes have align=center, anchor=center, and each node is a tikzpicture. This is not as general as I would have liked, but 1) it still gives the flexibility to set column and row sep, and 2) I've already wasted invested enough time in tikz for today [:

Fixed MWE below.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,matrix}

\tikzstyle{foo} = [draw,circle,inner sep=1pt,minimum size=15pt]

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes={align=center, anchor=center}] {

%% First group of nodes
\begin{tikzpicture}
\node[foo] (a) {};
\node[foo, below left=of a] (b) {};
\node[foo, below right=of a] (c) {};
\path[-latex] (a) edge (b)
(a) edge (c);
\end{tikzpicture}

&

%% Second group of nodes
\begin{tikzpicture}
\node[foo] at (3,0) (d) {};
\node[foo, right=of d] (e) {};
\node[foo, right=of e] (f) {};
\path[-latex] (d) edge (e)
(e) edge (f);
\end{tikzpicture}

\\
$E = mc^2$
&
$E = mc^2$
\\ };
\end{tikzpicture}
\end{document}