我正在尝试绘制具有重叠节点的层次模型。我可以使用使用 tikz 绘制分层模型(来自统计数据的多级模型)制作分层模型,但我不知道如何绘制重叠节点以及如何向边缘添加权重。我想绘制下图所示的结构
答案1
一个标准的技巧是通过 Ti 添加非树的内容钾Z.这一切的基础是你链接到的 cfr 答案。
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows,fit,backgrounds}
\forestset{
declare toks={level label}{},
declare toks register={level labels},
level labels={},
declare count register={leveller},
leveller'=0,
level split/.style={
temptoksa={#1},
split register={temptoksa}{:}{tempcounta,level label split},
},
level label split/.style={
temptoksb={#1},
temptoksc={},
split register={temptoksb}{,}{temptoksc, level label splitter},
tikz+/.wrap 2 pgfmath args={
\node (label leveller ##1) [anchor=east, align=right, font=\sffamily] at (level ##1.west -| westpoint) {##2};
}{tempcounta}{temptoksc},
before computing xy/.wrap pgfmath arg={
tikz+={
\node [anchor=north east, align=right, font=\sffamily\itshape] at (label leveller ##1.north -| west of westpoint) {Level ##1};
},
}{tempcounta},
},
level label splitter/.style={
temptoksc+={\\#1},
},
label levels/.style={
tikz+={
\coordinate (westpoint) at ([xshift=-15pt]current bounding box.west);
},
before packing={
tikz+={
\coordinate (west of westpoint) at ([xshift=-15pt]current bounding box.west);
},
},
before drawing tree={
tikz+={
\scoped[on background layer]{\node [left color=blue!50!cyan!25!white, right color=blue!50!cyan!25!white, middle color=blue!50!cyan, inner sep=10pt, rounded corners, draw=blue!50!cyan, draw opacity=.5, fill opacity=.15, fit=(current bounding box)] {};}
},
},
delay={
for tree breadth-first={
if level label={}{}{
if={(level())>(leveller)}{
leveller/.option=level,
alias/.wrap pgfmath arg={level ##1}{level()},
if level labels={}{}{
level labels+={;},
},
level labels+/.option=level,
level labels+={:},
}{},
level labels+/.option=level label,
level labels+={,},
},
},
},
before typesetting nodes={
if level labels={}{}{
split register={level labels}{;}{level split},
},
},
}
}
\tikzset{cute fancy rodent/.style={rounded corners,
outer color=blue!20,
inner color=blue!15,
minimum height=1cm,
minimum width=1cm,
draw,
drop shadow}}
\begin{document}
\begin{forest}
for tree={
math content,
},
delay={
where content={}{
if level=0{}{
content=\ldots,
math content,
no edge,
fit=band,
},
}{
cute fancy rodent
},
},
label levels,
[, phantom
[one, plain content, level label=platitudes
[c_1
[t_1,alias=t1a]
[]
[t_n,alias=tna]
]
[]
[c_n
[t_1,alias=t1b]
[]
[t_n,alias=tnb]
]
]
[two, plain content
[c_1
[t_1, level label=latitudes,alias=t1c]
[]
[t_n,alias=tnc]
]
[]
[c_n
[t_1,alias=t1d]
[]
[t_n, level label=longitudes,alias=tnd]
]
]
[]
[n
[c_1, level label=magnitudes
[t_1,alias=t1e]
[]
[t_n,alias=tne]
]
[]
[c_n
[t_1,alias=t1f]
[]
[t_n,alias=tnf]
]
]
]
\foreach \X in {a,...,f}
{\path (t1\X) -- (tn\X) node[midway,below=2.5em,cute fancy rodent]
(tm\X){$m_x$};
\draw (t1\X) -- (tm\X) (tm\X) -- (tn\X);
}
\end{forest}
\end{document}