1

I am trying to recreate the following diagram with TikZ.

enter image description here

Here is what I have

\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}

How do I add the backgrounds to it? Preferably I want a shaded background, say light blue for the outer "E" background and dark blue for the inner "N" background.

nalzok
  • 141
  • 1
    PLease extend your code snippet to complete small document (Minimal Working Example: MWE). Use of forest package and fit TikZ library seems to be promising way to solve your problem. – Zarko Nov 21 '22 at 06:22
  • @Zarko Done. The fit library seems promising, but I'm not sure about forest. This is not exactly a tree if you look closely: there is an additional edge from Y_{ne} to V_{ne}. – nalzok Nov 21 '22 at 06:28
  • Thank you for edit. This can be done by growing tri above, I will show thi (if someone else will do this before, ASAP I will have some spare time ... – Zarko Nov 21 '22 at 06:35
  • Looks more like a \matrix to me than a tree but both are possible. – Qrrbrbirlbel Nov 21 '22 at 09:54

1 Answers1

2

Here is a tikzcd and a forest solution that share the styles vert down, lab and lab'. Both use fit to draw a rectangular around a set of nodes as part of the submatrix key.

I've connected \theta^X to X_{ne} and not any of the rectangle since the arrow in your sketch doesn't touch any rectangle's border.

I have defined the \bgarrow macro for tikzcd in a previous answer of mine.

Both solutions create a “fake” inner node that exists only for the outer node for reference. After the outer node has created (and has been placed on the background) the inner node will be placed on top of the outer one but still on the background.

If you have to use this with more than two rectangulars, an easier solution will need to be developed (say a list of rectangles that will be faked from top to bottom but drawn from bottom to top).

Code

\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-3)(3-2) :inner}] \bgarrow[submatrix={fill=blue!10, lab=E (inner)(placeholder):outer}] \bgarrow[submatrix={fill=blue!40, lab=N, inner sep=+0pt :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(!111)(!121)(!131)(placeholder):outer}]; \node[submatrix={fill=blue!40, lab=N, inner sep=+0pt :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}

Output

enter image description here

enter image description here

Qrrbrbirlbel
  • 119,821