以下是代码:
\begin{figure}[H]\centering
\begin{tikzpicture}
\node (tree)
{
\begin{forest}
for tree = {circle, draw, minimum width = 2.25em}
[\textcolor{red}{$S$}
[$A$, edge label = {node [midway, above left, font = \scriptsize] {\textcolor{red}{$1$}}}
[$B$, edge label = {node [midway, above left, font = \scriptsize] {$4$}}
[$D_1$, edge label = {node [midway, left, font = \scriptsize] {$7$}}
[$G_1$, edge label = {node [midway, left, font = \scriptsize] {$10$}}]
]
]
[$C$, edge label = {node [midway, above right, font = \scriptsize] {$2$}}
[$D_2$, edge label = {node [midway, above left, font = \scriptsize] {$3$}}
[$G_2$, edge label = {node [midway, left, font = \scriptsize] {$6$}}]
]
[$G_3$, edge label = {node [midway, above right, font = \scriptsize] {$4$}}]
]
]
[$G_4$, edge label = {node [midway, above right, font = \scriptsize] {$12$}}]
]
\end{forest}
};
\end{tikzpicture}
输出如下:
以下是与此相关的几个问题:
我想将节点(圆圈)的边界涂成红色,并将一些边缘涂成红色。有办法吗?
对于边缘标签,对于水平和垂直位置,如
node[midway, right]
或中node[midway, above]
,这分别适用于垂直和水平边缘,但对于倾斜的边缘,因为它们可以承担任何角度,我想标记边缘,使它们成为与水平和垂直标签的距离相同, 但在垂直方向到倾斜的边缘。如何对其进行编码?如何让边长更长?对于所有边?对于某些边?
任何帮助都将不胜感激。提前谢谢您!
答案1
好吧,这是一片森林,有红色圆圈节点(draw=red
)、一个更大的l sep
(l sep+=2em
)和圆形边缘标签,以确保通用距离。我还将一条边涂成蓝色。您可以添加s sep+=1em
等等。
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\tikzset{eln/.style={midway, font = \scriptsize,circle,inner sep=2pt}}
\begin{forest}
for tree = {circle,
draw=red, %<-added =red
minimum width = 2.25em,
l sep+=2em
}
[\textcolor{red}{$S$}
[$A$, edge label = {node [above left,eln] {\textcolor{red}{$1$}}}
[$B$, edge label = {node [above left,eln] {$4$}}
[$D_1$, edge label = {node [left,eln] {$7$}}
[$G_1$, edge label = {node [left,eln] {$10$}}]
]
]
[$C$, edge label = {node [above right,eln] {$2$}},edge=blue%<-blue edge
[$D_2$, edge label = {node [above left,eln] {$3$}}
[$G_2$, edge label = {node [left,eln] {$6$}}]
]
[$G_3$, edge label = {node [above right,eln] {$4$}}]
]
]
[$G_4$, edge label = {node [above right,eln] {$12$}}]
]
\end{forest}
\end{document}
或者只为特定节点着色。
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\tikzset{eln/.style={midway, font = \scriptsize,circle,inner sep=2pt}}
\begin{forest}
for tree = {circle,
draw,%
minimum width = 2.25em,
l sep+=2em
}
[\textcolor{red}{$S$}
[$A$, edge label = {node [above left,eln] {\textcolor{red}{$1$}}}
[$B$, edge label = {node [above left,eln] {$4$}}
[$D_1$, edge label = {node [left,eln] {$7$}}
[$G_1$, edge label = {node [left,eln] {$10$}}]
]
]
[$C$,draw=red, edge label = {node [above right,eln] {$2$}},edge=blue%<-blue edge
[$D_2$, edge label = {node [above left,eln] {$3$}}
[$G_2$, edge label = {node [left,eln] {$6$}}]
]
[$G_3$, edge label = {node [above right,eln] {$4$}}]
]
]
[$G_4$, edge label = {node [above right,eln] {$12$}}]
]
\end{forest}
\end{document}