所以我可以这样做:
也就是说,很容易在连接器旁边添加文本(沿着连接器倾斜或笔直),但我希望连接器的一部分是平的,并且标题文本沿着平坦部分,以便于更好的可读性。
我想也许我可以通过创建一个假节点来实现这一点,该节点只是一行上面有文本的线条。不确定如何实现它,或者是否有更简单的方法。
谢谢你的任何想法?
第一个图的代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{line} = [draw=none]
\tikzset{
grow=right,
sloped,
join=miter,
level 1/.style={sibling distance=5cm,level distance=5.2cm},
level 2/.style={sibling distance=4cm, level distance=6.7cm},
level 3/.style={sibling distance=3cm, level distance=6.7cm},
edge from parent/.style={ultra thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}
\begin{tikzpicture}[]
\small
\node[decision]{}
child{node[line]{}
edge from parent
node[above]{Option 2}
}
child{node[chance]{}
child{node[line]{}}
child{node[line]{}}
edge from parent
node[below]{Option 1}
};
\end{tikzpicture}
\end{document}
答案1
您可以使用forest
包。一个小例子(根据需要调整设置):
\documentclass{article}
\usepackage{forest}
\tikzset{
Above/.style={
midway,
above,
font=\scriptsize,
text width=1.5cm,
align=center,
},
Below/.style={
midway,
below,
font=\scriptsize,
text width=1.5cm,
align=center
}
}
\begin{document}
\begin{forest}
for tree={
grow=east,
draw=cyan,
circle,
line width=0.2pt,
parent anchor=east,
child anchor=west,
edge={draw=cyan},
edge label={\Huge\color{black}},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- ([xshift=-1.6cm].child anchor) --
(.child anchor)\forestoption{edge label};
},
l sep=2cm,
}
[,rectangle, s sep=35pt,
[,edge label={node[Below]{option1}}
[,edge label={node[Below]{a longer text goes here}}
]
[,edge label={node[Above]{text}}
]
]
[,edge label={node[Above]{option2}}
[,edge label={node[Below]{a longer text goes here}}
]
[,edge label={node[Above]{text}}
]
]
]
\end{forest}
\end{document}