11

Assume that I have a figure like the next one. How can I put two labels on the edge? For instance, here I would like to name the edge a and keep some auxiliary info associated with the edge.

\begin{figure}[b]
   \centering
    \begin{tikzpicture}
     \SetUpEdge[lw         = 1pt,
                color      = black,
                labelcolor = white]
      \SetVertexNoLabel
      \GraphInit[vstyle=Normal] 
      \SetGraphUnit{3}
      \tikzset{VertexStyle/.append  style={fill}}
      \Vertex{s}
      \NO(s){a}  \EA(a){b} \SO(b){c}
      %\Edge[label={$a$}](s)(a) and the next line
      \Edge[label={$\{g_1\}$}, labelstyle={left}](s)(a)
      \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
      \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
    \end{tikzpicture}
\end{subfigure}

You need package tkz-graph to compile the document.

2 Answers 2

8

I can add some explanations and other solutions.

Firstly, tkz-graph is a tool for drawing graphs at school and generally one edge has one label but I can understand that sometimes it's necessary to use two labels. Secondly, i use xkeyval for the keys and options and I have some difficulties to make something substantial; you can find some inconsistency in the syntax. In the next version, I will use pgfkeys and I think I will get a better code.

Actually, easiest way with all my packages, is to use TikZ when you get a problem here a solution is to add : (like Gonzalo's code)

 \path (s)-- node [right]{$a$}(a); 

The best way

Sorry I forget my personal code :(

When you draw an edge, style is used to draw the edge but labelstyle is to add extra options for the labels

With \Edge[label={$\{g_1\}$}, labelstyle={left,label=right:$a$}](s)(a) you put the main label with label= and you can add another label inside the labelstyle ! (I forgot this possibility !!

   \documentclass{article}
   \usepackage{tkz-graph}


   \begin{document}

   \begin{figure}[b]
      \centering
       \begin{tikzpicture}
        \SetUpEdge[lw         = 1pt,
                   color      = black,
                   labelcolor = white]
         \SetVertexNoLabel
         \GraphInit[vstyle=Normal] 
         \SetGraphUnit{3}
         \tikzset{VertexStyle/.append  style={fill}}
         \Vertex{s}
         \NO(s){a}  \EA(a){b} \SO(b){c} 

         \Edge[label={$\{g_1\}$}, labelstyle={left,label=right:$a$}](s)(a)

         \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
         \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
       \end{tikzpicture}
   \end{figure}

   % or with a circle

    \begin{tikzpicture}[rotate=-45]% needs another rotation here -45
    \SetVertexNoLabel
    \SetGraphUnit{3}
    \GraphInit[vstyle=Normal]  
    \Vertices{circle}{c,b,a,s}
    \Edge[label={left:$\{g_1\}$}, labelstyle={left,label=right:$a$,label=60:$b$}](s)(a)     
    \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
    \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)  
    \end{tikzpicture}  

   \end{document}  

You can add several labels with for example:

  \Edge[label={left:$\{g_1\}$}, labelstyle={left,label=right:$a$,label=60:$b$}](s)(a) 

More complex is the use of LabelStylelike this

   \documentclass{article}
   \usepackage{tkz-graph}

   \begin{document}

   \begin{figure}[b]
      \centering
       \begin{tikzpicture}
        \SetUpEdge[lw         = 1pt,
                   color      = black,
                   labelcolor = white]
         \SetVertexNoLabel
         \GraphInit[vstyle=Normal] 
         \SetGraphUnit{3}
         \tikzset{VertexStyle/.append  style={fill}}
         \Vertex{s}
         \NO(s){a}  \EA(a){b} \SO(b){c} 
         {\tikzset{LabelStyle/.style={label=right:$a$}}%
         \Edge[label={$\{g_1\}$}, labelstyle={left}](s)(a)  }

          %\path (s)-- node [right]{$a$}(a);   

         \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
         \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
       \end{tikzpicture}
   \end{figure}

   \end{document}    

You can see that I need to use a scope (TeX's group to limit the action of the style.

The result is always :

enter image description here

Another way to draw your graph

you can use \Vertices with the option square , this macro can place vertices on a line, a triangle, a square and a circle directly.

   \documentclass{article}
   \usepackage{tkz-graph}

    \begin{document} 

    \begin{tikzpicture}[rotate=45]
    \SetVertexNoLabel
    \SetGraphUnit{3}
    \GraphInit[vstyle=Normal]  
    \Vertices{square}{c,b,a,s}
    \Edge[label={left:$\{g_1\}$}, labelstyle={left,label=right:$a$,label=60:$b$}](s)(a)     
    \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
    \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)  
    \end{tikzpicture}  

Conclusion

1) Remember that tkz-graph uses TikZ and you can use the commands of TikZ.

2) Rmember that you can modify each style locally with a TeX's group

8

An option is to use two \Edges connecting the same vertices, each \Edge can contain its own label:

\documentclass{article}
\usepackage{tkz-graph}

\begin{document}

\begin{figure}[b]
   \centering
    \begin{tikzpicture}
     \SetUpEdge[lw         = 1pt,
                color      = black,
                labelcolor = white]
      \SetVertexNoLabel
      \GraphInit[vstyle=Normal] 
      \SetGraphUnit{3}
      \tikzset{VertexStyle/.append  style={fill}}
      \Vertex{s}
      \NO(s){a}  \EA(a){b} \SO(b){c}
      \Edge[label={$\{g_1\}$}, labelstyle={left}](s)(a)
      \Edge[label={$a$}, labelstyle={right}](s)(a)     
      \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
      \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

Or you can also use any valid TikZ construct; for example: a node in a \path:

\documentclass{article}
\usepackage{tkz-graph}

\begin{document}

\begin{figure}[b]
   \centering
    \begin{tikzpicture}
     \SetUpEdge[lw         = 1pt,
                color      = black,
                labelcolor = white]
      \SetVertexNoLabel
      \GraphInit[vstyle=Normal] 
      \SetGraphUnit{3}
      \tikzset{VertexStyle/.append  style={fill}}
      \Vertex{s}
      \NO(s){a}  \EA(a){b} \SO(b){c}
      \Edge[label={$\{g_1\}$}, labelstyle={left}](s)(a)
      \path (s) to node[label=right:$a$] {} (a);     
      \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
      \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

or simply a \node:

\documentclass{article}
\usepackage{tkz-graph}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}[b]
   \centering
    \begin{tikzpicture}
     \SetUpEdge[lw         = 1pt,
                color      = black,
                labelcolor = white]
      \SetVertexNoLabel
      \GraphInit[vstyle=Normal] 
      \SetGraphUnit{3}
      \tikzset{VertexStyle/.append  style={fill}}
      \Vertex{s}
      \NO(s){a}  \EA(a){b} \SO(b){c}
      \Edge[label={$\{g_1\}$}, labelstyle={left}](s)(a)
      \node[xshift=3mm] at ($ (s)!0.5!(a) $) {$a$};     
      \Edge[label={$\{g_1\}$}, labelstyle={above}](a)(b)
      \Edge[label={$\{g_1\}$}, labelstyle={right}](b)(c)
    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

2
  • 1
    I don't like the first solution because you draw twice the edge but it's a possibility. Commented May 24, 2012 at 6:44
  • @Altermundus me neither; that's why I added the other options. Commented May 24, 2012 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .