6

I am using the following code to draw a Petersen graph in LaTeX beamer:

\documentclass{article}
\usepackage{tkz-berge}
\begin{document}
\begin{tikzpicture}[rotate=90]
\GraphInit[vstyle=Art]
\grPetersen[RA=5, RB=3]
\end{tikzpicture}
\end{document}

The graph, all vertices with the same colour

It outputs a Petersen graph with all the nodes. I can't find how to color the vertices differently. I figured out how to change the color of all the vertices, but this is not what I need. I need to color one vertex red, then an adjacent one say blue, and so on. How to do this?

2

Generally, I find tkz-berge is useful for doing standard graphs quickly in a pinch, but if I need to deviate from a standard graph, it's usually faster to break out plain TikZ and write it from scratch. That's what I've done in the example below.

A tkz guru may be able to colour the vertices differently with ease, but I found this method easier (and fairly simple).

Here's what it looks like:

enter image description here

and here's the associated code:

\documentclass{article}
\usepackage{tkz-berge}
\begin{document}

% Draw a cycle of vertices
\newcommand*{\drawvertices}[7]{
  \foreach \t/\mycolor in {1/#1, 2/#2, 3/#3, 4/#4, 5/#5} {
    \begin{scope}[rotate = \t * 72, xshift=#6,
      VertexStyle/.append style = {ball color = \mycolor}]
      \Vertex{#7\t}
    \end{scope}
  }
}

% Inner 5-cycle
\newcommand*{\innervertices}[5]{
  \drawvertices{#1}{#2}{#3}{#4}{#5}{\RA}{a}
}

% Outer 5-cycle
\newcommand*{\outervertices}[5]{
  \drawvertices{#1}{#2}{#3}{#4}{#5}{\RB}{b}
}

% The star-like edges in the inner cycle
\newcommand*{\staredges}[5]{
  \foreach \u/\v/\edgecolor in {1/3/#1, 3/5/#2, 5/2/#3, 2/4/#4, 4/1/#5} {
    \tikzstyle{EdgeStyle} = [color = \edgecolor]
    \Edges(a\u, a\v)
  }
}

% The outermost edges
\newcommand*{\outeredges}[5]{
  \foreach \u/\v/\edgecolor in {1/2/#1, 2/3/#2, 3/4/#3, 4/5/#4, 5/1/#5} {
    \tikzstyle{EdgeStyle} = [color = \edgecolor]
    \Edges(b\u, b\v)
  }
}

% The edges that join the inner and the outer five cycle
\newcommand*{\betweenedges}[5]{
  \foreach \u/\edgecolor in {1/#1, 2/#2, 3/#3, 4/#4, 5/#5} {
    \tikzstyle{EdgeStyle} = [color = \edgecolor]
    \Edges(a\u, b\u)
  }
}

\begin{tikzpicture}[rotate=90]
\GraphInit[vstyle=Art]

% Set the inner and outer radius as with \grPetersen
\newcommand*{\RA}{2.4cm}
\newcommand*{\RB}{5cm}

\innervertices{green}{red}{orange}{yellow}{blue}
\outervertices{purple}{cyan}{magenta}{lime}{pink}

\staredges{red}{orange}{green}{blue}{pink}

\outeredges{green}{blue}{purple}{cyan}{yellow}

\betweenedges{blue}{pink}{cyan}{teal}{yellow}

% Get numbered labels (like in the picture)
% \foreach \number in {1,...,5} {
%  \begin{scope}[rotate = \number * 72, xshift=\RB+0.8cm]
%    \draw (0,0) node {\LARGE{$\number$}};
%  \end{scope}
% }

\end{tikzpicture}
\end{document}

What you change

You need to set \RA and \RB to the inner/outer radii that you want; there are analogous to the parameters RA and RB in \grPetersen.

In each of the commands from \innervertices{} to \betweenedges{}, change the colours for those that you'd like. Each time, you start at the vertex or edge incident with 1, and head counter-clockwise. (Look at the picture if it helps.)

The colors are defined by xcolor. You can find a list of the standard colors on page 17 of the package documentation, or find out how to define your own colours in the same document.

I set up vertices just as you do in tkz-berge normally: with a \Vertex command, and they're moved to the appropriate position with a scope. These are then joined with the tkz-berge command \Edges().

| improve this answer | |
  • 1
    Is there a way to color edges locally as well? – Mat Nov 7 '13 at 3:31
  • @Mat: done. I also broke chunks of it out into commands so that it should be easier to reuse. – alexwlchan Nov 7 '13 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.