2

I am fairly new in TikZ and even more in TikZ macros. I wanted to modify the macros given in another thread, and the goal is to implement node loops but to be able to enter the in and out angles as parameters of the macro. Something, like the following code:

\documentclass[tikz]{standalone}

% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
    \foreach [count=\r] \row in {#3}{
        \foreach [count=\c] \cell in \row{
            \ifnum\cell>0
                \ifnum\c=\r
                    \draw[arc/.try=\cell] (#2\r) edge[loop arc/.try=\c , loop arc/.style={in=#4\r , out=#5\r}] node[arc label/.try=\cell]{\cell} (#2\c);
                \else
                    \draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
                \fi
            \fi
        }
    }
}   

\begin{document}
\begin{tikzpicture}[scale=4,
    vertex/.style={draw,circle},
    arc/.style={draw=blue!#10,thick,->},
    arc label/.style={fill=white, font=\tiny, inner sep=1pt},
    loop arc/.style={min distance=2.5mm}
    ]
    \foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
        \node[vertex] (p\i) at \coord {\i};
    }

    \weigthgraphfromadj[bend left=10]{p}
    {{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{{0,36,72,108,144,180,216,252,288,324}}{{30,66,102,138,174,210,246,282,318,354}}
\end{tikzpicture}
\end{document}

The macro takes the graph adjacency matrix in order to draw the oriented graph, however, the angles for the loops should be adapted to the vertex position which was not the case in the former post.

Thank you for in advance for your help.

Romain.

  • 3
    can you also add dummy arguments for testing – percusse Apr 16 '18 at 10:34
  • 1
    It will be very hard because of the way you set this up. For instance, the coordinates in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)} should be better set up as polar coordinates, in which case you'd know their angles. In the present setting you'd have to calculate the angles. – marmot Apr 16 '18 at 13:31
2

enter image description here

\documentclass[tikz]{standalone}
\usepackage{luatex85}
% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
    \foreach [count=\r] \row in {#3}{
        \foreach [count=\c] \cell in \row{
            \ifnum\cell>0
                \ifnum\c=\r
         \pgfmathsetmacro{\In}{36*\r-45}
         \pgfmathsetmacro{\Out}{36*\r+45}
         \draw[arc/.try=\cell] (#2\r)
         edge[loop arc,in=\In , out=\Out]
         node[arc label/.try=\cell]{\cell} (#2\c);
                \else
                    \draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
                \fi
            \fi
        }
    }
}   

\begin{document}
\begin{tikzpicture}[scale=4,
    vertex/.style={draw,circle},
    arc/.style={draw=blue!#10,thick,->},
    arc label/.style={fill=white, font=\tiny, inner sep=1pt},
    loop arc/.style={min distance=2.5mm}
    ]
    \foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
        \node[vertex] (p\i) at \coord {\i};
    }

    \weigthgraphfromadj[bend left=10]{p}
    {{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{{0,36,72,108,144,180,216,252,288,324}}{{120,66,102,138,174,210,246,282,318,354}}
\end{tikzpicture}
\end{document}
1

This is what I was looking for, the argument #4 is the angle between the vertices and the argument #5 is half of the angle for the in and out arguments of the loops.

\documentclass[tikz]{standalone}

% Weighted graph from adjacency matrix
\newcommand{\weigthgraphfromadj}[5][draw,->]{
    \foreach [count=\r] \row in {#3}{
        \foreach [count=\c] \cell in \row{
            \ifnum\cell>0
                \ifnum\c=\r
                    \draw[arc/.try=\cell] (#2\r) edge[loop arc, in= #4*\r -#5 , out= #4*\r +#5] node[arc label/.try=\cell]{\cell} (#2\c);
                \else
                    \draw[arc/.try=\cell, #1] (#2\r) edge node[arc label/.try=\cell]{\cell} (#2\c);
                \fi
            \fi
        }
    }
}   

\begin{document}
\begin{tikzpicture}[scale=4,
    vertex/.style={draw,circle},
    arc/.style={draw=blue!#10,thick,->},
    arc label/.style={fill=white, font=\tiny, inner sep=1pt},
    loop arc/.style={min distance=2.5mm}
    ]
    \foreach [count=\i] \coord in {(0.809,0.588),(0.309,0.951),(-0.309,0.951),(-0.809,0.588),(-1.,0.),(-0.809,-0.588),(-0.309,-0.951),(0.309,-0.951),(0.809,-0.588),(1.,0.)}{
        \node[vertex] (p\i) at \coord {\i};
    }

    \weigthgraphfromadj[bend left=10]{p}
    {{5,5,0,0,1,0,5,0,0,5},{2,5,1,0,0,5,0,2,0,0},{0,5,5,2,0,0,2,0,5,0},{0,0,7,5,5,0,0,2,0,5},{7,0,0,7,5,5,0,0,1,0},{0,5,0,0,2,5,5,0,0,1},{2,0,5,0,0,1,5,5,0,0},{0,7,0,5,0,0,2,5,1,0},{0,0,5,0,7,0,0,5,5,1},{5,0,0,5,0,1,0,0,1,5}}{36}{20}
\end{tikzpicture}
\end{document}

Thanks

Romain

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.