# How to draw a tree-like diagram with partially shaded nodes? [closed]

I would like to draw this figure but I don't know how I can do this. Can someone help me and give me advice? I appreciate any help!

Here are at least the input symbols:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
Layer 3: Disjoint base lists $\mathcal{B}_{i,1}$ and $\mathcal{B}_{i,2}$ for $i=1,2,3,4$, $p_3=\frac{p_2}{2}$.
Layer 2: $\mathcal{L}_1^{(2)}$, $\mathcal{L}_2^{(2)}$, $\mathcal{L}_3^{(2)}$, $\mathcal{L}_4^{(2)}$, $r_2$, $p_2=\frac{p_1}{2} + \Delta_2$.
Layer 1: $\mathcal{L}_1^{(1)}$, $\mathcal{L}_2^{(1)}$, $r_1$, $p_1=\frac{p}{2}+\Delta_1$.
Layer 0: $\mathcal{L}$, $\ell$, $p$.
Strange symbol: $\bowtie$
\end{document} • Have you used TikZ before? Jan 10, 2018 at 13:35
• When you post a question, you are expected to provide a "Minimal Working Example" (MWE) that starts with \documentclass, includes all relevant \usepackage commands, ends with \end{document} and compiles without errors, even if it does not produce your desired output. Questions of the form "Please do this for me" that show no attempt by the poster to at least partially solve the problem are looked down upon by the community. Please edit your post, adding your best attempt. Jan 10, 2018 at 14:03
• I would also edit the title to indicate specifically what might make this question useful for other users. For example, "How can I make a tree-like diagram with partially shaded nodes?" or "How can I add text to both sides of every row of a tree diagram?" Jan 10, 2018 at 14:09
• Matrix doesn't quite work due to the odd spacing. I would start with the line of boxes in the second row and work my way out. Jan 10, 2018 at 16:35
• @Sandy G You are completely right that I should have provided a MWE but unfortunately I really didn't have any idea how I could even start. I will try to edit my post later though. Jan 10, 2018 at 17:28

This is close reproduction of your table. \documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
% for horizontal splits, drawing dashed splitting lines is a mess
% https://tex.stackexchange.com/a/34353/121799
\tikzset{pfft/.style={inner sep=1pt,rectangle split, rectangle split parts=2, draw, rectangle split
horizontal,rectangle split draw splits = false,
minimum height=1.2cm,
rectangle split part fill={white, gray!20!white},
%  append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
%       \draw[dashed] ($(\tikzlastnode.north west)!0.70!(\tikzlastnode.north east)$)
%     -- ($(\tikzlastnode.south west)!0.7!(\tikzlastnode.south east)$);}
}
}
\newcommand{\DrawVerticalDashedLine}[0.7]{%
\draw[dashed] ($(#2.north west)!#1!(#2.north east)$) -- ($(#2.south west)!#1!(#2.south east)$);}

\begin{document}
\begin{tabular}{p{3cm}cp{3cm}}
& & weight\\
Layer 3 &
\begin{tikzpicture}[remember picture,baseline=(layer3.base)]
\node[draw](layer3){Disjoint base list $\mathcal{B}_{i,1}$ and $\mathcal{B}_{i,2}$
for $i=1,\dots 4$};
\end{tikzpicture}
& $p_3=\frac{p_2}{2}$\\[1cm]
Layer 2 &
\begin{tikzpicture}[remember picture,baseline=(layer2a.base)]
\node[pfft](layer2a){~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2a}
\node[pfft,right=0.4cm of layer2a](layer2b){~$\mathcal{L}_2^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2b}
\node[pfft,right=0.4cm of layer2b](layer2c){~$\mathcal{L}_3^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2c}
\node[pfft,right=0.4cm of layer2c](layer2d){~$\mathcal{L}_4^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2d}
\end{tikzpicture}
& $p_2=\frac{p_1}{2}+\Delta_2$\\[1cm]
Layer 1 &
\begin{tikzpicture}[remember picture,baseline=(layer1a.base)]
\node[pfft](layer1a){~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer1a}
\node[pfft,right=2cm of layer1a](layer1b){~$\mathcal{L}_2^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer1b}
\end{tikzpicture}
& $p_1=\frac{p}{2}+\Delta$\\[1cm]
Layer 0 &
\begin{tikzpicture}[remember picture,baseline=(layer0a.base)]
\node[pfft](layer0a){~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer0a}
\end{tikzpicture}
& $p$
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
\draw[dashed] ($(layer3.south west)!0.1!(layer3.south east)$) -- (layer2a.north) --  ($(layer3.south west)!0.25!(layer3.south east)$);
\draw[dashed] ($(layer3.south west)!0.9!(layer3.south east)$) -- (layer2d.north) --  ($(layer3.south west)!0.75!(layer3.south east)$);
\node[below=0.3cm of layer3]{$\scriptscriptstyle \bullet~\bullet~\bullet$};
\node[above=0.2cm of layer1a]{$\bowtie$};
\node[above=0.2cm of layer1b]{$\bowtie$};
\draw (layer2a.south) -- (layer1a.north) -- (layer2b.south);
\draw (layer2c.south) -- (layer1b.north) -- (layer2d.south);
\node[above=0.2cm of layer0a]{$\bowtie$};
\draw (layer1a.south) -- (layer0a.north) -- (layer1b.south);
\end{tikzpicture}
\end{document}


I ended up drawing the dashed lines separating the white and gray parts of the boxes by hand. According to this post, this seems to be the only option. I also failed to add these lines as an automatic postaction. This might have to do that the rectangle split produces two nodes, such that \lastikznode gets confused. If anyone knows how to fix this, I'd be really eager to learn that.

EDIT: This does not give an error and the lowest box has the same dimensions as the ones above. (But I'd recommend to try to learn these tricks yourself, you'll see that this is much more fun. ;-)

\documentclass{article}
\usepackage{amssymb}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
% for horizontal splits, drawing dashed splitting lines is a mess
% https://tex.stackexchange.com/a/34353/121799
\tikzset{pfft/.style={inner sep=1pt,rectangle split, rectangle split parts=2, draw, rectangle split
horizontal,rectangle split draw splits = false,
minimum height=1.2cm,
rectangle split part fill={white, gray!20!white},
%  append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
%       \draw[dashed] ($(\tikzlastnode.north west)!0.70!(\tikzlastnode.north east)$)
%     -- ($(\tikzlastnode.south west)!0.7!(\tikzlastnode.south east)$);}
}
}
\newcommand{\DrawVerticalDashedLine}[0.7]{%
\draw[dashed] ($(#2.north west)!#1!(#2.north east)$) -- ($(#2.south west)!#1!(#2.south east)$);}

\begin{document}
\begin{tabular}{p{3cm}cp{3cm}}
& & weight\\
Layer 3 &
\begin{tikzpicture}[remember picture,baseline=(layer3.base)]
\node[draw](layer3){Disjoint base list $\mathcal{B}_{i,1}$ and $\mathcal{B}_{i,2}$
for $i=1,\dots 4$};
\end{tikzpicture}
& $p_3=\frac{p_2}{2}$\\[1cm]
Layer 2 &
\begin{tikzpicture}[remember picture,baseline=(layer2a.base)]
\node[pfft](layer2a){~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2a}
\node[pfft,right=0.4cm of layer2a](layer2b){~$\mathcal{L}_2^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2b}
\node[pfft,right=0.4cm of layer2b](layer2c){~$\mathcal{L}_3^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2c}
\node[pfft,right=0.4cm of layer2c](layer2d){~$\mathcal{L}_4^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer2d}
\end{tikzpicture}
& $p_2=\frac{p_1}{2}+\Delta_2$\\[1cm]
Layer 1 &
\begin{tikzpicture}[remember picture,baseline=(layer1a.base)]
\node[pfft](layer1a){~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer1a}
\node[pfft,right=2cm of layer1a](layer1b){~$\mathcal{L}_2^{(2)}$~ \nodepart{two} ${r_2}$};
\DrawVerticalDashedLine{layer1b}
\end{tikzpicture}
& $p_1=\frac{p}{2}+\Delta$\\[1cm]
Layer 0 &
\begin{tikzpicture}[remember picture,baseline=(layer0a.base)]
\node[pfft](layer0a){~\makebox[\widthof{$\mathcal{L}_2^{(2)}$}][c]{$\mathcal{L}$}~ \nodepart{two}
\makebox[\widthof{${r_2}$}][c]{${\ell}$}};
\DrawVerticalDashedLine{layer0a}
\end{tikzpicture}
& $p$
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
\draw[dashed] ($(layer3.south west)!0.1!(layer3.south east)$) -- (layer2a.north) --  ($(layer3.south west)!0.25!(layer3.south east)$);
\draw[dashed] ($(layer3.south west)!0.9!(layer3.south east)$) -- (layer2d.north) --  ($(layer3.south west)!0.75!(layer3.south east)$);
\node[below=0.3cm of layer3]{$\scriptscriptstyle \bullet~\bullet~\bullet$};
\node[above=0.2cm of layer1a]{$\bowtie$};
\node[above=0.2cm of layer1b]{$\bowtie$};
\draw (layer2a.south) -- (layer1a.north) -- (layer2b.south);
\draw (layer2c.south) -- (layer1b.north) -- (layer2d.south);
\node[above=0.2cm of layer0a]{$\bowtie$};
\draw (layer1a.south) -- (layer0a.north) -- (layer1b.south);
\end{tikzpicture}
\end{document} • When I change in Layer 0 "{~$\mathcal{L}_1^{(2)}$~ \nodepart{two} ${r_2}$}" to "{~$\mathcal{L}$~ \nodepart{two} ${\ell}$}" then the node at the very bottom becomes very small. Is it possible to let the nodes all have the same width? I tried to fix this by adding minimum width=... but unfortunately this didn't work. Jan 10, 2018 at 22:02
• Hmmh, I don't understand what you mean by does not work, but another ad hoc fix would be to change the node to {~\makebox[l]{$\mathcal{L}$}$\phantom{\mathcal{L}_1^{(2)}}$~ \nodepart{two} \makebox[l]{${\ell}$}$\phantom{r_2}$}.
– user121799
Jan 10, 2018 at 22:09
• Did you try the above code with the phantoms?
– user121799
Jan 10, 2018 at 22:16
• I added a version that does not produce an error (but the changes suggested above also worked fine, what I added just looks a bit nicer, I think.)
– user121799
Jan 10, 2018 at 22:32