\documentclass{article}
\usepackage{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}
\usetikzlibrary{arrows.meta}
\usepackage{mdframed}
\usepackage{boxedminipage}
\usepackage{forest}
\begin{document}
\begin{forest}
% Styling
for tree={
align=center,
parent anchor=south,
child anchor=north,
edge={thick, -{Stealth[]}},
l sep+=10pt,
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{
inner xsep=-15pt,
tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
[a
[b]
[c] ]
[b
[d]
[e] ]
]
\end{forest}
\end{document}
我怎样才能按照下图所示的方式连接所有分支?
答案1
更新 2:具有 nodewalk 规范的节点之间的箭头
所有附加箭头(不是森林原生建造的)连接两个节点的节点行走语法。这不命名节点与以前一样(真正的风格练习)为了让这些箭头更直观,我给它们上了颜色红色的。
我引用手册:
节点行走是一种表达节点关系的简洁方式。它只是一串步骤,用单个字符表示,其中: 你代表父节点(向上);页对于前一个兄弟姐妹;n对于下一个兄弟姐妹;s为了这兄弟(仅在二叉树中有用);1,2,。 。 。9表示第一个、第二个……第九个孩子;l 表示最后一个孩子,等等。有关完整规范,请参阅第节3.8.7。
\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}
\begin{document}
\begin{forest}
% Styling
for tree={
align=center,
parent anchor=south,
child anchor=north,
edge={thick, -{Stealth[]}},
l sep+=10pt,
edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
[a
[b]
[c,s sep=10,
[f,s sep=30,
[,phantom]
[z,edge={white}]
{\draw[thick,red,-{Stealth[]}] (!rllN.south east) -- +(0,-10pt) -| (!c); %<-- arrow from g to z
\draw[thick,red] (!uss.south west) -- +(0,-10pt) -| (!c) ;%<-- arrow from f to z
}
]{\draw[thick,red,shorten >=3pt] (!us.south) -- +(0,-10pt) -| (!c) ;}%<-- arrow from b to f
[,phantom]
]
]
[b
[d]
[e,s sep=9
[g]{\draw[thick,red,shorten >=3pt] (!us.south) -- +(0,-10pt) -| (!c) ;}%<-- arrow from d to g
[,phantom]
]
]
]
\end{forest}
\end{document}
回应更新
为了实现这一结果,我做了很多修改,所以我不确定我的代码是否优雅。如果有森林专家过来,我会听听可以做哪些改进。
\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}
\begin{document}
\begin{forest}
% Styling
for tree={
align=center,
parent anchor=south,
child anchor=north,
edge={thick, -{Stealth[]}},
l sep+=10pt,
edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
[a
[b,name=b]
[c,s sep=10,
[f,name=f,s sep=30,
[,phantom]
[z,name=z,edge={white}]
]
[,phantom]
]
]
[b
[d,name=d]
[e,s sep=9
[g,name=g]
[,phantom]
]
]
]
\draw[thick,shorten >=2pt] (b.south) -- +(0,-10pt) -| (f.north);
\draw[thick,shorten >=2pt] (d.south) -- +(0,-10pt) -| (g.north);
\draw[thick,shorten >=2pt] (g.south east) -- +(0,-10pt) -| (z.north);
\draw[thick,-{Stealth[]}] (f.south west) -- +(0,-10pt) -| (z.north);
\end{forest}
\end{document}
旧答案
可能有更优雅的解决方案,但这是我的建议。
\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\useforestlibrary{linguistics}
%\usepackage{fullpage}
%\usepackage[utf8]{inputenc}
%\usepackage{tikz}
%\usetikzlibrary{arrows,shapes,positioning,shadows,trees}
%\usepackage{mdframed}
%\usepackage{boxedminipage}
\begin{document}
\begin{forest}
% Styling
for tree={
align=center,
parent anchor=south,
child anchor=north,
edge={thick, -{Stealth[]}},
l sep+=10pt,
edge path={\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-10pt) -| (.child anchor)\forestoption{edge label};
},
if level=0{inner xsep=-15pt,tikz={\draw [thick] (.south east) -- (.south west);}
}{}
}
%
[Alphabet
[a
[b,name=b]
[c,name=c
[,phantom]
[z,name=z]
]
]
[b
[d,name=d]
[e,name=e]
]
]
\draw[thick,shorten >=2pt] (b.south) -- +(0,-10pt) -| (z.north);
\draw[thick,shorten >=2pt] (d.south) -- +(0,-10pt) -| (z.north);
\draw[thick,shorten >=2pt] (e.south) -- +(0,-10pt) -| (z.north);
\end{forest}
\end{document}