![tikz 树叉下边缘标签](https://linux22.com/image/278354/tikz%20%E6%A0%91%E5%8F%89%E4%B8%8B%E8%BE%B9%E7%BC%98%E6%A0%87%E7%AD%BE.png)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm},
edge from parent fork down]
\tikzstyle{every node}=[circle,draw]
\node (Root) [red] {3}
child {
node {1}
child { node {4} edge from parent node[above,draw=none] {help!} }
child { node {1} }
child { node {3} }
}
child {
node {2}
child { node {3} }
child { node {2} }
child { node {5} }
};
\end{tikzpicture}
\end{document}
产量
但我想在下面用红色和蓝色文本标记的位置添加文本标签。
这个帖子某种程度上解决了红色问题,但没有解决蓝色问题。
在分叉之前,让(水平运行的)文本沿着第一条短线运行是最让我头疼的事情。
看起来第一个茎需要更高以便为标签留出空间,尤其是当右侧分支有自己的标签时。
答案1
--
这个例子有助于理解路径运算符(如和-|
)和键之间的关系吗pos
?
对于最后一个例子,你需要我的paths.ortho
为此原因(以及其他原因)而开发的库。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{trees}
\tikzset{level distance=1.5cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm}}
\makeatletter
\def\tikz@ege@from@parent@macro@node#1#2{%
\pgfextra{\def\tikz@@tonodes{#2}}
[style=edge from parent, #1, /utils/exec=\tikz@node@is@a@labeltrue]
\tikz@edge@to@parent@path}
\tikzset{
edge from parent fork down*/.style={
edge from parent macro=\tikz@ege@from@parent@macro@node,
edge from parent path={(\tikzparentnode\tikzparentanchor) |-
([shift=(down:.5\tikzleveldistance)]
\tikzparentnode\tikzparentanchor-|\tikzchildnode\tikzchildanchor)
\tikz@@tonodes -- (\tikzchildnode\tikzchildanchor)}}}
\makeatother
\usetikzlibrary{paths.ortho}
\tikzset{
edge from parent fork down ortho/.style={
edge from parent path={{[hvvh/distance=.5\tikzleveldistance, hvvh/from center]
(\tikzparentnode\tikzparentanchor) |-| (\tikzchildnode\tikzchildanchor)}}}}
\newcommand*\myTree[1]{%
\tikz[#1]\path [every child node/.append style={draw, circle}]
node[every child node] (Root) [red] {3}
child { node {1}
child { node {4} } child { node {1} } child { node {3} }
edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
node[near start] {.25} node[midway] {.5} node[near end] {.75}
node [pos=.875] {.875} node[at end] {1}}
child { node {2}
child { node {3} } child { node {2} } child { node {5} }
edge from parent[font=\tiny] node[at start] {0} node [pos=.125] {.125}
node[near start] {.25} node[midway] {.5} node[near end] {.75}
node [pos=.875] {.875} node[at end] {1}};}
\begin{document}
\myTree{edge from parent fork down}
\myTree{edge from parent fork down*}
\myTree{edge from parent fork down ortho}
\end{document}
输出
答案2
这是另一种方法,其中将内部标签 (A) 分配给 ROOT,即原点,然后使用 shift={(x,y)} 添加标签。这里,第一级距离稍微增加一点,以便为蓝色标签留出空间。最后 3 行告诉如何添加标签,
代码:
\documentclass{article}
\usepackage[papersize={10cm,8cm}]{geometry}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=2cm,
level 1/.style={sibling distance=3.5cm},
level 2/.style={sibling distance=1cm,level distance=1.5cm},
edge from parent fork down]
\tikzstyle{every node}=[circle,draw]
\node (Root) [red] (A) {3}
child {
node {1}
child { node {4} edge from parent node[above,draw=none] {help!} }
child { node{1} }
child { node {3} }
}
child {
node {2}
child { node {3} }
child { node{2} }
child { node {5} }
};
\node [draw=none,blue,shift={(2.1cm,-0.5cm)}] (A) {Horizontal label here};
\node [draw=none,red, shift={(-1cm,-0.8cm)}] (A) {here A};
\node [draw=none,red, shift={( 1cm,-0.8cm)}] (A) {here B};
\end{tikzpicture}
\end{document}