2
\begin{figure}[H]
        \centering
        
        \begin{tikzpicture}[
        C/.style = {circle, draw}, %   C: circle
        every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
        ]
        \begin{scope}[nodes={C}]
        \node (a1)   at (0,0)        {$v_2$};
        \node (a3) at (5,0)  {$v_4$};
        \node (a2) at (3,1) {$v_3$};
        \node (a0) at (2,-2)  {$v_1$};
        \node (a4) at (3,-1)  {$v_5$};
        \node (a6) at (2,2)  {$v_6$};
        \end{scope}
        %
        \path 
        (a1) edge ["\hspace{40mm} $e_{23}$"]  (a2)
        (a1) edge ["$e_{24}$"]  (a3)
        (a1) edge ["$e_{25}$"]  (a4)
        (a1) edge ["$e_{26}$"]  (a6)
        (a1) edge ["$e_{21}$"]  (a0);
        
    \end{tikzpicture}
\end{figure}

Why cant I add edges between the vertices $3$ and $4$ in this tikz picture? I dont know why when I add an edge between vertices $v_3$ and $v_4$ with a edge label $e_{34}$, the new edge does not show up and the entire graph gets disturbed.

Where am I doing wrong here? Can someone please help me out here.

3
  • 1
    Try (a1) edge node[below, pos=.75] {$e_{24}$} (a3). You can use above instead of below to shift the label above the edge and with pos, you can shift the label to the left and right (or up and down, depending on the orientation of the edge). Commented Mar 22, 2021 at 8:12
  • @JasperHabicht; its not working, can you please give a complete answer
    – Charlotte
    Commented Mar 22, 2021 at 8:14
  • I just got to know that there is a TikZ library for using a syntax such as yours. You need to load it first, however, in order to be able to use it. See my edit. Commented Mar 22, 2021 at 8:31

1 Answer 1

4

In general, in order to label edges you essentially need to place a node along the path of the edge. Whithout loading any TikZ library, this can be done as follows:

\documentclass[tikz,border=1pt]{standalone}

\begin{document}

    \begin{tikzpicture}[
    C/.style = {circle, draw}, %   C: circle
    every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
    ]
    
    \begin{scope}[nodes={C}]
    \node (a1) at (0,0)  {$v_2$};
    \node (a3) at (5,0)  {$v_4$};
    \node (a2) at (3,1)  {$v_3$};
    \node (a0) at (2,-2) {$v_1$};
    \node (a4) at (3,-1) {$v_5$};
    \node (a6) at (2,2)  {$v_6$};
    \end{scope}
    
    \path 
    (a1) edge node {\hspace{40mm} $e_{23}$} (a2)
    (a1) edge node[below, pos=.75] {$e_{24}$} (a3)
    (a1) edge node[below] {$e_{25}$} (a4)
    (a1) edge node[below, xshift=5pt] {$e_{26}$} (a6)
    (a1) edge node[below, yshift=-3pt] {$e_{21}$} (a0);
    
    \end{tikzpicture}

\end{document}

You can use the options above or below to place the label node above or below the edge. It is placed midways between the start and end point of the edge by default, but you can use the pos option to shift it (where pos=0 means at the start point and pos=1 at the end point). You may want to additionally shift the labels using xshift and yshift.

However, you probably got your code from someone who used the TikZ library quotes. You need to load this library in your preamble if you want to use it:

\documentclass[tikz,border=1pt]{standalone}

\usetikzlibrary{quotes} % <-- add this!

\begin{document}

    \begin{tikzpicture}[
        C/.style = {circle, draw}, %   C: circle
        every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt, pos=0.8}
        ]
        \begin{scope}[nodes={C}]
        \node (a1)   at (0,0)        {$v_2$};
        \node (a3) at (5,0)  {$v_4$};
        \node (a2) at (3,1) {$v_3$};
        \node (a0) at (2,-2)  {$v_1$};
        \node (a4) at (3,-1)  {$v_5$};
        \node (a6) at (2,2)  {$v_6$};
        \end{scope}
        %
        \path 
        (a1) edge ["$e_{23}$"{xshift=5pt}]  (a2)
        (a1) edge ["$e_{24}$"]  (a3)
        (a1) edge ["$e_{25}$"]  (a4)
        (a1) edge ["$e_{26}$"]  (a6)
        (a1) edge ["$e_{21}$"]  (a0);
        
    \end{tikzpicture}

\end{document}

You can still use the above mentioned options to shift the node. See the example fot the edge between nodes (a1) and (a2) in the above example.

enter image description here

4
  • Will it be possible for you to add an edge between $v_3$ and $v_4$, i dont know why but its not working in my latex document
    – Charlotte
    Commented Mar 22, 2021 at 8:46
  • Sure, just add \path (a2) edge ["$x$"] (a3);. Commented Mar 22, 2021 at 8:49
  • Thanks a lot, its now working , one question, why was the command (a2) edge ["$e_{23}$"] (a3); not working? Any idea
    – Charlotte
    Commented Mar 22, 2021 at 8:55
  • It looks fine. Maybe you forgot to put \path before or a semicolon behind it? Every path macro in TikZ need to end with a semicolon, but within a macro, there should not be a semicolon. So, you need to be careful that you either use multiple \path macros for each edge, or you use just one, but then you also only should use one semicolon at the end. But this is just a guess ... Commented Mar 22, 2021 at 8:58

You must log in to answer this question.

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