Consider this tree, where the node, C1, is missing. The missing node may occur at any tier of the tree, and nodes may be present at one or more tiers below the missing node.
What are the syntax to draw the edges for B1 -> D1 and B1 -> D2, as shown in red, to align the vertical section of these edges with the vertical section of the edges below node C2? The fork sep
key specifies the l
-distance between the parent anchor and the fork, but I don't know how to use it.
This is the MWE:
\documentclass[12pt,crop=true,border=1cm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow'=0,
draw,
forked edges,
text width=10mm,
minimum height=5mm,
parent anchor=east,
child anchor=west,
text centered,
}
[A,tier=level1
[B1,tier=level2,name=B1
% [C1 would be here, but it is missing
[D1,tier=level4,name=D1
[E1,tier=level5]
[E2,tier=level5]
]
[D2,tier=level4,name=D2
[E3,tier=level5]
[E4,tier=level5]
]
% ]
]
[B2,tier=level2
[C2,tier=level3
[D3,tier=level4
[E5,tier=level5]
[E6,tier=level5]
]
[D4,tier=level4
[E7,tier=level5]
[E8,tier=level5]
]
]
]
]
% Draw the edges with the fork at the desired level.
% The fork sep key specifies the "The l-distance between the parent anchor and the fork."
% See Forest manual version July 14, 2017, p81
\draw[red,thick] (B1.parent anchor) -- +(57pt,0) |- (D1.child anchor);
\draw[red,thick] (B1.parent anchor) -- +(57pt,0) |- (D2.child anchor);
\end{forest}
\end{document}
UPDATE
Testing an expanded MWE with the fork sep
code from @marmot revealed some shortfalls
With two nodes (C1
and C3
) at tier=level3
, and a third node C2
is missing, the edge shape resembles a folder structure rather than being forked.
Removing C2
and D5
results in the tree no longer correctly representing the structure.
While I expect that an automated solution is going to rely on using the fork sep
key, @marmot's solution appears to be close, but perhaps something is still missing.
The solution posted by @Zarko drew edges between grandparent and child nodes using orthogonal coordinates to define the location of the fork. This is a robust and versatile approach. @Zarko, please put back your answer.
UPDATE 2
This may be a robust solution. It uses (a) @marmot's fork sep
code, (b) identifies the missing nodes as coordinates without drawing the node shape i.e. [,draw=none,name=C2
and (c) uses a \draw
command to join node.child anchor
to node.parent anchor
. For example: \draw (C2.child anchor) -- (C2.parent anchor);
\documentclass[12pt,crop=true,border=1cm]{standalone}
\usepackage[edges]{forest}
\forestset{
declare dimen={my fork sep}{0.5em},
my forked edge'/.style={
edge={rotate/.option=!parent.grow},
edge path'={let \noexpand\p1=($(.child anchor)-(!u.parent anchor)$) in
(!u.parent anchor) -- ++(\noexpand\x1-\forestoption{my fork sep},0) |- (.child anchor)},
},
my forked edge/.style={
on invalid={fake}{!parent.parent anchor=children},
child anchor=parent,
my forked edge',
},
my forked edges/.style={for nodewalk={#1}{my forked edge}},
my forked edges/.default=tree,
}
\begin{document}
\begin{forest}
for tree={
grow'=0,
draw,
my forked edges,my fork sep=8pt,
text width=10mm,
minimum height=5mm,
parent anchor=east,
child anchor=west,
text centered,
}
[A,tier=level1
[B1,tier=level2,name=B1
[C1% would be here, but it is missing
[D1,tier=level4,name=D1a
[E1,tier=level5]
[E2,tier=level5]
]
[D2,tier=level4,name=D2a
[E3,tier=level5]
[E4,tier=level5]
]
[D3,tier=level4,name=D3a
[E5,tier=level5]
[E6,tier=level5]
]
]
[,draw=none,name=C2
[D4,tier=level4,name=D1b
[E7,tier=level5]
[E8,tier=level5]
]
[,draw=none,name=D5
[E9,tier=level5]
[E10,tier=level5]
]
]
]
[B2,tier=level2
[C3,tier=level3
[D6,tier=level4
[E11,tier=level5]
[E12,tier=level5]
]
[D7,tier=level4
[E13,tier=level5]
[E14,tier=level5]
]
]
]
]
\draw (C2.child anchor) -- (C2.parent anchor);
\draw (D5.child anchor) -- (D5.parent anchor);
\end{forest}
\end{document}
forked edges
should not be withinfor tree
. Eitherforked edges
orfor tree={forked edge}
.