嵌套节点中的 tikz 破折号矩形

嵌套节点中的 tikz 破折号矩形

我想在外部节点中使用虚线矩形,而在内部节点中使用实线矩形。但是下面的示例代码实际上将内部或外部节点的所有节点都变成了虚线。

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[
  inner/.style={draw,fill=blue!5,thick,inner sep=3pt,minimum width=8em},
  outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
  ]
%---------------------------------------------------%
\node[outer] (A) {
    \begin{tikzpicture}[node distance=1cm,outer sep = 0pt]
      \node [inner,minimum width=18em] (A1) {Mr. A};
      \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
      \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
      \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
    \end{tikzpicture}
}; 
\end{tikzpicture}

\end{document} 

输出如下所示:

在此处输入图片描述

我只在外部样式中使用了虚线,但实际上内部节点也有这样的属性!我不想要那个!

答案1

使用fitbackgrounds避免嵌套tikzpicture。Zarko 的答案比我的简单,但我想说明此选项。它在其他情况下可能很有用。

\documentclass[tikz]{standalone}
\usetikzlibrary{fit,backgrounds} % <- added
\begin{document}

\begin{tikzpicture}[
  inner/.style={draw,fill=blue!5,thick,inner sep=3pt,minimum width=8em},
  outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
  ]
%---------------------------------------------------%
  \node [inner,minimum width=18em] (A1) {Mr. A};
  \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
  \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
  \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
\begin{pgfonlayer}{background}
\node[outer,fit=(A1) (A2) (A3) (text)] (A) {};
\end{pgfonlayer}
\end{tikzpicture}

\end{document} 

结果是一样的。

答案2

尝试:

\documentclass[tikz]{standalone}
\begin{document}
    \begin{tikzpicture}[
inner/.style={draw,
              solid,% <-- added
              fill=blue!5,thick,inner sep=3pt,minimum width=8em},
outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
                        ]
\node[outer] (A) {
    \begin{tikzpicture}[node distance=1cm,outer sep = 0pt]
      \node [inner,minimum width=18em] (A1) {Mr. A};
      \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
      \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
      \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
    \end{tikzpicture}
                };
    \end{tikzpicture}
\end{document} 

在此处输入图片描述

编辑: 直接在节点中嵌套tikzpicture只是偶然起作用。接受的答案中建议了更好的方法。所以这里是它的一个小变化。节点的位置由库的使用决定positioning,外部节点中的文本“Hello Tikz”写为其标签:

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{backgrounds,
                fit,
                positioning}
                
\begin{document}
    \begin{tikzpicture}[
node distance = 2ex and 0em,
inner/.style={draw, thick, fill=blue!5,
              inner sep=3pt,minimum width=8em},
outer/.style={draw=gray, thick, 
              densely dashed, fill=green!5,
              inner xsep=1ex, inner ysep=2ex, yshift=1ex,
              fit=#1}
                        ]
\node (A1)  [inner, minimum width=18em] {Mr. A};
\node (A2)  [inner,above right=of A1.north west] {Mr. A1};
\node (A2)  [inner,above  left=of A1.north east] {Mr. A2};
\scoped[on background layer]
\node [outer=(A1) (A2),
       label={[anchor=north]:Hello Tikz}] {};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容