我正在用 制作文档中的树tikz-qtree
,并且成功制作了树。我还在边缘添加了标签,没有任何问题。但是,标签出现在边缘的顶部,即距离父节点比距离子节点更近。我想将标签放在中间。我该怎么做?
我退房了关于定位标签的这个问题,但我无法从答案中弄清楚他们到底做了什么……
我的代码如下:
\begin{tikzpicture}[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=1cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
\edge node[auto=right] {$[a]$};
[.b
\edge node[auto=right] {$[b]$};
[.1 ]
\edge node[auto=left] {$[b]$};
[.2 ]
]
\edge node[auto=left] {$[a]$};
[.c
\edge node[auto=right] {$[c]$};
[.3 ]
\edge node[auto=left] {$[c]$};
[.4 ]
]
]
\end{tikzpicture}
根据答案中提供的,这里是我使用的实际解决方案(请注意,Christian R. 的解决方案也有效:
\begin{tikzpicture}[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=1cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
\edge node[auto=right,pos=.6] {$[a]$};
[.b
\edge node[auto=right,pos=.8] {$[b]$};
[.1 ]
\edge node[auto=left,pos=.8] {$[b]$};
[.2 ]
]
\edge node[auto=left,pos=.6] {$[a]$};
[.c
\edge node[auto=right,pos=.8] {$[c]$};
[.3 ]
\edge node[auto=left,pos=.8] {$[c]$};
[.4 ]
]
]
\end{tikzpicture}
给出结果:
答案1
您可以使用选项midway
,如下所示:
\edge node[midway,left] {$[b]$};
代码
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[every tree node/.style={draw,circle},
level distance=1.25cm,sibling distance=1cm,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree
[.a
\edge node[auto=right] {$[a]$};
[.b
\edge node[midway,left] {$[b]$};
[.1 ]
\edge node[midway,right] {$[b]$};
[.2 ]
]
\edge node[auto=left] {$[a]$};
[.c
\edge node[midway,left] {$[c]$};
[.3 ]
\edge node[midway,right] {$[c]$};
[.4 ]
]
]
\end{tikzpicture}
\end{document}
结果