Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

Removing a set of braces and replacing one of the variables solve the problem.

\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 \x [count=\xi] in {#2}
    {% 
    \foreach \tritem in \x % <-- Here no braces to make it a foreach list also not \xi but \x
    \draw(a\xi) -- (b\tritem);
    }
    \end{tikzpicture}  
}

\begin{document}
\bipgraph{5}{{1,2},{4,3},{2,4,1}}
\end{document}

enter image description here

share|improve this answer
    
thanks, you have been very clear. I understood the error I did in reading the list :) –  Claudio Fiandrino May 4 '12 at 12:18
    
@ClaudioFiandrino You're very welcome. Good idea by the way. But Altermundus' tkz-berge package is a killer :) –  percusse May 4 '12 at 13:25
    
I started seen the tkz collection and yes.. it is a killer collection :). Anyway I learnt from my error applying your trick in quite different environment –  Claudio Fiandrino May 4 '12 at 15:02
    
@ClaudioFiandrino That's quite nice. Next stop is to get the same effect by using pgfkeys to make the input scheme more natural :) –  percusse May 5 '12 at 19:00

Only for information, you can draw all these kind of graphs with tkz-berge. There are many possibilities to draw the edges, here I show you two methods. The first very simple : \Edges(b2,a0,b0,a1,b2,a3,b1,a2) you get a chain of edges. The names of vertices are determined by a prefix a, b etc.. (it's an option) and a number (indice).

\Edges is a macro from tkz-graph. tkz-berge is useful for classic graphs (see the last example) and new tools are given to draw specific egdes like \EdgeFromOneToSel you draw from one vertex through a selection of vertices. There are also \EdgeFromOneToAll, EdgeIdentity etc.

\documentclass[11pt]{scrartcl}
\usepackage{tkz-berge}

\begin{document}
\begin{tikzpicture}
   \begin{scope}[rotate=90]
       \SetVertexMath
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture} 
\hspace{1cm}
\begin{tikzpicture}
   \begin{scope}[rotate=90]
       \SetVertexNoLabel  
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture} 

\begin{tikzpicture}
   \begin{scope}[rotate=90]
      \GraphInit[vstyle=Simple]
       \grEmptyLadder[RA=2,RB=4]{5}   
   \end{scope}
    \Edges(b2,a0,b0,a1,b2,a3,b1,a2)
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}
  \GraphInit[vstyle=Shade]
  \grPath[form=1,RA=2]{5}
  \grPath[form=1,prefix=b,RA=2,RS=4]{5}
  \EdgeFromOneToSel{a}{b}{0}{0,3}
  \EdgeFromOneToSel{a}{b}{1}{0,3}
  \EdgeFromOneToSel{a}{b}{3}{3}
  \EdgeFromOneToSel{a}{b}{4}{2,3}    
\end{tikzpicture}
\end{document} 

enter image description here

\documentclass[11pt]{scrartcl}
\usepackage{tkz-berge}

\begin{document}

\begin{tikzpicture}
 \GraphInit[vstyle=Shade] 
  \SetVertexNoLabel 
   \grCompleteBipartite[RA=4,RB=3,RS=6]{3}{5} 
   \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
I really like tkz-graph and tkz-berge! –  Gonzalo Medina May 4 '12 at 12:12
    
@Altermundus: thanks for the information. I didn't know those packages, but the first impression is very positive. I will document myself on that. –  Claudio Fiandrino May 4 '12 at 12:22
    
I'm surprised nobody linked to his website or his Gallery of named Graphs. They are natural places to follow up. –  DJP May 4 '12 at 22:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.