In a document I need to insert several bipartite graphs, each one with different number of nodes and edge composition.
My idea was to create a command with two arguments:
- the first one for the number of nodes;
- the second one with the list of connections.
The call to this command is:
\bipgraph{5}{{1,2},{4,3},{2,4,1}}
in which there are 5 nodes and connections are built as:
the first node on the left side is connected to
{1,2}
, the first and the second node of the right side;the second node on the left side is connected to
{4,3}
, the fourth and the third node of the right side.
This is my MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\bipgraph}[2]{%
\begin{tikzpicture}[every node/.style={circle,draw}]
\foreach \xitem in {1,2,...,#1}
{%
% first set
\node at (0,\xitem) (a\xitem) {};
% second set
\node at (2,\xitem) (b\xitem) {};
}%
% connections
\foreach \dritem [count=\yi] in {#2}
{%
\foreach \tritem in {\yi}
\path (a\yi) edge (b\tritem);
}
\end{tikzpicture}
}
\begin{document}
\bipgraph{5}{{1,2},{4,3},{2,4,1}}
\end{document}
which gives as result:
I think the problem arises when I read the list of connections in the second argument of \bipgraph
, but I don't know how to fix it.