Tikz background color of node multilayer

I am trying to draw two nodes who are on a different layer. The background layer consists of the 'big' node and the foreground layer consist of the 'small' node. Only the big node can have a background colour without affecting the background colour of the small one. Below is an image that is produced by the MWE. the 'small' node and it's background should be white.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{backgrounds,scopes}
\usetikzlibrary{arrows,positioning,shapes.geometric}

\begin{document}

\begin{tikzpicture}[remember picture]

{[on background layer]
\node (rect) at (4,2) [draw,thick,minimum width=2cm,minimum height=2cm, fill=red!30] {};
}

\node (smallRect) at (4,2) [draw,thick,minimum width=1cm, minimum height=1cm] {Small};

\end{tikzpicture}

\end{document}


I thought that the on background layer provided by the backgrounds TikZ library should work, but am I misunderstanding it?

• Normal node behavior is to have no background color. Try adding fill=white to the small square. – Alain Merigot Sep 27 '19 at 10:33
• Hi, welcome to TeX.sx. Did you try adding fill=white to the small node? By default a node is transparent. – Fritz Sep 27 '19 at 10:33
• Adding the fill=white solved the issue. I did not know that nodes are transparent by default. Thank you very much! – Zhendos Sep 27 '19 at 11:05

I would not fill the smaller node white. This is because if you have something behind, it will be overpainted. Rather, I'd like to suggest to use even odd rule to spare the smaller node from being filled.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usetikzlibrary{backgrounds,scopes}

\begin{document}

\begin{tikzpicture}[remember picture]

\node (rect) at (4,2) [draw,thick,minimum width=2cm,minimum height=2cm] {};
\node (smallRect) at (4,2) [draw,thick,minimum width=1cm, minimum height=1cm] {Small};
{[on background layer]
\fill[even odd rule,red!30] (rect.south west) rectangle (rect.north east)
(smallRect.south west) rectangle (smallRect.north east);
}
\end{tikzpicture}
\end{document}


For your nodes you not need to use background layer. Nodes are drawn in order as they are code. So, if you first write bigger node with red fill color and than smaller one with white fill, you will get the following result:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
box/.style args = {#1/#2}{draw, fill=#1, minimum size=#2}
]
\node (rect)        [box=red!30/2cm] {};
\node (smallRect)   [box=white/1cm] {Small};
\end{tikzpicture}
\end{document}


Addendum: Inspired by @Schrödinger's cat answer ...

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
box/.style = {minimum size=#1}
]
\node   (Rout)    [box=2cm]   {};
\node   (Rin)     [box=1cm]   {Small};
\draw[even odd rule,fill=red!30]      % borrowed from @Schrödinger's cat answer
(Rout.south west) rectangle (Rout.north east)
(Rin.south west)  rectangle (Rin.north east);
\end{tikzpicture}
\end{document}


result is the same as before.

• The second code will not give the desired result if, say, the outer node has a different draw color than the inner one. That is to say that there is a reason why I wrote my answer in the way I did. – user194703 Sep 27 '19 at 15:41
• @Schrödinger'scat, I suspect that this is reason why you write code on such a way :-). However, about this OP didn't provide any information, so I took opportunity to make small exercise ... – Zarko Sep 27 '19 at 15:47

You do not even need two \nodes to achieve this result ! Just use the properties of double lines...

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node[double=red!30,double distance=10pt,draw=black,fill=white,minimum size=1.5cm] (smallRect)    {Small};
\end{tikzpicture}
\end{document}