此图重复了标签,并且边缘既不是垂直的也不是水平的。我希望垂直边缘离开 A,标记为“x”,然后分成两个左右水平分支,这些分支将向节点 B 和 C 旋转 90 度。
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary {graphs, graphdrawing, quotes}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
\graph [
layered layout,
nodes = {draw, fill = white},
edge quotes = {fill = white},
level distance = 3 cm,
sibling sep = 4 cm
]{
A -> ["x", out = -90, in = 90] {
B,
C
}
};
\end{tikzpicture}
\end{document}
答案1
除非你坚持使用\graph
根据你的用例使用,否则你可以这样做:
\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary {positioning, calc}
\begin{document}
\begin{tikzpicture}[node distance = 2.5cm,>=stealth]
\node[rectangle,inner sep=2pt,draw,align=center] (A) {A};
\node[rectangle,inner sep=2pt,draw,align=center,below left = of A] (B) {B};
\node[rectangle,inner sep=2pt,draw,align=center,below right = of A] (C) {C};
\coordinate (D) at ($(A)!.7!(A |- B)$);
\draw[thick,->] (A) -- (D) node[anchor=west,midway] {x} -| (B);
\draw[thick,->] (D) -| (C);
\end{tikzpicture}
\end{document}
编辑:或者稍微简单一点的方法,例如:
\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary {positioning, calc}
\begin{document}
\begin{tikzpicture}[node distance = 2.5cm,>=stealth]
\node[rectangle,inner sep=2pt,draw,align=center] (A) {A};
\node[rectangle,inner sep=2pt,draw,align=center,below left = of A] (B) {B};
\node[rectangle,inner sep=2pt,draw,align=center,below right = of A] (C) {C};
\draw[thick,->] (A) -- ($(B)!.5!(C)$) edge (C) node[anchor=west,midway] {x} -- (B) ;
\end{tikzpicture}
\end{document}
编辑2:所以,如果您想使用\graph
,您可以使用这个,尽管标签有点尴尬:
\documentclass[tikz]{standalone}
\usetikzlibrary {graphs, graphdrawing, quotes}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}[>=stealth,thick,
connect/.style={to path={|- (\tikztotarget)}},
connectwlabel/.style={to path={|- node[pos=0.25,anchor=west]{x} (\tikztotarget)}},
]
\graph [
layered layout,
nodes = {draw, fill = white, minimum width=.5cm, minimum height=.5cm, thick},
edge quotes = {fill = white},
level distance = 3cm,
sibling sep = 4cm,
]{
A ->[connectwlabel] B;
A ->[connect] C;
};
\end{tikzpicture}
\end{document}
编辑3:事实证明,您实际上可以结合这些方法并执行以下操作:
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing, quotes,calc}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}[>=stealth,thick,
connect/.style={to path={(\tikztostart) -- ($(\tikztostart)!.7!(\tikztostart |- \tikztotarget)$) -| (\tikztotarget)}},
connectwlabel/.style={to path={(\tikztostart) -- node[pos=0.5,anchor=west]{x} ($(\tikztostart)!.7!(\tikztostart |- \tikztotarget)$) -| (\tikztotarget)}},
]
\graph [
layered layout,
nodes = {draw, fill = white, minimum width=.5cm, minimum height=.5cm, thick},
edge quotes = {fill = white},
level distance = 2.5cm,
sibling sep = 2cm,
]{
A ->[connectwlabel] B;
A ->[connect] C;
};
\end{tikzpicture}
\end{document}
其 (对 和 进行了一些调整level distance
)sibling sep
如下所示: