我有点思维混乱,希望有人能帮我。我正在尝试绘制以下树,但遇到了两个问题:
- 以最左侧“doc”为根的子树应以节点 &o2 为根。不知道为什么会这样。
- 边缘应有方向。
这是我的 MWE:
\documentclass[margin=1mm,xcolor=pdftex,dvipsnames]{standalone}
\usepackage{tikz}
\tikzset{every picture/.style={/utils/exec={\sffamily}},>=latex,align=center,font={\fontsize{10pt}{12}\selectfont}}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=2cm},
tree node/.style={circle,draw,inner sep=1pt,minimum width=0.5cm},
every child node/.style={tree node}]
\node[circle,draw] {\&o1}
child {node {\&o2} edge from parent node[midway] {doc}
child {node {\&o3} edge from parent node[midway] {authors}}
child {node {\&o6} edge from parent node[midway] {title}}
}
child {node {\&o22} edge from parent node[midway] {doc}}
child {node {\&o34} edge from parent node[midway] {doc}}
;
\end{tikzpicture}
\end{document}
非常感谢您的帮助。
答案1
你把节点设为文档节点的子节点,这就是你得到的。如果你将边移到两个子节点之后,你会得到
\documentclass[margin=1mm,xcolor=pdftex,dvipsnames]{standalone}
\usepackage{tikz}
\tikzset{every picture/.style={/utils/exec={\sffamily}},>=latex,align=center,font={\fontsize{10pt}{12}\selectfont}}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=2cm},
tree node/.style={circle,draw,inner sep=1pt,minimum width=0.5cm},
every child node/.style={tree node},
every path/.append style={-latex}]
\node[circle,draw] {\&o1}
child {node {\&o2}
child {node {\&o3} edge from parent node[midway,above,sloped] {authors}}
child {node {\&o6} edge from parent node[midway,above,sloped] {title}}
edge from parent node[midway,above,sloped] {doc} }
child {node {\&o22} edge from parent node[midway,above,sloped] {doc}}
child {node {\&o34} edge from parent node[midway,above,sloped] {doc}}
;
\end{tikzpicture}
\end{document}
不过,我建议使用forest
,这可能更直观。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{forest}
\begin{document}
\forestset{el/.style={edge label={node[above,pos=0.5,sloped,font=\sffamily]{#1}}}}
\begin{forest}
for tree={circle,draw,l+=2em,s sep=5em,font=\sffamily,edge=-latex}
[\&o1
[\&o2,el=doc
[\&o3,el=authors]
[\&o6,el=title]
]
[\&o22,el=doc]
[\&o34,el=doc]
]
\end{forest}
\end{document}