使用“绘制边缘”命令在 Tikz 图上显示权重

使用“绘制边缘”命令在 Tikz 图上显示权重

我想显示一些数字作为 Tikz 图边缘的权重。没有权重的图如下:

\begin{figure}[!ht]
\begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick]
    \tikzstyle{node_style} = [circle,draw=blue,fill=blue!20!,font=\sffamily\Large\bfseries]
    \tikzstyle{edge_style} = [draw=black, line width=2, ultra thick]
    \node[node_style] (v1) at (-2,2) {2};
    \node[node_style] (v2) at (2,2) {3};
    \node[node_style] (v3) at (4,0) {6};
    \node[node_style] (v4) at (2,-2) {4};
    \node[node_style] (v5) at (-2,-2) {5};
    \node[node_style] (v6) at (-4,0) {1};
    \draw[edge_style]  (v1) edge (v2);
    \draw[edge_style]  (v2) edge (v3);
    \draw[edge_style]  (v3) edge (v4);
    \draw[edge_style]  (v4) edge (v5);
    \draw[edge_style]  (v5) edge (v6);
    \draw[edge_style]  (v6) edge (v1);
    \draw[edge_style]  (v5) edge (v1);
    \draw[edge_style]  (v5) edge (v2);
    \draw[edge_style]  (v4) edge (v2);
    \end{tikzpicture}
\end{figure}

我曾尝试在网上寻找一些东西,但没有找到任何可以使用\draw边缘命令显示权重的东西,这是我在图表中使用的命令。

您能建议我如何解决我的问题吗?

答案1

要在边旁边添加标签,请使用语法edge node {some text}。例如,请参阅 TikZ 手册中的第二个教程,第 3.11 节在行旁添加文本

如果您想将节点放置在线的另一侧,请使用edge node[swap] {some text}

还请注意,这\tikzstyle被视为已弃用,您应该使用\tikzset或将样式添加到tikzpicture选项中,如下所示。请参阅应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?

下面是创建同一张图表的三种不同方法,其中一种是 Alain 在评论中建议的。

在此处输入图片描述

\documentclass[tikz]{standalone}
\begin{document}

% first method
\begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick,
   node_style/.style={circle,draw=blue,fill=blue!20!,font=\sffamily\Large\bfseries},
   edge_style/.style={draw=black, ultra thick}]

    \node[node_style] (v1) at (-2,2) {2};
    \node[node_style] (v2) at (2,2) {3};
    \node[node_style] (v3) at (4,0) {6};
    \node[node_style] (v4) at (2,-2) {4};
    \node[node_style] (v5) at (-2,-2) {5};
    \node[node_style] (v6) at (-4,0) {1};
    \draw[edge_style]  (v1) edge node{1} (v2);
    \draw[edge_style]  (v2) edge node{2} (v3);
    \draw[edge_style]  (v3) edge node{3} (v4);
    \draw[edge_style]  (v4) edge node{4} (v5);
    \draw[edge_style]  (v5) edge node{5} (v6);
    \draw[edge_style]  (v6) edge node{6} (v1);
    \draw[edge_style]  (v5) edge node{7} (v1);
    \draw[edge_style]  (v5) edge node{8} (v2);
    \draw[edge_style]  (v4) edge node{9} (v2);
    \end{tikzpicture}

% Second method
\begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick]
   \begin{scope}[every node/.style={circle,draw=blue,fill=blue!20!,font=\sffamily\Large\bfseries}]
    \node (v1) at (-2,2) {2};
    \node (v2) at (2,2) {3};
    \node (v3) at (4,0) {6};
    \node (v4) at (2,-2) {4};
    \node (v5) at (-2,-2) {5};
    \node (v6) at (-4,0) {1};
   \end{scope}
   \begin{scope}[every edge/.style={draw=black,ultra thick}]
    \draw  (v1) edge node{1} (v2);
    \draw  (v2) edge node{2} (v3);
    \draw  (v3) edge node{3} (v4);
    \draw  (v4) edge node{4} (v5);
    \draw  (v5) edge node{5} (v6);
    \draw  (v6) edge node{6} (v1);
    \draw  (v5) edge node{7} (v1);
    \draw  (v5) edge node{8} (v2);
    \draw  (v4) edge node{9} (v2);
   \end{scope}
\end{tikzpicture}

% Third method    
\begin{tikzpicture}[shorten >=1pt, auto, node distance=3cm, ultra thick,
   edge_style/.style={draw=black, ultra thick}]

   \foreach [count=\i] \x/\y/\t in {-2/2/2, 2/2/3, 4/0/6, 2/-2/4, -2/-2/5, -4/0/1}
     \node [circle,draw=blue,fill=blue!20!,font=\sffamily\Large\bfseries]
        (v\i) at (\x,\y) {\t};

   \foreach \i/\j/\t in {1/2/1, 2/3/2, 3/4/3, 4/5/4, 5/6/5, 6/1/6, 5/1/7, 5/2/8, 4/2/9}
    \draw [edge_style]  (v\i) edge node{\t} (v\j);
\end{tikzpicture}
\end{document}

相关内容