![森林树木汇聚](https://linux22.com/image/360662/%E6%A3%AE%E6%9E%97%E6%A0%91%E6%9C%A8%E6%B1%87%E8%81%9A.png)
我正在尝试制作一个公司结构树,从某个所有者开始,到单个公司结束。这意味着所有线路都从单个节点开始,并且都以单个节点结束。
这个帖子帮助我找到了我现在的位置:LaTeX:如何制作一棵末端封闭的水平树(森林),但我最终得到了通过节点的线。由于我将重复执行此操作(许多不同的公司结构),因此我正在寻找结构解决方案。
\documentclass{standalone}
\usepackage{forest}
\forestset{
default preamble={
for tree={rectangle, draw, text width = 2cm, grow=0 }
}
}
\begin{document}
\begin{forest}
[Company 4, name =E0
[Company 3
[Company 2, name = E1]
]
[Company 5, name = E2]
[Company 8
[Company 7, name=E3]
[Company 10, name = E4]
]
]
\node[draw] (EndNode) at ($(E3.east) + (2,0)$){Company 1};
\foreach \num in {0,1,2,3, 4}{
\draw (E\num.east) -- (EndNode.west);
}
\end{forest}
\end{document}
另外一个小问题:如何使这些框内的文本居中?
答案1
这是一个相当自动化的解决方案。它改变了边缘的样式,但我还是更喜欢方形边缘。它只经过了最低限度的测试,所以如果它不能正常工作,请告诉我。(我肯定忘记了一些东西。)
棘手的部分是将根节点连接到附加终点。这需要一点技巧。
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\tikzset{%
company/.style={draw, text width = 2cm},
}
\forestset{%
declare keylist register={terminus},
terminus=,
terminal/.style={
tikz+={
\node (terminal) [company, anchor=west] at ([xshift=2*\foresteoption{l sep}]current bounding box.east) {#1};
},
before drawing tree={
split register={terminus}{,}{terminal connection},
},
},
terminal connection/.style={
tikz+={
\draw (terminal.west) -- ++(-\foresteoption{fork sep},0pt) |- (#1.parent anchor) ;
},
},
default preamble={
forked edges,
for tree={
company,
grow=0,
fit=band,
parent anchor=children,
child anchor=parent,
},
delay={
if={>On>{!r.n children}{1}}{
for n/.process={OSw+P?_lw2+n{n children}{isodd(#1)}{1}{0}{(#1+#2)/2}{insert after={[,coordinate, calign with current edge]}}},
}{
append={[, coordinate, calign with current]}
},
},
before typesetting nodes={
where n children=0{
terminus/.option=name,
}{},
},
},
}
\begin{document}
\begin{forest}
terminal=Company 1,
[Company 4
[Company 3
[Company 2]
]
[Company 5]
[Company 8
[Company 7]
[Company 10]
]
]
\end{forest}
\end{document}
分离 Ti 的好处钾Forest 配置中的 Z 设置是您可以轻松地将相同的样式应用于树中和树外部的节点,而无需为键指定特殊路径。
例如,要根据需要将文本置于节点的中心,请将company
样式定义更改为
\tikzset{%
company/.style={draw, text width = 2cm, text centered,
}
或者,如果想要更酷炫的东西,可以尝试
\usetikzlibrary{shadows.blur}
\tikzset{%
company/.style={draw, text width = 2cm, text centered, rounded corners, font=\sffamily, top color=gray!5, bottom color=gray!15, blur shadow},
}
答案2
你喜欢么?姆韦:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{forest}
\begin{document}
\forestset{
default preamble={
for tree={rectangle, draw, text width=22mm, inner sep=2mm,
text centered,
grow = 0,
s sep = 11mm,
l sep = 7mm }
}
}
\begin{forest}
[Company 4, name =E0
[Company 3
[Company 2, name = E1]
]
[Company 5, name = E2]
[Company 8
[Company 7, name=E3]
[Company 10, name = E4]
]
]
\node[draw, right=of E3.east |- E2] (EndNode) {Company 1};
\foreach \num in {1,2,3, 4}{
\draw (E\num.east) -- (EndNode.west);
}
\draw (E0) edge [bend angle=20,bend right] (EndNode.west);
\end{forest}
\end{document}