76

First of all, let me say I'm using LyX, though I have no problem using ERT.

Secondly, what is the most simplest way to draw a simple graph like this in Latex? alt text

I've seen some documents with graphs and I've seen some examples, but I couldn't figure out how to just draw a simple graph - what packages do I need, etc?

Community
  • 1
  • 1
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248

5 Answers5

109

TikZ can do this.

A quick demo:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  [scale=.8,auto=left,every node/.style={circle,fill=blue!20}]
  \node (n6) at (1,10) {6};
  \node (n4) at (4,8)  {4};
  \node (n5) at (8,9)  {5};
  \node (n1) at (11,8) {1};
  \node (n2) at (9,6)  {2};
  \node (n3) at (5,5)  {3};

  \foreach \from/\to in {n6/n4,n4/n5,n5/n1,n1/n2,n2/n5,n2/n3,n3/n4}
    \draw (\from) -- (\to);

\end{tikzpicture}

\end{document}

produces:

enter image description here

More examples @ http://www.texample.net/tikz/examples/tag/graphs/

More information about TikZ: http://sourceforge.net/projects/pgf/ where I guess an installation guide will also be present.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 2
    Specifically, the [tkz-berge package](http://www.texample.net/tikz/examples/tkz-berge/) may be especially useful. – Alex R Mar 08 '11 at 19:38
  • @Alex, great link! (even though it is at the bottom of the `More examples` link I already posted :)) – Bart Kiers Mar 08 '11 at 19:49
  • 1
    coming back to this question - is it necessary to specify the coordinates? Can it handle on itself somehow? – Amir Rachum Jun 03 '11 at 22:19
  • Also, how can I add directed edges with notations on them? – Amir Rachum Jun 03 '11 at 22:30
  • 1
    @Amir Rachum, the examples page posted in my original answer shows a graph including edges (see "Example: A Petri-net for Hagen"). Whether it is possible to do automatic layout, as [Graphviz](http://www.graphviz.org/) does, I don't know. – Bart Kiers Jun 05 '11 at 11:58
3

Aside from the (excellent) suggestion to use TikZ, you could use gastex. I used this before TikZ was available and it did its job too.

Pieter
  • 2,822
  • 18
  • 17
3

I have used graphviz ( https://www.graphviz.org/gallery ) together with LaTeX using dot command to generate graphs in PDF and includegraphics to include those.

If graphviz produces what you are aiming at, this might be the best way to integrate: dot2tex: https://ctan.org/pkg/dot2tex?lang=en

Agile_Eagle
  • 1,710
  • 3
  • 13
  • 32
Ville Laitila
  • 1,187
  • 11
  • 18
  • 2
    dot2tex is good, and the dot2texi LaTeX package is very good. It allows you to define your graph inline in your .tex file using the dot format, and then to annotate it with TikZ (see the nice example at the end of http://www.fauskes.net/code/dot2tex/documentation/#the-dot2texi-latex-package). – Mark Mar 10 '13 at 00:29
2

Perhaps use tikz.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Any way you can help me get this package installed? – Amir Rachum Jun 06 '10 at 19:23
  • @Amir Go to the sourceforge download page to get the package, then read the directions - they are pretty straight forward. – zdav Jun 06 '10 at 19:36
  • 3
    @Amir: How to install the package depends on your OS. On Ubuntu, `tikz` is provided by the `pgf` package. So all one has to do there is `sudo apt-get install pgf`. – unutbu Jun 06 '10 at 19:44
  • 2
    In Windows I didn't have to do anything. Once I had the \usepackage{tikz} in my preamble, miktex did all the rest. Same with mac. Nothing had to be done, I just had to call the package. – Vivi Jun 07 '10 at 00:36
0

In my experience, I always just use an external program to generate the graph (mathematica, gnuplot, matlab, etc.) and export the graph as a pdf or eps file. Then I include it into the document with includegraphics.

zdav
  • 2,752
  • 17
  • 15