Tikz background color of node multilayer

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:

enter image description here

\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.


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}

enter image description here


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}            

enter image description here

Tags:

Tikz Pgf