将背景环绕在标签周围

将背景环绕在标签周围

我创建了以下 tikz 图形。

我希望灰色背景区域也包含两个节点pk和的标签Ak

有没有办法做到这一点?

我的身影

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{bm}
\usepackage{stmaryrd}
\pgfplotsset{compat=1.12}
\usetikzlibrary{arrows,petri,backgrounds}
\tikzset{>=stealth',every on chain/.append style={join}, every join/.style={->}}
\begin{document}
\begin{tikzpicture}[thick,auto,node distance=1cm]
    \tikzstyle{n}=[circle,draw=blue!75,fill=blue!20,minimum size=10pt,inner sep=0pt]
    \tikzstyle{pil}=[->,thick,shorten <=2pt,shorten >=2pt]

    \tikzstyle{nlegend}=[circle,draw=blue!75,fill=blue!20,minimum size=5pt,inner sep=0pt]

    \node [n] (Z) [label=right:{$Z \sim \mathcal{N}(\bm{\mu}, \bm{\Sigma})$}] {};
    \node [n] (pk) [below of=Z,label=right:{$p_k = \texttt{logistic}(\langle \beta_k, Z \rangle))$},tokens=1] {};
    \node [n] (bk) [left of=pk,label=below:$\beta_k$,tokens=1] {};
    \node [n] (Ak) [below of=pk,label=right:$A_k \sim \mathcal{B}(p_k)$] {};
    \node (plate) [below of=bk] {\scriptsize$k \in V$};

    \path[pil] (Z) edge (pk);
    \path[pil] (bk) edge (pk);
    \path[pil] (pk) edge (Ak);

    \node [n] (nl) [label=right:deterministic,tokens=1,below of=plate] {};

    \begin{pgfonlayer}{background}
    \filldraw [line width=4mm,join=round,black!10]
    (pk.north  -| plate.west)  rectangle (Ak.south  -| pk.east);
    \end{pgfonlayer}
\end{tikzpicture}

\end{document}

答案1

如果要绘制围绕某些节点的形状,请考虑fitTikZ 库。一旦你为标签pk节点上,您可以确保形状包含它。

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{amsfonts}
\usepackage{bm}
\usetikzlibrary{arrows,petri,backgrounds,fit}
\tikzset{>=stealth',every on chain/.append style={join}, every join/.style={->}}
\begin{document}
\begin{tikzpicture}[thick,auto,node distance=1cm]
    \tikzstyle{n}=[circle,draw=blue!75,fill=blue!20,minimum size=10pt,inner sep=0pt]
    \tikzstyle{pil}=[->,thick,shorten <=2pt,shorten >=2pt]

    \tikzstyle{nlegend}=[circle,draw=blue!75,fill=blue!20,minimum size=5pt,inner sep=0pt]

    \node [n] (Z) [label=right:{$Z \sim \mathcal{N}(\bm{\mu}, \bm{\Sigma})$}] {};
    \node [n] (pk) [below of=Z,label={[name=pklabel]right:{$p_k = \texttt{logistic}(\langle \beta_k, Z \rangle))$}},tokens=1] {};
    \node [n] (bk) [left of=pk,label=below:$\beta_k$,tokens=1] {};
    \node [n] (Ak) [below of=pk,label=right:$A_k \sim \mathcal{B}(p_k)$] {};
    \node (plate) [below of=bk] {\scriptsize$k \in V$};

    \path[pil] (Z) edge (pk);
    \path[pil] (bk) edge (pk);
    \path[pil] (pk) edge (Ak);

    \node [n] (nl) [label=right:deterministic,tokens=1,below of=plate] {};

    \begin{pgfonlayer}{background}
    \node[draw=black!10,fill=black!10,rectangle,rounded corners,fit=(plate) (pklabel)] {};
    \end{pgfonlayer}
\end{tikzpicture}

\end{document}

示例代码输出

答案2

标签可以命名。对于背景区域,使用适合其上方选定节点的节点是明智的:

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{mathtools, amssymb}
\usepackage{bm}
\usepackage{stmaryrd}
\usetikzlibrary{arrows,
                backgrounds,
                chains,
                fit,
                petri}
\tikzset{>=stealth',
every on chain/.append style={join= by pil},
    n/.style = {circle,draw=blue!75,fill=blue!20,minimum size=10pt,inner sep=0pt,
                node contents={}},
  pil/.style = {->,thick,shorten <=2pt,shorten >=2pt},
  FIT/.style = {rounded corners, fill=gray!30, fit=#1}
          }

\begin{document}
    \begin{tikzpicture}[thick,auto,
node distance = 1cm, 
  start chain = going below,
every label/.append style = {align=center}
                        ]
\node (Z)  [n,on chain,
            label=right:{$Z\sim\mathcal{N}(\bm{\mu},\bm{\Sigma})$}];
\node (pk) [n,on chain,
            label={[name=lbl-pk]right:
                   $p_k=\texttt{logistic}(\langle\beta_k, Z\rangle))$},
            tokens=1];
\node (Ak) [n,on chain,
            label=right:$A_k \sim \mathcal{B}(p_k)$];
%
\node (bk) [n, left of=pk,
            label={[name=lbl-bk]below:$\beta_k$,\\
                  \scriptsize$k \in V$},
            tokens=1];
\node (nl) [n, below=of lbl-bk,
            label=right:deterministic,tokens=1];
%
\draw[pil] (bk) -- (pk);
 %
\scoped[on background layer]
    \node[FIT= (lbl-bk) (lbl-pk) (Ak)] {};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容