# Can we define a multiedge command using tikz?

I usually need to draw graphs with multiple edges. I would really appreciate if someone can tell me how to write a multiple edge command which will allow me to write one line instead of repeatedly many. Say the name of my edge command is \myedge[m] meaning that I will draw m edges between two nodes.

I want to use something like the following whenever I need to draw a multiple edge (multiplicity is 5 in the following)

\draw (a) \myedge[5] (b);


Here is my actual code example... It is troublesome to keep using it in graphs where I have to keep drawing edges.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) -- (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=5] (b);
\draw[thick] (a) edge[bend right=5] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left] (b);
\draw[thick] (a) edge (b);
\draw[thick] (a) edge[bend right] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=15] (b);
\draw[thick] (a) edge[bend left=5] (b);
\draw[thick] (a) edge[bend right=5] (b);
\draw[thick] (a) edge[bend right=15] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[bend left=16] (b);
\draw[thick] (a) edge[bend left=8] (b);
\draw[thick] (a) edge[bend right=8] (b);
\draw[thick] (a) edge[bend right=16] (b);
\draw[thick] (a) -- (b);
\end{tikzpicture}
\end{center}
\end{document}


Output:

SITUATION: Here is what exactly I want to do in this new definition: Say, I would like to bend perpendicular to the line connecting two nodes (u) and (v) with coordinates (a,b) and (c,d). This gives me the chance to use ((c-a)*0.2*\i,(d-b)*0.2*\i) instead of $(0,0.2*\i)$ and it is most general. However, I can see that the bigger trouble here is that in defining edge[me=<number>] you actually do not account the ends of the edge. Can we do that?

This would certainly prevent the curly edges when edges are not on a horizontal line (it is worst when they are vertical actually). On the other hand, depending on how big is r=\sqrt{(c-a)^2+(d-b)^2} is, we might need to replace 0.2 with a much smaller/larger number. Maybe it is a good idea even to replace 0.2 with \frac{0.2}{r}.

• Welcome to TeX.sx! I've added in an image of what your code produces. If you edit the question before you get enough rep to include images yourself (I think it's 10), simply remove the ! at the start of the line where the image is included - someone can put that back in afterwards. I also cleaned up your code a little. – Andrew Stacey Mar 19 '12 at 13:31
• And on the question itself, how were the numbers (5,15,8,16) chosen? – Andrew Stacey Mar 19 '12 at 13:32
• It would be much better if you can ask a follow-up question. By doing so, we can also appreciate the additional efforts of the poster and answering user by upvoting. Moreover, the focus stays mutually exclusive and more users can benefit from it. – percusse Mar 20 '12 at 14:38

Update

Now it's better. If A and B are the vertices and M the middle of AB, you need to determine two points P and Q such as PM orthogonal to AM and QM orthogonal to AM. Then you can draw the curve from A to B with a good control point.

If you want to draw multi edges between vertices at any position you need to use the curveto command like in the next code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\tikzset{me/.style={to path={
\pgfextra{%
\pgfmathsetmacro{\startf}{-(#1-1)/2}
\pgfmathsetmacro{\endf}{-\startf}
\pgfmathsetmacro{\stepf}{\startf+1}}
\ifnum 1=#1 -- (\tikztotarget)  \else
let \p{mid}=($(\tikztostart)!0.5!(\tikztotarget)$)
in
\foreach \i in {\startf,\stepf,...,\endf}
{%
(\tikztostart) .. controls ($(\p{mid})!\i*6pt!90:(\tikztotarget)$) .. (\tikztotarget)
}
\fi
\tikztonodes
}}}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (1,1) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (5,3) {};
\draw[thick] (a) edge[me=5] (b);
\end{tikzpicture}
\end{document}


I define a new option mebut you can change the name. You need only to write [me=<number> ] It's possible to use an argument to modify 0.2 in 0.2*\i.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\tikzset{me/.style={to path={
\pgfextra{%
\pgfmathsetmacro{\startf}{-(#1-1)/2}
\pgfmathsetmacro{\endf}{-\startf}
\pgfmathsetmacro{\stepf}{\startf+1}}
\ifnum 1=#1 -- (\tikztotarget)  \else
\foreach \i in {\startf,\stepf,...,\endf}
{%
(\tikztostart)        parabola[bend pos=0.5] bend +(0,0.2*\i)  (\tikztotarget)
}
\fi
\tikztonodes
}}}

\begin{center}
\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=1] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=2] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=3] (b);
\end{tikzpicture}

\begin{tikzpicture}
\node[circle,fill=black,inner sep=1.5pt,draw] (a) at (180:1cm) {};
\node[circle,fill=black,inner sep=1.5pt,draw] (b) at (0:1cm) {};
\draw[thick] (a) edge[me=5] (b);
\end{tikzpicture}
\end{center}

\end{document}


• Lovely solution. Just for the clarification for others, an edge uses a to internally so installing the to path (as the me key does) has an effect (it wouldn't do if the path weren't an edge or a to). – Andrew Stacey Mar 19 '12 at 14:21
• Thanks everyone (most to Altermundus). This will save me huge time. – Barish Mar 19 '12 at 21:18
• Ohhh... Kaayyyy! I think I have another problem. When the vertices are not on a horizontal line, the edges become curly using – Barish Mar 20 '12 at 12:35
• Here is what exactly I want to do in this new definition: – Barish Mar 20 '12 at 14:18
• @Barish Do you want to adapt the style to draw multi edges with vertices in any position ? I think in this case, you need to use bezier curve (.. controls ..) instead of parabola. I don't have enough time actually but I look at the new problem in few hours. – Alain Matthes Mar 20 '12 at 14:44