
Quiero dibujar un árbol como el que se muestra eneste hilo. Sin embargo, el texto de mis nodos puede ser largo y provocar saltos de línea. Luego, la anchor=150
opción del hilo estropea la alineación horizontal de los grandchildren
nodos. Cuando uso anchor=west
la alineación horizontal está bien, pero los nodos se desplazaron hacia la derecha y cruzan la siguiente columna.
¿Qué puedo hacer para evitar eso?
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}
Resultado
Respuesta1
Estás cargando la edges
biblioteca pero en realidad no la estás usando, lo cual es extraño ya que este es precisamente el tipo de estilo que admite. Además, elimina la necesidad de falsificar la estructura del árbol, lo que le permite mantener las relaciones intuitivas entre padres e hijos en su marcado.
En este caso, el folder
estilo es la elección obvia, al menos ignorando su nombre engañoso. En realidad, el único problema es que no hace lo correcto cuando se aplica sólo más allá de un cierto nivel y, debido a que intenta hacer lo correcto de una manera inteligente, hay que anularlo en una etapa avanzada del procesamiento del árbol.
Por ejemplo,
\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}