I'm considering moving from tkz-berge
to the new tikz graph library
for drawing my graphs (in the sense of graph theory). I have produced
the Petersen graph, but is there is a more elegant way of
coding it? I'm specifically interested in a way to avoid having to
define a new counter.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{graphs.standard}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,circle,very thick}]
\graph[clockwise, radius=2cm] {subgraph C_n [n=5,name=A]};
\graph[clockwise, radius=1cm] {subgraph I_n [n=5,name=B]};
\foreach \i in {1,2,3,4,5}{\draw (A \i) -- (B \i);}
\newcounter{j}
\foreach \i in {1,2,3,4,5}{%
\pgfmathsetcounter{j}{ifthenelse(mod(\i+2,5),mod(\i+2,5),5)}
\draw (B \i) -- (B \thej);
}
\end{tikzpicture}
\end{document}
Best Answer
Instead of explicitly defining a new counter
\j
you can useevaluate
. Of course, this isn't much of a saving as you still need to define\j
inside theevaluate
statment, but it does save a loop:Note that you need to take
int(...)
of themod
statement because otherwise you are asking to draw edges like(B 1.0) -- (B 3.0)
, which is not what you want.