I've just started using TikZ to make basic graphs, and I'm trying to reproduce the Petersen graph. I've managed to make it without using for loops, but this obviously isn't a great method for bigger examples.
Currently, my new code stands at:
\begin{tikzpicture}
\foreach \x in {1,...,5}{
\pgfmathparse{(\x-1)*360/5+90}
\node[draw,circle,inner sep=8pt] (\x) at (\pgfmathresult:120pt) [semithick] {};
}
\foreach \x in {1,...,5}{
\pgfmathparse{(\x-1)*360/5+90}
\node[draw,circle,inner sep=8pt] (5+\x) at (\pgfmathresult:60pt) [semithick] {};
}
\end{tikzpicture}
This works, but isn't complete. However, I've realised that the second set of five nodes aren't numbered as 6, ... , 10 but as 5+1, ... , 5+5, which isn't what I want. I tried using \pgfmathparse{int(5+\x)}\pgfmathresult instead of 5+\x, but this results in an error. Strangely (at least to me), if I put \pgfmathparse{int(5+\x)}\pgfmathresult in the empty braces at the end of the second loop, it'll happily put the numbers 6, ... , 10 in the circles, so I can't see why my method shouldn't work.
What am I doing wrong, and why doesn't it work?
Edit: new code:
\begin{tikzpicture}
\foreach \i[evaluate={\angle=(\i)*360/5+90}] in {0,...,4}
{
\node[draw,circle,inner sep=8pt] (\i) at (\angle:120pt) [semithick] {};
}
\foreach \i[evaluate={\angle=(\i)*360/5+90}, evaluate={\Name=int(\i+5)}] in {0,...,4}
{
\node[draw, circle,inner sep=8pt] (\Name) at (\angle:60pt) [semithick] {};
}
\foreach \i in {0,...,4}
{
\pgfmathparse{int(mod(\i+1,5))}
\draw[-] (\i) -- (\pgfmathresult);
}
\foreach \i in {0,...,4}
{
\pgfmathparse{int(mod(\i,5)+5)}
\draw[-] (\i) -- (\pgfmathresult);
}
\foreach \i in {5,...,9}
{
\pgfmathparse{int(mod(\i+2,5)+5)}
\draw[-] (\i) -- (\pgfmathresult);
}
\end{tikzpicture}



evaluatein them to let me do anything else in the loop. Have you any pointers on how to improve it? – lokodiz Sep 12 '13 at 17:58strcatfunction for PGFmath so that you can easily build a node name that includes a “string” and a calculated part. Secondly, I added a third code that actually builds your diagram but it uses chains (which will build node names like<chain name>-<integer>) and thenodes around centerkey. We could probably create achainsstyle similar tonodes around centerthat also positions the inner nodes like they are connected (skipping every second position) but I don’t want to think about that algorithm. ;) – Qrrbrbirlbel Sep 12 '13 at 21:51