如何使用 Tikz 的 fit 包含标签?

如何使用 Tikz 的 fit 包含标签?

我正在使用 TikZ 创建图表。它运行良好,但是当我尝试使用它fit来制作背景时,它不够大:

在此处输入图片描述

请注意,绿色没有覆盖标签 Gateway 和 Root。我认为如果有一种方法可以引用节点的标签(类似于我可以执行的方法node.north等),那么应该可以。

这是我正在使用的代码:

\documentclass{article}
\usepackage{tikz}
\begin{document}\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}

\begin{tikzpicture}
    [gateway/.style={circle, fill=blue!20!white, draw=blue, thick, inner sep=0pt,
                     minimum size=6mm, label=below:Gateway},
     root/.style={circle, fill=red!20!white, draw=red, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Root},
     node/.style={circle, fill=gray!20!white, draw=gray, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Node},
     every edge/.style={<->, semithick, draw}, 
     ]
    \node[gateway] (gateway_1) at ( 0, 0) {};
    \node[root]    (root_1)    at (10, 0) {};
    \node[node]    (node_1)    at ( 5, 5) {}
         edge node [auto] {connect} (root_1)
         edge (gateway_1);
    
    \begin{pgfonlayer}{background}
        \node [fill=green!20!white, fit=(gateway_1) (root_1) (node_1)]{};
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

所以当然正如标题所说,我如何在背景中包含标签?

谢谢!

答案1

来自 pgf 手册的相关信息,“建立边界框”部分

有一个节点可以让你获取当前边界框的大小。该current bounding box节点具有矩形形状,其大小始终是当前边界框的大小。

因此,您可以使用此节点的坐标来填充背景:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}

\begin{document}
\begin{tikzpicture}
    [gateway/.style={circle, fill=blue!20!white, draw=blue, thick, inner sep=0pt,
                     minimum size=6mm, label=below:Gateway},
     root/.style={circle, fill=red!20!white, draw=red, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Root},
     node/.style={circle, fill=gray!20!white, draw=gray, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Node},
     every edge/.style={<->, semithick, draw}, 
     ]
    \coordinate[gateway] (gateway_1) at ( 0, 0);
    \coordinate[root]    (root_1)    at (10, 0);
    \coordinate[node]    (node_1)    at ( 5, 5)
         edge node [auto] {connect} (root_1)
         edge (gateway_1);

    \begin{pgfonlayer}{background}
        \fill[green!20!white] (current bounding box.south west) rectangle
            (current bounding box.north east);
    \end{pgfonlayer}
\end{tikzpicture}
\end{document}

注意我还\node用对象替换了您的空对象\coordinate。坐标相当于节点,除了一些小细节,例如没有默认形状。坐标不包含文本,因此不需要一组空括号{}


更新

这是一个更灵活的版本,它使用环境local bounding box参数scope仅为范围的内容计算边界框:

\begin{tikzpicture}
    [gateway/.style={circle, fill=blue!20!white, draw=blue, thick, inner sep=0pt,
                     minimum size=6mm, label=below:Gateway},
     root/.style={circle, fill=red!20!white, draw=red, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Root},
     node/.style={circle, fill=gray!20!white, draw=gray, thick, inner sep=0pt,
                  minimum size=6mm, label=below:Node},
     every edge/.style={<->, semithick, draw},
     background fill/.store in=\bgfill,
     background fill=green!20!white,   % Default value
     fill background/.style={
       local bounding box=bbox,
       execute at end scope={
         \begin{pgfonlayer}{background}
           \coordinate[rectangle,fill=\bgfill,fit=(bbox)];
         \end{pgfonlayer}}
    }]

    \begin{scope}[fill background,background fill=red!20!white]
      \coordinate[gateway] (gateway_1) at ( 0, 0);
      \coordinate[root]    (root_1)    at (10, 0);
      \coordinate[node]    (node_1)    at ( 5, 5)
           edge node [auto] {connect} (root_1)
           edge (gateway_1);
    \end{scope}    

    \begin{scope}[fill background]
      \coordinate[gateway] (gateway_2) at ( 2.5, -2);
      \coordinate[node]    (node_2) at ( 7.5, -2)
           edge (gateway_2);
    \end{scope}    

\end{tikzpicture}

对于每个标有 的范围fill background,边界框都存储在名为 的节点中bboxexecute at end scope用于在范围结束时自动在背景层上创建填充。 键background fill设置为更改 的值,\bgfill以便可以自定义背景颜色。

结果是:

使用范围仅填充图片部分的背景

相关内容