向 tikzpicture 添加嵌套背景

向 tikzpicture 添加嵌套背景

我正在尝试使用 TikZ 重新创建下图。

在此处输入图片描述

这是我所拥有的

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[roundnode/.style={circle, draw=blue!60, fill=blue!5, very thick},squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick}]
\node[squarednode] (theta_I) {$\theta_e^I$};
\node[squarednode] (theta_V) [right=10mm of theta_I] {$\theta_e^V$};
\node[squarednode] (theta_Y) [right=10mm of theta_V] {$\theta_e^Y$};
\node[roundnode] (I_ne) [below=10mm of theta_I] {$I_{ne}$};
\node[roundnode] (V_ne) [below=10mm of theta_V] {$V_{ne}$};
\node[roundnode] (Y_ne) [below=10mm of theta_Y] {$Y_{ne}$};
\node[roundnode] (X_ne) [below=10mm of V_ne] {$X_{ne}$};
\node[squarednode] (theta_X) [below=10mm of X_ne] {$\theta^X$};

\draw[->, ultra thick] (theta_I.south) -- (I_ne.north);
\draw[->, ultra thick] (theta_V.south) -- (V_ne.north);
\draw[->, ultra thick] (theta_Y.south) -- (Y_ne.north);
\draw[->, ultra thick] (I_ne.south) -- (X_ne.north west);
\draw[->, ultra thick] (V_ne.south) -- (X_ne.north);
\draw[->, ultra thick] (Y_ne.south) -- (X_ne.north east);
\draw[->, ultra thick] (Y_ne.west) -- (V_ne.east);
\draw[->, ultra thick] (X_ne.south) -- (theta_X.north);
\end{tikzpicture}

\end{document}

我该如何添加背景?我最好使用阴影背景,例如,外部“E”背景为浅蓝色,内部“N”背景为深蓝色。

答案1

这里是tikzcd和 的forest解决方案,它们共享样式vert downlablab'。两者都使用fit围绕一组节点绘制一个矩形作为submatrix键的一部分。

我已经连接\theta^XX_{ne}但不是任何矩形,因为草图中的箭头没有触及任何矩形的边框。

我已经在中定义了\bgarrowtikzcd我之前的回答

inner这两种解决方案都会创建一个仅供outer节点参考的“假”节点。outer节点创建后(并放置在 上background),inner节点将放置在 之上outer,但仍在 上background

如果你必须使用两个以上的矩形,那么就需要开发一个更简单的解决方案(比如一个矩形列表,伪造的从上到下但从下到上绘制)。

代码

\documentclass[tikz]{standalone}
\usepackage{tikz-cd, forest}
\usetikzlibrary{fit, backgrounds}
\makeatletter
\let\tikzcd@ar@new@orig\tikzcd@ar@new
\def\tikzcd@ar@new@bg[#1]{% https://tex.stackexchange.com/a/664328/16595
  \pgfutil@g@addto@macro\tikzcd@savedpaths{%
    \pgfonlayer{background}\path[/tikz/commutative diagrams/.cd,every arrow,#1]%
    (\tikzcd@ar@start\tikzcd@startanchor)to(\tikzcd@ar@target\tikzcd@endanchor);
    \endpgfonlayer}\let\tikzcd@ar@new\tikzcd@ar@new@orig}
\tikzcdset{every diagram/.append code=%
  \def\bgarrow{\let\tikzcd@ar@new\tikzcd@ar@new@bg\tikzcd@arrow}%
  \let\bgar\bgarrow}
\makeatother

\tikzset{
  vert down/.style={to path={--(\tikztostart|-\tikztotarget.north)\tikztonodes}},
  lab/.style={label={[anchor=south east]south east:$#1$}},
  lab'/.style={label={[anchor=south west,name=placeholder,xshift=-\pgfkeysvalueof{/pgf/inner xsep}]
    south east:\phantom{$#1$}}},
  submatrix/.style args={[#1]#2:#3}{
    fit={#2},name={#3},node contents=,draw,#1}}
\tikzset{
  roundnode/.style={circle, draw=blue!60, fill=blue!5, very thick},
  squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick}}

%% tikzcd
\tikzcdset{
  submatrix/.style args={[#1]#2:#3}{
    to path={node[inner sep=+.85ex,name prefix=\tikzcdmatrixname-,submatrix={[{#1}]{#2}:{#3}}]}},
  rows'/.style 2 args={
    /utils/temp/.style={/tikz/row ##1/.append style={nodes={#2}}},/utils/temp/.list={#1}}}

%% forest
\tikzset{forest edge/.style={style/.expanded=\forestoption{edge}}}

\begin{document}
\begin{tikzcd}[
  column sep=small, rows'={1,4}{squarednode}, rows'={2,3}{roundnode}]
\theta_e^I & \theta_e^V & \theta_e^Y \\
   F_{ne} \drar
 & V_{ne} \dar
 & Y_{ne} \dlar \lar \\
 & X_{ne} \\
 & \theta^X \uar
%
\arrow  [submatrix={[path only,    lab'=E               ](2-1)(2-3)(3-2)          :inner}]
\bgarrow[submatrix={[fill=blue!10, lab=E                ](1-2)(inner)(placeholder):outer}]
\bgarrow[submatrix={[fill=blue!40, lab=N, inner sep=+0pt](inner)                  :inner}]
%
\arrow[from=1-1, to=inner, vert down]
\arrow[from=1-2, to=inner, vert down]
\arrow[from=1-3, to=inner, vert down]
\end{tikzcd}

\begin{forest}
  for tree={grow'=90, math content, edge+=<-},
  where level={0}{squarednode}{}, where level={3}{squarednode}{},
  where level={1}{roundnode}{},   where level={2}{roundnode}{},
[\theta^X
  [X_{ne}, edge+=->
    [F_{ne} [\theta_e^I, no edge]]
    [V_{ne} [\theta_e^V, no edge]]
    [Y_{ne} [\theta_e^Y, no edge]]
  ]
]
%
\begin{pgfonlayer}{background}
    \node[submatrix={[path only, lab'=E]  (!1)(!11)(!12)(!13)                   :inner}];
    \node[submatrix={[fill=blue!10, lab=E](inner)(!111)(!121)(!131)(placeholder):outer}];
    \node[submatrix={[fill=blue!40, lab=N, inner sep=+0pt](inner)               :inner}];
\end{pgfonlayer}
\foreach \thet in {1,2,3}
  \path[forest edge,->] (!1\thet 1) edge[vert down] (inner);
\path[forest edge] (!12) edge (!13);
\end{forest}

\end{document}

输出

在此处输入图片描述

在此处输入图片描述

相关内容