Ich möchte einen Baum zeichnen wie den indieser Thread. Der Text in meinen Knoten kann jedoch lang sein und zu Zeilenumbrüchen führen. Dann bringt die anchor=150
Option aus dem Thread die horizontale Ausrichtung der grandchildren
Knoten durcheinander. Wenn ich sie verwende, anchor=west
ist die horizontale Ausrichtung in Ordnung, aber die Knoten verschieben sich nach rechts und überqueren die nächste Spalte.
Was kann ich tun, um das zu vermeiden?
MWE
\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}
Ergebnis
Antwort1
Sie laden die edges
Bibliothek, verwenden sie aber nicht wirklich, was seltsam ist, da genau dieser Stil unterstützt wird. Darüber hinaus entfällt die Notwendigkeit, die Struktur des Baums zu fälschen, sodass Sie die intuitiven Eltern-Kind-Beziehungen in Ihrem Markup beibehalten können.
In diesem Fall folder
ist der Stil die naheliegende Wahl, zumindest wenn man seinen irreführenden Namen ignoriert. Das einzige Problem ist, dass er nicht das Richtige tut, wenn er nur über eine bestimmte Ebene hinaus angewendet wird, und weil er auf clevere Weise versucht, das Richtige zu tun, muss man ihn spät bei der Verarbeitung des Baums überschreiben.
Zum Beispiel,
\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}