3

I am trying to draw a graph with node and edge weights. Here is what I've managed to do so far. I've placed node names. but I want to place node weights under the circles. And I want to put edge weights too. I am not getting anywhere how to put edge label. Here is my current code.

\begin{tikzpicture}[transform shape]
\node[vertex][](t) at (2, 0) {$ \omega_{i} $};
\node[vertex][](r1) at (1, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex][](r2) at (3, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex][](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex][](q2) at (2,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex][](q3) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}]
       \draw (t) -- (q1);
       \draw (t) -- (q2); 
       \draw (t) -- (q3);

       \draw (t) -- (r1);
       \draw (t) -- (r2);
\end{scope}
\begin{scope}[every path/.style={-}]
       \draw (r1) -- (q1);
       \draw (r1) -- (q2);
       \draw (r1) -- (q3);
       \draw (r2) -- (q1);
       \draw (r2) -- (q2);
       \draw (r2) -- (q3);
\end{scope} 
\end{tikzpicture}
6
  • You can add an edge label like this: \draw (t) -- node [anchor=south] {$p_1$} (q1); You will have to play around a bit with getting the anchors right (choose north, south west, etc to see the effect). Other parameters you may want to look at is inner sep (which is the white space around the label) and pos (which determines where on the edge the label will appear).
    – yori
    Dec 12 '14 at 9:00
  • I don't want to specify anchor direction. also I want to align the label text by the edge line
    – Neel Basu
    Dec 12 '14 at 9:10
  • So you want the labels to be positioned automatically? I don't think that's possible. Adding node weights, by the way, can be done by \node[vertex, label=below:$10$](t) at (2, 0) {$ \omega_{i} $}; (also note that you don't need the extra []).
    – yori
    Dec 12 '14 at 10:05
  • That is easy with nodes. but aligning labels with edges is tuf. I think there is some rotation required.
    – Neel Basu
    Dec 12 '14 at 10:11
  • Ah, if you want the labels to be rotated automatically, this can actually be done automatically. See my answer below. You'll still need some positioning, but it gets a bit easier.
    – yori
    Dec 12 '14 at 10:19
2

You can add an edge label like this: \draw (t) -- node [anchor=south] {$p_1$} (q1). You will have to play around a bit with getting the anchors right (choose north, south west, etc., to see the effect). Other parameters you may want to look at is inner sep (which is the white space around the label) and pos (which determines where on the edge the label will appear).

Adding node weights can be done by \node[vertex, label=below:$10$](t) at (2, 0) {$ \omega_{i} $}; (also note that you don't need the extra []).

Here you go:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikzstyle{vertex}=[circle, draw]
\begin{tikzpicture}[transform shape]
\node[vertex, label=right:$1$](t) at (4, 0) {$ \omega_{i} $};
\node[vertex, label=above:$2$](r1) at (2, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex, label=above:$3$](r2) at (6, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex, label=below:$4$](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex, label=below:$5$](q2) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex, label=below:$6$](q3) at (8,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}, every node/.style={inner sep=1pt}]
       \draw (t) -- node [anchor=north west] {$1$} (q1);
       \draw (t) -- node [anchor=west]  {$2$} (q2); 
       \draw (t) -- node [anchor=north east] {$3$} (q3);

       \draw (t) -- node [anchor=north east] {$4$} (r1);
       \draw (t) -- node [anchor=north west] {$5$} (r2);
\end{scope}
\begin{scope}[every path/.style={-}, every node/.style={inner sep=1pt}]
       \draw (r1) -- node [anchor=east] {$6$} (q1);
       \draw (r1) -- node [pos=0.15, anchor=north east] {$7$} (q2);
       \draw (r1) -- node [pos=0.15, anchor=south west] {$8$} (q3);
       \draw (r2) -- node [pos=0.15, anchor=south east] {$9$} (q1);
       \draw (r2) -- node [pos=0.15, anchor=north west] {$10$} (q2);
       \draw (r2) -- node [anchor=west] {$11$} (q3);
\end{scope} 
\end{tikzpicture}
\end{document}

enter image description here

If you want your edge labels to be rotated automatically, you can use sloped:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikzstyle{vertex}=[circle, draw]
\begin{tikzpicture}[transform shape]
\node[vertex, label=right:$1$](t) at (4, 0) {$ \omega_{i} $};
\node[vertex, label=above:$2$](r1) at (2, 2) {$ r^{\omega_{i}}_{1} $};
\node[vertex, label=above:$3$](r2) at (6, 2) {$ r^{\omega_{i}}_{2} $};
\node[vertex, label=below:$4$](q1) at (0,-2) {$ q^{\omega_{i}}_{1} $};
\node[vertex, label=below:$5$](q2) at (4,-2) {$ q^{\omega_{i}}_{2} $};
\node[vertex, label=below:$6$](q3) at (8,-2) {$ q^{\omega_{i}}_{2} $};
\begin{scope}[every path/.style={-, dashed}, every node/.style={sloped, inner sep=1pt}]
       \draw (t) -- node [anchor=north] {$1$} (q1);
       \draw (t) -- node [anchor=south]  {$2$} (q2); 
       \draw (t) -- node [anchor=north] {$3$} (q3);

       \draw (t) -- node [anchor=north] {$4$} (r1);
       \draw (t) -- node [anchor=north] {$5$} (r2);
\end{scope}
\begin{scope}[every path/.style={-}, every node/.style={sloped, inner sep=1pt}]
       \draw (r1) -- node [anchor=south] {$6$} (q1);
       \draw (r1) -- node [pos=0.15, anchor=north] {$7$} (q2);
       \draw (r1) -- node [pos=0.15, anchor=south] {$8$} (q3);
       \draw (r2) -- node [pos=0.15, anchor=south] {$9$} (q1);
       \draw (r2) -- node [pos=0.15, anchor=north] {$10$} (q2);
       \draw (r2) -- node [anchor=south] {$11$} (q3);
\end{scope} 
\end{tikzpicture}
\end{document}

enter image description here

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.