# tikz - place nodes independently of each other

I'm wondering no one seemed to have this problem so far, but here it is.

This piece of code is supposed to produce four nodes with a green box in the background:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds, positioning}

\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}
% normal nodes
\node [draw,             ] (a) {a};
\node [draw, right = of a] (b) {b};
\node [draw, below = of b] (c) {c};
\node [draw, left  = of c] (d) {d};

% background node
\begin{scope}[on background layer]
\node [draw=none, minimum height=3cm, minimum width=3cm, fill=green] {};
\end{scope}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}


But the green box causes my nodes to move:

I tried a whole lot of things with absolute positioning, anchoring, aligning and shifting, but the graphics I want to build are a bit more complex than this MWE, and every time I go and change a bit, like adding another colored box or changing the size of one, everything is messed up again. How do I tell TikZ which nodes it should layout together and which independently from the others?

EDIT: Some additional information according to the comments: I don't want the box necessarily to be symmetrically around the nodes, I just want it not to change the position of the other nodes. The goal is to tell TikZ to place the a node as there was nothing else in the figure and to place the green box as there was nothing else in the figure as well. Currently, when I, for example, increase the size of the green box, it pushes all other nodes more to the bottom right, instead of consuming more space behind the other nodes.

• the green box is supposed to be symmetrically around the nodes? Dec 15, 2016 at 11:18
• The first node drawn is a, centered at (0,0) because you do not specify a coordinate. Same thing happens to the green one, which is drawn centered at (0,0) again. So it's kind of expected --- methink. Dec 15, 2016 at 11:37
• ...and it seems that the nodes move because the bounding box of the figure changes. Dec 15, 2016 at 11:48
• I don't quite understand what you mean. Coordinates inside a tikzpicture are only related to other coordinates in the tikzpicture, and after all the elements are drawn, LaTeX just sees a big box that it places on the page/slide, the same way it would the letter X, or an \includegraphics. If you don't want the green node to influence the bounding box of the diagram, so that LaTeX doesn't see it when placing the diagram on the slide, add overlay to the scope options. Dec 15, 2016 at 11:48
• You can also use the calc package ($(a)!.5$(c)\$) to locate the center, or \path (a) -- (c) node[midway,... Dec 15, 2016 at 14:38

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
\begin{frame}
\begin{figure}
\begin{tikzpicture}
% normal nodes
\node [draw,             ] (a) {a};
\node [draw, right = of a] (b) {b};
\node [draw, below = of b] (c) {c};
\node [draw, left  = of c] (d) {d};
% background node
\begin{scope}[on background layer]
\node [draw=none, minimum height=3cm, minimum width=3cm, fill=green, fit=(a) (c)] {};
\end{scope}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}


gives by "magic" of fit library the following rezult:

Purpose of adustbox is not very clear, but I left it as you have in your MWE. Also the \begin{frame} and specially \end{frame} had to be on beginning of editor lines.

For this you only need to use one more node for background color. In MWE below are defined styles for nodes which on hand simplify code but on the other it require frame option fragile. I also omit adjust box since it is not relevant to your problem.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
\begin{frame}[fragile]
\begin{figure}
\begin{tikzpicture}[
box/.style = {draw, semithick, minimum size=6mm, outer sep=0mm},
fitbox/.style = {fill=#1, inner sep=3mm, semitransparent}
]
% normal nodes
\node [box,             ] (a) {a};
\node [box, right = of a] (b) {b};
\node [box, below = of b] (c) {c};
\node [box, left  = of c] (d) {d};
% background node
\begin{scope}[on background layer]
\node [fitbox=green!80, fit=(a) (d) (b.west)] {};
\node [fitbox=red!80,   fit=(b.north east) (c.south east)] {};
\end{scope}
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

• Pretty close to what I want! The only problem is - let's say, the colored boxes indicate a membership of the nodes to a certain group. Now, nodes can be in multiple groups, and I want the green box only consume half of the space behind b and c and have the other half of the space consumed by a red box... Dec 15, 2016 at 16:21
• this is simple. green box should fit b and c and red one a and d, I will edit my answer with this option. Dec 15, 2016 at 16:23
• I think, you might have gotten me wrong (or I explained it bad): I want the green box behind a, d, the half of b and the half of c, so I have two colors in the background of b and c` Dec 15, 2016 at 16:32
• ojej, i need correct my addendum :( Dec 15, 2016 at 16:45