1

I have some nodes in a plate diagram that need different background colors. Ideally the whole thing would be colored:

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{bayesnet}

\begin{tikzpicture}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\node[obs]  (x)    {$x_i$};
\node[latent, above=15pt of x]  (z)   {$z_i$};
\node[latent, above left=5pt and 25pt of z] (pi)   {$\pi$};
\node[latent, below left=5pt and 25pt of x] (theta)  {$\theta$};

\edge{pi}{z};
\edge{theta}{x};
\edge{z}{x};

{\tikzset{plate caption/.append style={below right=1pt and 1pt of #1.south east}}
\plate[inner sep=0.35cm]{zwn}{(z)(x)}{\scriptsize{$10$}};}

\begin{pgfonlayer}{background}

% this needs to be something like "<10pt above left of pi> rectangle <50pt below right of z>"
\fill [orange!20] (pi) rectangle (z);

% this needs to be something like "<10pt below left of theta> rectangle <50pt above right of x>"
\fill [purple!20] (theta) rectangle (x);

\end{pgfonlayer}
\end{tikzpicture}

\end{document}

Output:

looks dumb

What I want:

enter image description here

2

Welcome! You can use the node anchors, and add additional shifts where needed.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\node[obs]  (x)    {$x_i$};
\node[latent, above=15pt of x]  (z)   {$z_i$};
\node[latent, above left=5pt and 25pt of z] (pi)   {$\pi$};
\node[latent, below left=5pt and 25pt of x] (theta)  {$\theta$};

\edge{pi}{z};
\edge{theta}{x};
\edge{z}{x};

\tikzset{plate caption/.append style={below right=1pt and 1pt of #1.south east}}
\plate[inner sep=0.35cm]{zwn}{(z)(x)}{\scriptsize{$10$}};

\begin{pgfonlayer}{background}

% this needs to be something like "<10pt above left of pi> rectangle <50pt below right of z>"
\fill [orange!20] ([xshift=-2ex,yshift=2ex]pi.north west) 
    rectangle ([xshift=1ex]zwn.east);

% this needs to be something like "<10pt below left of theta> rectangle <50pt above right of x>"
\fill [purple!20] ([xshift=-2ex,yshift=-2ex]theta.south west) 
    rectangle ([xshift=1ex]zwn.east);

\end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

1
  • Perfect, thanks! – mkk Apr 16 '20 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.