1

Consider the following code:

\documentclass[tikz, border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{math,calc}

\usepackage{xcolor} \definecolor{MyColor1}{rgb}{0.24, 0.59, 0.32} \definecolor{MyColor2}{rgb}{0.85, 0.49, 0.19} \definecolor{MyColor3}{rgb}{0.22, 0.42, 0.69}

\begin{document} \begin{tikzpicture} \foreach \x in {0,1,2}{ \draw[fill=MyColor1] (\x, 0) circle (2pt) node[anchor=south] (\x) {$V_{\x}$};
} \draw[fill=MyColor1] (3, 0) circle (2pt) node[anchor=south] (3) {$V_{n-1}$}; \draw[fill=MyColor1] (4, 0) circle (2pt) node[anchor=south] (4) {$V_n$};

\foreach \x in {-1,5}{
    \node at (\x,0) (\x) {$0$};
}    
\node at (2.5,0) {$\dots$};

\draw[->, MyColor3] (0) to [out=75, in=105] (1) node at (0.5,0.95) {$e$};
\draw[->, MyColor3] (1) to [out=75, in=105] (2) node at (1.5,0.95) {$e$};
\draw[->, MyColor3] (3) to [out=75, in=105] (4) node at (3.5,0.95) {$e$};

\node (tmp) at (-0.05,-0.35) {};
\node (tmp2) at (0.05,-0.35) {};    

\draw[->, MyColor2] ($(1)+ (tmp)$) to [out=-105, in=-75] ($(0)+(tmp2)$) node at (0.5,-0.6) {$f$};
\draw[->, MyColor2] ($(2)+ (tmp)$) to [out=-105, in=-75] ($(1)+(tmp2)$) node at (1.5,-0.6) {$f$};
\draw[->, MyColor2] ($(4)+ (tmp)$) to [out=-105, in=-75] ($(3)+(tmp2)$) node at (3.5,-0.6) {$f$};    

\draw[->, MyColor2] ($(0)+ (-0.05,-0.35)$) to [out=-105, in=-75] ($(-1)+ (0.05,-0.15)$) node at (-0.5,-0.6) {$f$};
\draw[->, MyColor3] (4) to [out=75, in=105] ($(5)+ (-0.05,0.53)$) node at (4.5,0.95) {$e$};    

\foreach \x in {0,1,2,3,4}{
    \path[->, MyColor1] ($(\x)+(0,0.3)$) edge [loop above, out=120, in=60, looseness=8] node {$h$} ($(\x)+(0.5,0.3)$);
}

\end{tikzpicture} \end{document}

which produces enter image description here

I would like the green arrows to be pointing to itself. I therefore tried to modify the line

\path[->, MyColor1] ($(\x)+(0,0.3)$) edge [loop above, out=120, in=60, looseness=8] node {$h$} ($(\x)+(0.5,0.3)$);

to

\path[->, MyColor1] ($(\x)+(0,0.3)$) edge [loop above, out=120, in=60, looseness=8] node {$h$} ($(\x)+(0,0.3)$);

following the answer of this question, but this results in only the arrow heads being drawn in place. I also tried the following

\path[->, MyColor1] ($(\x)+(-0.025,0.3)$) edge [loop above, out=120, in=60, looseness=8] node {$h$} ($(\x)+(0.025,0.3)$);

(baisically shifting the center to the label and then just making the gap between beginning and end smaller), but this resulted in a very small arrow... Can somebody maybe help a bit?

PS: I'm aware of the fact that I'm doing something wrong by constantly manipulating coordinates to specify beginning and end of arrows, etc. but I just don't really know how to do it otherwise.. If someone wants to suggest a solution for that as well, then it's obviously very appreciated!

Sito
  • 681

1 Answers1

4

Like this?

\documentclass[tikz, border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{math,calc}

\definecolor{MyColor1}{rgb}{0.24, 0.59, 0.32}
\definecolor{MyColor2}{rgb}{0.85, 0.49, 0.19}
\definecolor{MyColor3}{rgb}{0.22, 0.42, 0.69}

\begin{document}
\begin{tikzpicture}
    \foreach \x in {0,1,2}{
        \draw[fill=MyColor1] (\x, 0) circle (2pt) node[anchor=south] (\x) {$V_{\x}$};   
    }
    \draw[fill=MyColor1] (3, 0) circle (2pt) node[anchor=south] (3) {$V_{n-1}$};
    \draw[fill=MyColor1] (4, 0) circle (2pt) node[anchor=south] (4) {$V_n$};

    \foreach \x in {-1,5}{
        \node at (\x,0) (\x) {$0$};
    }   
    \node at (2.5,0) {$\dots$};

    \draw[->, MyColor3] (0) to [out=75, in=105] (1) node at (0.5,0.95) {$e$};
    \draw[->, MyColor3] (1) to [out=75, in=105] (2) node at (1.5,0.95) {$e$};
    \draw[->, MyColor3] (3) to [out=75, in=105] (4) node at (3.5,0.95) {$e$};

    \node (tmp) at (-0.05,-0.35) {};
    \node (tmp2) at (0.05,-0.35) {};    

    \draw[->, MyColor2] ($(1)+ (tmp)$) to [out=-105, in=-75] ($(0)+(tmp2)$) node at (0.5,-0.6) {$f$};
    \draw[->, MyColor2] ($(2)+ (tmp)$) to [out=-105, in=-75] ($(1)+(tmp2)$) node at (1.5,-0.6) {$f$};
    \draw[->, MyColor2] ($(4)+ (tmp)$) to [out=-105, in=-75] ($(3)+(tmp2)$) node at (3.5,-0.6) {$f$};   

    \draw[->, MyColor2] ($(0)+ (-0.05,-0.35)$) to [out=-105, in=-75] ($(-1)+ (0.05,-0.15)$) node at (-0.5,-0.6) {$f$};
    \draw[->, MyColor3] (4) to [out=75, in=105] ($(5)+ (-0.05,0.53)$) node at (4.5,0.95) {$e$}; 

    \foreach \x in {0,1,2,3,4}{
        \path[->, MyColor1] (\x) edge [loop above, out=120, in=60, looseness=16]
        node {$h$} (\x);
    }
\end{tikzpicture}
\end{document}

enter image description here

The problem with your approach is that you turn nodes, i.e. extended object, into coordinates by using the calc syntax. It seems to me that you do not want that here.

You can simplify the code quite a bit.

\documentclass[tikz, border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}% To get more advances positioning options
\usetikzlibrary{arrows}% To get more arrow heads
\usetikzlibrary{math,calc}

\definecolor{MyColor1}{rgb}{0.24, 0.59, 0.32}
\definecolor{MyColor2}{rgb}{0.85, 0.49, 0.19}
\definecolor{MyColor3}{rgb}{0.22, 0.42, 0.69}

\begin{document}
\begin{tikzpicture}
    \foreach \x in {0,1,2}{
        \draw[fill=MyColor1] (\x, 0) circle (2pt) node[anchor=south] (\x) {$V_{\x}$};   
    }
    \draw[fill=MyColor1] (3, 0) circle (2pt) node[anchor=south] (3) {$V_{n-1}$};
    \draw[fill=MyColor1] (4, 0) circle (2pt) node[anchor=south] (4) {$V_n$};

    \foreach \x in {-1,5}{
        \node at (\x,0) (\x) {$0$};
    }   
    \node at (2.5,0) {$\dots$};

    \node (tmp) at (-0.05,-0.35) {};
    \node (tmp2) at (0.05,-0.35) {};    
    \foreach \X in {1,2,4}
    {\draw[->, MyColor3] (\the\numexpr\X-1.50) to [out=75, in=105]
     node[above]  {$e$}(\X.130)  ;
    \draw[->, MyColor2] ($(\X)+ (tmp)$) to [out=-105, in=-75]
        node[below]{$f$}($(\the\numexpr\X-1)+(tmp2)$) ;}

    \draw[->, MyColor2] ($(0)+ (-0.05,-0.35)$) to [out=-105, in=-75] ($(-1)+ (0.05,-0.15)$) node at (-0.5,-0.6) {$f$};
    \draw[->, MyColor3] (4.50) to [out=75, in=105] node[above] {$e$} ++ (0.5,0);  

    \foreach \x in {0,1,2,3,4}{
        \path[->, MyColor1] ([xshift=-0.7ex,yshift=0.3ex]\x.north) edge [loop above, out=120, in=60, looseness=16]
        node {$h$} ([xshift=0.7ex,yshift=0.3ex]\x.north);
    }
\end{tikzpicture}
\end{document}

enter image description here

  • Would it be possible to not let the green and blue arrow intersect? Maybe by moving the green loop a bit higher and making the gap between the start and end a bit smaller? Otherwise this is exactly what I want. – Sito Feb 14 '20 at 21:50
  • @Sito Of course. I simplified the code and avoided the intersections in an update (the second example). –  Feb 14 '20 at 21:59
  • Looks very nice, thank you for the work! – Sito Feb 14 '20 at 22:03