Example: Drawing a graph

Published 2012-02-26 | Author: Stefan Kottwitz

In graph theory, models and drawings often consists mostly of vertices, edges, and labels. So, it may be possible, to use a simpler language for generating a diagram of a graph.

The tkz-graph package offers a convenient interface.

The code is fully explained in the LaTeX Cookbook, Chapter 11, Science and Technology, Application in graph theory.

Download as: [PDF] [TEX]

Drawing a graph

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% Drawing a graph
% Author: Stefan Kottwitz
% https://www.packtpub.com/hardware-and-creative/latex-cookbook
\documentclass[border=10pt]{standalone}
\usepackage{tkz-graph}
\GraphInit[vstyle = Shade]
\tikzset{
  LabelStyle/.style = { rectangle, rounded corners, draw,
                        minimum width = 2em, fill = yellow!50,
                        text = red, font = \bfseries },
  VertexStyle/.append style = { inner sep=5pt,
                                font = \Large\bfseries},
  EdgeStyle/.append style = {->, bend left} }
\thispagestyle{empty}
\begin{document}
\begin{tikzpicture}
  \SetGraphUnit{5}
  \Vertex{B}
  \WE(B){A}
  \EA(B){C}
  \Edge[label = 1](A)(B)
  \Edge[label = 2](B)(C)
  \Edge[label = 3](C)(B)
  \Edge[label = 4](B)(A)
  \Loop[dist = 4cm, dir = NO, label = 5](A.west)
  \Loop[dist = 4cm, dir = SO, label = 6](C.east)
  \tikzset{EdgeStyle/.append style = {bend left = 50}}
  \Edge[label = 7](A)(C)
  \Edge[label = 8](C)(A)
\end{tikzpicture}
\end{document}

Comments

  • #1 Richard Hsu, November 2, 2012 at 2:38 a.m.

    One of the best examples that beginners can use to create graphs without spending hours trying to figure out how to make something. Very clean! Thank you!

  • #2 Al.tro., September 5, 2013 at 12:04 p.m.

    Brilliant example. Which version of pgf-tikz does it need?

Adding comments is currently not enabled.