可能重复:
如何连接 tikz 中树的任意节点
大家好,我是 latex 新手。刚开始使用 tikz 包进行绘图。下面是一段代码,就像有 6 个子节点联网一样。现在,我想连接两个子节点,比如子节点 $v_1$ 和子节点 $v_2$,形成一个从顶点 $v_0$、$v_1$ 到 $v_2$ 的三角形,我希望它的颜色为红色。很抱歉问了这么简单的问题,我只是觉得很难。而且我实际上已经忙得不可开交了,因此任何帮助都将不胜感激。
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzstyle{every node}=[ball color=orange,circle,text=white]
\tikzstyle{edge from parent}=[draw,dashed,thick,red]
\node {$v_0$}
child {node {$v_1$}}
child {node {$v_2$}}
child {node {$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\end{tikzpicture}
\end{center}
如果我有一个子节点和一个子节点,并且我希望连接子节点和子节点的边是不同的颜色,比如说蓝色,那该怎么办呢?我是这样做的,
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzstyle{every node}=[ball color=orange,circle,text=white]
\tikzstyle{edge from parent}=[draw,dashed,thick,red]
\node {$v_0$}
child {node (a) {$v_1$}
child {node (b) {$v_2$}}
child {node (c) {$v_3$}}
child {node (d) {$v_4$}}
}
child {node (e) {$v_7$}}
child {node {$v_8$}}
child {node {$\cdots$}}
child {node {$v_{16}$}}
;
\path[draw=red,thick,dashed]
(a) edge[blue] (b)
(a) edge[blue] (c)
(a) edge[blue] (d)
(a) edge[bend right, blue] (e);
\end{tikzpicture}
\end{center}
我的网络现在有重叠的蓝色。我该如何解决这个问题?谢谢!
答案1
你是指这样的吗?
这是实现它的代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick,red}}
\node {$v_0$}
child {node (v1){$v_1$}}
child {node (v2){$v_2$}}
child {node {$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\path[draw=red,thick,dashed] (v1) edge(v2);
\end{tikzpicture}
\end{center}
\end{document}
首先为要连接的两个节点分配标签,然后通过path
(几乎类似于 Jake 的评论方法)建立连接。
例如,要连接 v1 和 v3,您可以使用bend right
边缘选项。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick,red}}
\node {$v_0$}
child {node (v1){$v_1$}}
child {node (v2){$v_2$}}
child {node (v3){$v_3$}}
child {node {$v_4$}}
child {node {$v_5$}}
child {node {$v_6$}}
;
\path[draw=red,thick,dashed] (v1) edge[bend right](v3);
\end{tikzpicture}
\end{center}
\end{document}
结果:
一个快速的解决方案只是为了修复最后的编辑(对于将来会读到这个问题的人来说)。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
[parent anchor=south,child anchor=north,grow=south]
\tikzset{every node/.style={ball color=orange,circle,text=white}}
\tikzset{edge from parent/.style={draw,dashed,thick}}
% Style to levels: first one red connections, second one blue connections
\tikzset{level 1/.style={red,sibling distance=20mm},level 2/.style={blue,sibling distance=20mm}}
\node {$v_0$}
child {node (a) {$v_1$}
child {node (b) {$v_2$}}
child {node (c) {$v_3$}}
child {node (d) {$v_4$}}
}
child {node (e) {$v_7$}}
child {node {$v_8$}}
child {node {$\cdots$}}
child {node {$v_{16}$}}
;
\end{tikzpicture}
\end{center}
\end{document}
结果: