# Draw a graph using xymatrix. Is there another way?

I am using the package xy to draw simple Graphs or Networks. It allows oriented and non oriented edges and even weights on the edges, but the code is very complicated and it is very difficult to build bigger examples than the ones shown bellow.

Does anybody know another way to do this (a simple way)?

An example of the code I currently use:

\begin{figure}
\centering
\begin{minipage}{0.4\textwidth}
\centering
$\xymatrix{ \xy*{1}*\cir<6pt>{}\endxy\ar@{->}[r] & \xy*{2}*\cir<6pt>{}\endxy\ar@{->}[d] \\ \xy*{4}*\cir<6pt>{}\endxy\ar@{->}[ur]\ar@{->}@/_/[r]\ar@{->}[u] & \xy*{3}*\cir<6pt>{}\endxy\ar@{->}[lu]\ar@{->}@/_/[l] }$
\end{minipage}
%\hfill
\begin{minipage}{0.4\textwidth}
\centering
$\xymatrix{ \xy*{1}*\cir<6pt>{}\endxy\ar@{-}[r] & \xy*{2}*\cir<6pt>{}\endxy\ar@{-}[d] \\ \xy*{4}*\cir<6pt>{}\endxy\ar@{-}[ur]\ar@{-}[r]\ar@{-}[u] & \xy*{3}*\cir<6pt>{}\endxy\ar@{-}[lu] }$
\end{minipage}

\label{fig:graphExample}
\end{figure}


The result is:

Something like this via tikz. First define a style file m for all nodes and allocate the position of node 1 with an (internal) and {external label} and the remaining nodes can be allocated by using the relative position commands right/left/above/below = xx cm of <node 1> in the option. After that use

\draw[m,->] (< node 1 >) -- (< node 2 >);

for curve lines use (a) to [bend right/left, out=<ang1>, in=<ang2>] (b)

Code

\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\tikzset{
m/.style={circle,draw,fill=gray!40,minimum size=20},outer sep=5pt}
\begin{document}
\begin{tikzpicture}
\node[m] at (0,0)(1){1};
\node[m, right=1cm of 1](2){2};
\node[m, below=1cm of 2](3){3};
\node[m, below=1cm of 1](4){4};
\draw[-] (1)--(2) (2)--(3) (3)--(4) (4)--(1);
\draw[-] (4) -- (2);
\draw[-] (3) -- (1);
\end{tikzpicture}
\par

\begin{tikzpicture}
\node[m] at (0,0)(1){1};
\node[m, right=1cm of 1](2){2};
\node[m, below=1cm of 2](3){3};
\node[m, below=1cm of 1](4){4};
\draw[->] (1) -- (2);
\draw[->] (2) -- (3);
\draw[->] (4) -- (1);
\draw[->] (3) to[bend right] (4);
\draw[->] (4) to[bend right] (3);
\draw[->] (4) -- (2);
\draw[->] (3) -- (1);
\end{tikzpicture}
\end{document}

• I hope you don't mind, I added the two missing arrows. (Quite strange, that \draw[->] (1)--(2) (2)--(3); does not work. Would have expected the same... – LaRiFaRi Jun 5 '14 at 10:40

You can do it with . You find the documentation on the usepackage pst-node and some pictures on TUG:psmatrix

Here the code for your picture (compile with latex->dvips>pstopdf)

\documentclass{article}

\usepackage{pst-all}

\begin{document}
\psset{nodesep=3pt}
\begin{psmatrix}[mnode=circle,rowsep=3cm,colsep=3cm]
[name=A] 1 & [name=B] 2\\
[name=C]4 & [name=D]3

\ncline{->}{A}{B}
\ncline{->}{B}{D}
\ncline{->}{C}{A}
\ncline{->}{C}{B}
\ncline{->}{D}{A}
\ncarc[arcangle=20]{->}{C}{D}
\ncarc[arcangle=20]{->}{D}{C}
\end{psmatrix}
\hspace{1cm}
\begin{psmatrix}[mnode=circle,rowsep=3cm,colsep=3cm]
[name=A] 1 & [name=B] 2\\
[name=C]4 & [name=D]3

\ncline{A}{B}
\ncline{B}{D}
\ncline{C}{A}
\ncline{C}{B}
\ncline{D}{A}
\ncline{C}{D}
\end{psmatrix}

\end{document}


For completeness sake here is an answer using :

% arara: pdflatex

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}
$\begin{tikzcd} \tikz{\node[draw,circle]{1}} \ar{r} & \tikz{\node[draw,circle]{2}} \ar{d}\\ \tikz{\node[draw,circle]{4}} \ar{u} \ar{ur} \ar[bend right=15]{r} & \tikz{\node[draw,circle]{3}} \ar[crossing over]{ul} \ar[bend right=15]{l} \end{tikzcd}$
$\begin{tikzcd}[every arrow/.append style=dash] \tikz{\node[draw,circle]{1}} \ar{r} & \tikz{\node[draw,circle]{2}} \ar{d}\\ \tikz{\node[draw,circle]{4}} \ar{u} \ar{r} \ar{ur} & \tikz{\node[draw,circle]{3}} \ar[crossing over]{ul} \end{tikzcd}$
\end{document}