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{adjustbox}
\usepackage{tikz}
\usetikzlibrary{backgrounds, positioning}
\begin{document}
\begin{frame}
\begin{figure}
\begin{adjustbox}{max totalsize={\textwidth}{\textheight}, left}
\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{adjustbox}
\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.
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.tikzpicture
are only related to other coordinates in thetikzpicture
, 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 letterX
, 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, addoverlay
to thescope
options.