8

I would like to show some numbers as weights of the edges of my Tikz graph. My graph without weights is the following:

\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}

I have tried looking for something on the web, but I have not found anything to show weights using the \draw edge command, which is the command I am using in my graph.

Could you suggest me how to solve my problem?

10

To add a label next to an edge, use the syntax edge node {some text}. See for example the second tutorial in TikZ's manual, section 3.11 Adding text next to lines.

Should you want to place the node on the other side of the line, use edge node[swap] {some text}.

Note also that \tikzstyle is considered deprecated, you should use \tikzset or add the styles to the tikzpicture options, as below. See Should \tikzset or \tikzstyle be used to define TikZ styles?

Below is three different ways of creating the same graph, one suggested by Alain in the comments.

enter image description here

\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}
  • In edge_style/.style={draw=black, line width=2, ultra thick} the option line width=2 is overwritten by ultra thick, so you can safely remove the former. – Gonzalo Medina Mar 17 '14 at 17:35
  • Possible : \begin{scope}[every node/.style={circle,draw=blue,fill=blue!20!,font=\sffamily\Large\bfseries}] \path (-2,2) node (v1) {2} ... and \begin{scope}[every edge/.style={draw=black, ultra thick}] \draw (v1) edge node{1} (v2); ... to avoid some repetitions .. – Alain Matthes Mar 17 '14 at 17:36
  • @GonzaloMedina Thanks, will edit. I didn't really look at the styles. – Torbjørn T. Mar 17 '14 at 18:17
  • Had difficulty replicating in my project, so it's important to note that auto is required for this to work. – StevenClontz Nov 22 '16 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.