# Create slide with graph in beamer

I am using beamer to create talks. I need to create a slide with a graph. Are there ways to create graphs in LaTeX or should create a picture somewhere and add a picture to a slide?

Edit: Oh, sorry for confusion. I meant this kind of graph:

-
Both are acceptable. You can create the graph in the most convinient way. –  Leo Liu Feb 18 '12 at 4:31
@LeoLiu but how create graph in beamer? –  ashim Feb 18 '12 at 4:53
Look at the pgfplots package. It is built on PGF/TikZ hence naturally supports beamer. –  percusse Feb 18 '12 at 4:58
You didn't really give much input. What kind of graph is it, 2D, 3D? Are you familiar with common visualisation software such as R or are you starting from scratch? –  Uwe Ziegenhagen Feb 18 '12 at 5:52
Creating the graph in a separate input file, compiling it with La(TeX) or its variants to produce a PDF output and importing the PDF output from within your main input file for presentation will be a good way to manage your work. No matter what documentclass you are using, you can always import image files as images from within the input file using graphicx package. But in general, the supported image format is not the same for each compiler. –  kiss my armpit Feb 18 '12 at 7:47

I recommend that you take a look at Graphviz for constructing graphs of any kind. The language is simple to grasp if you're used to dealing with graphs. In fact, the graph on the wikipedia page was made using Graphviz!

You can then use dot2tex to generate TikZ code which you can use in your beamer presentation with all the LaTeX prettiness.

Below is a graph written in dot (the Graphviz language), and then processed with dot2tex and Graphviz.

-

The following is a minimal example from LaTeX and Friends. You may also find this example in the lecture presentation about data plots.

\documentclass{beamer}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile]
\frametitle{Bar Graphs}
\begin{tikzpicture}[scale=0.85]
\begin{axis}[xbar,tick align=outside,
width=11cm,
height=8cm,
bar width={10pt},
enlargelimits=0.13,
nodes near coords,
nodes near coords align=horizontal,
point meta=x * 1, % The displayed number.
xlabel=\textbf{Number of Finals Won},
xtick={0,5,...,35},
ytick={1,...,13},
yticklabels={Kerry,Laois,London,Waterford,Clare,Offaly,
Galway,Wexford,Dublin,Limerick,Tipperary,Cork,Kilkenny}
]
[draw=blue,fill=blue!15]
coordinates
{(1,1) (1,2) (1,3) (2,4) (3,5) (4,6) (4,7) (6,8) (6,9) (7,10) (26,11) (30,12) (33,13)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}


EDIT: Undeleted on 2012-12-11.

-

An example of creating a graph in Beamer with the tkz-graph package:

\documentclass{beamer}
\usepackage{tkz-graph}

\begin{document}
\begin{frame}
\centerline{%
\begin{tikzpicture}
\SetGraphUnit{2}
\GraphInit[vstyle=Normal]
\SetVertexNoLabel
\Vertex{A}
\EA(A){B}
\EA(B){C}
\SO(C){D}
\SO(B){E}
\SO(A){F}
\Edge[label=a](A)(B)
\Edge[label=b](B)(C)
\Edge[label=c](C)(D)
\Edge[label=d](D)(E)
\Edge[label=e](E)(F)
\Edge[label=f](F)(A)
\tikzstyle{bentedge}=[bend
left]
\tikzstyle{thickbentedge}=[ultra
thick,
bend
left]
\Edge[label=g,style=bentedge](B)(E)
\Edge[label=h,style=bentedge](E)(B)
\SetUpEdge[style={ultra
thick}, color=red]
\pause
\only<+->{\Edge[label=a](A)(B)}
\only<+->{\Edge[label=g,style=thickbentedge](B)(E)}
\only<+->{\Edge[label=d](D)(E)}
\only<+->{\Edge[label=c](C)(D)}
\only<+->{\Edge[label=b](B)(C)}
\only<+->{\Edge[label=h,style=thickbentedge](E)(B)}
\only<+->{\Edge[label=e](E)(F)}
\only<+->{\Edge[label=f](F)(A)}
\end{tikzpicture}}
\end{frame}

\begin{frame}
\centerline{%
\begin{tikzpicture}
\SetGraphUnit{2}
\GraphInit[vstyle=Normal]
\Vertex{A}
\EA(A){B}
\EA(B){C}
\SO(C){D}
\SO(B){E}
\SO(A){F}
\Edge(A)(B)
\Edge(B)(C)
\Edge(C)(D)
\Edge(D)(E)
\Edge(E)(F)
\Edge(F)(A)
\tikzstyle{bentedge}=[bend
left]
\tikzstyle{thickbentedge}=[ultra
thick, bend left]
\Edge[style=bentedge](B)(E)
\Edge[style=bentedge](E)(B)
\SetUpEdge[style={ultra
thick}, color=red]
\pause
\only<+->{\Edge(A)(B)}
\only<+->{\Edge[style=thickbentedge](B)(E)}
\only<+->{\Edge(D)(E)}
\only<+->{\Edge(C)(D)}
\only<+->{\Edge(B)(C)}
\only<+->{\Edge[style=thickbentedge](E)(B)}
\only<+->{\Edge(E)(F)}
\only<+->{\Edge(F)(A)}
\end{tikzpicture}}
\end{frame}
\end{document}


Another example with a directed graph:

\documentclass{beamer}
\usepackage{tkz-graph}

\begin{document}
\begin{frame}
\frametitle{Camping Example:}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{description}
\item<alert@2>[$T_1$:]  Buy matches: 10 minutes
\item<alert@3>[$T_2$:]  Collect firewood: 8 minutes
\item<alert@4>[$T_3$:]  Light the fire: 6 minutes
\item<alert@5>[$T_4$:]  Get water: 12 minutes
\item<alert@6>[$T_5$:]  Cook soup: 15 minutes
\item<alert@7>[$T_6$:]  Make patties: 9 minutes
\item<alert@8>[$T_7$:]  Cook patties: 7 minutes
\end{description}
\end{column}
\begin{column}{.5\textwidth}
\begin{tikzpicture}[scale=.7]
\SetGraphUnit{2.7}
\GraphInit[vstyle=Normal]
\tikzset{EdgeStyle/.style = {->}}
\uncover<2->{
\Vertex[L=10]{a}
\node[above] at (a.90) {$T_1$};
}
\uncover<3->{
\SO[L=8](a){b}
\node[above] at (b.90) {$T_2$};
}
\uncover<4->{
\EA[L=6](b){c}
\node[above] at (c.90) {$T_3$};
\Edge(a)(c)
\Edge(b)(c)
}
\uncover<5->{
\SO[L=12](b){d}
\node[above] at (d.90) {$T_4$};
}
\uncover<6->{
\EA[L=15](c){e}
\node[above] at (e.90) {$T_5$};
\Edge(c)(e)
\Edge(d)(e)
}
\uncover<7->{
\SO[L=12](d){f}
\node[above] at (f.90) {$T_6$};
}
\uncover<8->{
\SO[L=7](e){g}
\node[above] at (g.90) {$T_7$};
\Edge(c)(g)
\Edge(f)(g)
}
\end{tikzpicture}
\end{column}
\end{columns}
\end{frame}
\end{document}


A good tool for manually creating graphs in a pdf and eps format that you can insert into your Beamer presentation is the Ipe graphics editor. You can easily create a multi-page pdf in which you build or color a graph incrementally, and insert the pages info your document as described in the Beamer manual.

Another tool that will make it easier to create graph in tikz for inclusion into your Beamer presentation is tikzit.

-

You could use tkz-graph.sty and tkz-berge.sty, which are build in top of tikz. I have some examples with code in my blog: Graph Theory in LaTeX. For example:

-