For a document I am working on, I wanted to be able to draw polygon graphs. The graphs are to be labelled with the integers from 0
to n - 1
, for some n. I wanted to write a macro that could be called inside a tikzpicture to draw such a graph, when supplied with the number of edges (n) and the "radius" of the graph (distance of each vertex from the centre). It turned out to be quite a frustrating experience because trying to use mathematical expressions involving macro arguments in TikZ's \foreach
is not straightforward and results in many baffling error messages, but eventually I realised I just need to use \pgfmathparse
to avoid the hassle. Yet I now have a perfectly good graph, except that the edges are "wonky" - each edge seems to connect from one node to a point slightly off the next node. Why is this happening? Here is a screengrab of what I am getting:
Here is a minimal working example.
\documentclass[a4paper]{amsart}
\usepackage{tikz}
\tikzset{graph/.style = {every node/.style = { draw,
shape = circle,
fill = black,
minimum size = 0.8mm,
inner sep = 0mm,
label distance = 0.8mm
}}}
\newcommand{\circumferencenode}[3]{\node (#1) at (#3: #2) [label = #3: $#1$] {};}
\newcommand{\polygon}[2]{
\pgfmathparse{subtract(#1, 1)}
\foreach \x in {0, ..., \pgfmathresult} {
\pgfmathparse{90 - 360 * \x / #1}
\circumferencenode{\x}{#2}{\pgfmathresult}
}
\pgfmathparse{subtract(#1, 1)}
\foreach \x in {0, ..., \pgfmathresult} {
\pgfmathparse{mod(\x + 1, #1)}
\draw (\x) -- (\pgfmathresult);
}
}
\begin{document}
\begin{tikzpicture}[graph]
\polygon{6}{10mm}
\end{tikzpicture}
\end{document}