我想畫一棵樹,如圖所示這個線程。但是,我的節點中的文字可能很長並導致換行。然後,anchor=150
執行緒中的選項會擾亂grandchildren
節點的水平對齊方式。當我使用anchor=west
水平對齊時,效果很好,但節點向右移動並穿過下一列。
我可以做什麼來避免這種情況?
微量元素
\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.markings}
% \usetikzlibrary{fpu}
\usetikzlibrary{intersections}
\usetikzlibrary{trees}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{shadows}
\useforestlibrary{edges}
\tikzset{
parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
grandchild/.style={fill=white,text width=2.3cm}
}
\begin{document}
\begin{forest}
for tree={%
thick,
drop shadow,
l sep=0.8cm,
s sep=1.0cm,
node options={
draw,
font=\sffamily
},
edge={
semithick,
-Latex
},
where level=0{parent}{},
where level=1{
minimum height=1cm,
child,
parent anchor=south west,
tier=p,
l sep=0.45cm,
for descendants={%
grandchild,
minimum height=0.6cm,
anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
(!to tier=p.parent anchor) |-(.child anchor)\forestoption{edge label};
},
}
}{},
}
[\large Long text with line break%
[\textbf{Test 1} \\ with a lot of subtext%
[Topic%
[Long topic with line break%
[Topic%
]]]%
]
[\textbf{Test 2} \\ with a lot of subtext%
[Topic%
[Long topic with line break%
[Topic%
]]]%
]
[\textbf{Test 3} \\ with a lot of subtext%
[Topic%
[Long topic with line break%
[Topic%
]]]%
]
[\textbf{Test 4} \\ with a lot of subtext%
[Topic%
[Long topic with line break%
[Topic%
]]]%
]
]
\end{forest}
\end{document}
結果
答案1
您正在加載edges
庫,但實際上並沒有使用它,這很奇怪,因為這正是它支援的樣式。此外,它消除了偽造樹結構的需要,允許您在標記中保留直觀的父子關係。
在這種情況下,folder
樣式是顯而易見的選擇,至少忽略其誤導性的名稱。唯一的問題實際上是,當它僅適用於超出一定級別時,它不會做正確的事情,並且因為它試圖以聰明的方式做正確的事情,所以您必須在樹的處理後期覆蓋它。
例如,
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shadows,arrows.meta}
\tikzset{
parent/.style={align=center,text width=4cm,fill=gray!50,rounded corners=2pt},
child/.style={align=center,text width=2.5cm,fill=gray!20,rounded corners=6pt},
grandchild/.style={fill=white,text width=2.3cm}
}
\begin{document}
\begin{forest}
for tree={%
thick,
drop shadow,
node options={
draw,
font=\sffamily
},
edge={
semithick,
-Latex
},
where level=0{
parent,
l sep'=0.8cm,
s sep'=1.0cm,
}{
folder,
grow'=0,
},
where level=1{
minimum height=1cm,
child,
l sep=7.5mm,
for descendants={%
grandchild,
minimum height=0.6cm,
},
for children={
before computing xy={s+=5mm},
}
}{},
}
[\large Long text with line break%
[\textbf{Test 1} \\ with a lot of subtext%
[Topic]
[Long topic with line break]
[Topic]
]
[\textbf{Test 2} \\ with a lot of subtext%
[Topic]
[Long topic with line break]
[Topic]
]
[\textbf{Test 3} \\ with a lot of subtext%
[Topic]
[Long topic with line break]
[Topic]
]
[\textbf{Test 4} \\ with a lot of subtext%
[Topic]
[Long topic with line break]
[Topic]
]
]
\end{forest}
\end{document}