I need to draw simple graph (for example Petersen Graph) in LaTeX. I am using Kile in Ubuntu. I exactly don't know that which package should I use. Any suggestion with example is highly expected.
-
3A good tutorial : Create Trees and Figures in Graph Theory with PSTricks.– kiss my armpitMay 24, 2012 at 8:20
-
2»pgf/tikZ« is very popular for doing such things.– Thorsten DonigMay 24, 2012 at 8:27
-
Check texample.net for possible examples of Petersen or similar graphs.– Martin ScharrerMay 24, 2012 at 8:29
-
2possible duplicate of Typesetting a directed, weighted graph with TikZ– percusseMay 24, 2012 at 8:31
-
1If you use other programs to generate your graphs (e.g. Veusz), you can also easily convert the svg output into an editable LaTeX graph, as there's an extension for Inkscape to save as tikz. Yetish Joshi wrote a guest post on our blog explaining the steps to do this -- if this is what you were thinking with your question then you can find more info here. Hope this helps (this didn't feel like a full answer so I posted it as a comment).– John HammersleyAug 15, 2013 at 22:47
5 Answers
I think the best approach is to use the tkz-berge from Altermundus. You can also find a complete guide here.
However, to make a simple example just using TikZ
, you can follow this approach:
\documentclass {article}
% example taken from
% http://www.guitex.org/home/images/doc/GuideGuIT/introingtikz.pdf
\usepackage {tikz}
\usetikzlibrary {positioning}
%\usepackage {xcolor}
\definecolor {processblue}{cmyk}{0.96,0,0,0}
\begin {document}
\begin {center}
\begin {tikzpicture}[-latex ,auto ,node distance =4 cm and 5cm ,on grid ,
semithick ,
state/.style ={ circle ,top color =white , bottom color = processblue!20 ,
draw,processblue , text=blue , minimum width =1 cm}]
\node[state] (C)
{$1$};
\node[state] (A) [above left=of C] {$0$};
\node[state] (B) [above right =of C] {$2$};
\path (A) edge [loop left] node[left] {$1/4$} (A);
\path (C) edge [bend left =25] node[below =0.15 cm] {$1/2$} (A);
\path (A) edge [bend right = -15] node[below =0.15 cm] {$1/2$} (C);
\path (A) edge [bend left =25] node[above] {$1/4$} (B);
\path (B) edge [bend left =15] node[below =0.15 cm] {$1/2$} (A);
\path (C) edge [bend left =15] node[below =0.15 cm] {$1/2$} (B);
\path (B) edge [bend right = -25] node[below =0.15 cm] {$1/2$} (C);
\end{tikzpicture}
\end{center}
\end{document}
which leads to:
It is an example of a Markov Chain in which several TikZ
options are used. In the same guide, you will find an example in which nodes are placed in a matrix.
-
1I remove some blank spaces before style and after the color because the code doesn't compile (problem with cut/past ?) . You don't need to load xcolor because tikz loads it but if you need some extra options of xcolor, you need to load xcolor before tikz. If you use
edge
instead ofto
, you can remove some extra commands example \path (A) edge ... (A) edge ... (B) edge ... (C) ; gives three edges that start from (A) May 24, 2012 at 10:04 -
Thanks: great explanation. :) The problem of compilation was exactly due to cut and paste from the guide: I later removed several blank spaces, but probably I forgot someone. May 24, 2012 at 11:22
-
1@Ziezi: sorry, I never translated it and I'm not aware of any tranlations. Sep 6, 2017 at 18:32
-
1@ClaudioFiandrino Never mind, it looks great and provides practical examples from many fields of Physics, it would have been great if I knew Italian, and understood the rest of the text.– ZieziSep 6, 2017 at 19:41
-
1@Master.AKA: you should try a combination of bends and or the definition of in and out angles (see tex.stackexchange.com/q/209303/13304) May 1, 2020 at 12:55
You can use the tkz-berge package . This package uses TikZ it's possible to add all the commands of TikZ.
Here the three classic forms of Petersen's graph
\documentclass[11pt]{scrartcl}
\usepackage{tkz-berge}
\begin{document}
\begin{tikzpicture}[scale=.5]
\GraphInit[vstyle=Art]
\SetGraphArtColor{red}{olive}
\grPetersen[form=1,RA=5,RB=3]%
\end{tikzpicture}
\begin{tikzpicture}[scale=.4]
\GraphInit[vstyle=Art]
\SetGraphArtColor{red}{olive}
\grPetersen[form=2,RA=7,RB=3]%
\end{tikzpicture}
\begin{tikzpicture}[scale=.5]
\GraphInit[vstyle=Art]
\SetGraphArtColor{red}{olive}
\grPetersen[form=3,RA=7]%
\end{tikzpicture}
\end{document}
I wrote a document with some classic named graphs. It's inside the your distribution (>2011 I think)
texdoc NamedGraphs
in the terminal
I added the sources and all the examples you can also find them here
Here an example to draw the Petersen's graph only with TikZ I try to structure correctly the code. The first scope is used for vertices ans the second one for edges. The only problem is to get the edges with `mod``
\pgfmathtruncatemacro{\nextb}{mod(\i+1,5)}
\pgfmathtruncatemacro{\nexta}{mod(\i+2,5)}
The complete code
\documentclass[border=6pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope} [vertex style/.style={draw,
circle,
minimum size=6mm,
inner sep=0pt,
outer sep=0pt,
shade}]
\path \foreach \i in {0,...,4}{%
(72*\i:2) coordinate[vertex style] (a\i)
(72*\i:4) coordinate[vertex style] (b\i)}
;
\end{scope}
\begin{scope} [edge style/.style={draw=gray,double=white}]
\foreach \i in {0,...,4}{%
\pgfmathtruncatemacro{\nextb}{mod(\i+1,5)}
\pgfmathtruncatemacro{\nexta}{mod(\i+2,5)}
\draw[edge style] (a\i)--(b\i);
\draw[edge style] (a\i)--(a\nexta);
\draw[edge style] (b\i)--(b\nextb);
}
\end{scope}
\end{tikzpicture}
\end{document}
As there lacks an answer with pstricks, here a very simple code for the Petersen graph; it uses pst-poly
:
\documentclass[12pt, pdf, x11names]{standalone}%
\usepackage{pstricks-add}
\usepackage{pst-poly}
\begin{document}
\begin{pspicture*}(-1.1,-1)(1.1,1.1)
\providecommand{\PstPolygonNode}{%
\psdots[dotstyle=o, dotsize=3pt,fillstyle=solid, fillcolor=cyan](1;\INode)}
\psset{linewidth=0.6pt}
\rput(0,0){\PstPentagon[PolyName=A]}
\rput(0,0){\PstPentagon[unit=5mm, PolyOffset=2,PolyName=B]}
\multido{\i=1+1}{5}{\ncline[nodesep=1.5pt]{A\i}{B\i}}
\end{pspicture*}
\end{document}
In case you do not want to do the layout yourself, you can use Graphviz to do so. That will result in a ".dot" file, that can be converted to Latex with dot2tex.
I just used my GraphvizFiddle tool to get "circo" layout for Petersen graph and verified that dot2tex works. Here is share link for the Petersen graph, switch output from "svg" to "dot" and press "Draw" button to get the Petersen ".dot" file. The tex output uses "tikzpicture" as well.