# Draw a cyclical graph with nicely aligned edges using nodes in tikz [duplicate]

I would like to draw a cyclical graph in tikz using nodes and edges as hassle free and with as little code repetition as possible. However, the resulting edges do not align nicely with respect to the nodes (as seen below). How can I fix this, while still using nodes and edges, and not having to specify it for each node? I am looking for the simplest, shortest solution, so if that is not possible, I am open to alternatives without nodes and edges.

My code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}
\tikzmath{\n = 7; \r = 2.5;}
\begin{scope}[every node/.style={circle,thick,draw}]
\foreach \i in {1,...,\n}
{
\node (\i) at ({360/\n * (\i - 1)}: \r) {};
}
\end{scope}

\foreach \i in {2,...,\n}
{
\tikzmath{\im = \i-1;}
\path[-,thick,draw]
(\im) edge (\i);
}
\path[-,thick,draw] (\n) edge (1);
\end{tikzpicture}
\end{document}

The result:

EDIT: It irks me that people keep marking this question as a duplicate, as they seem to have missed the point. I wanted a solution that used NODES AND EDGES, not one that uses circles, not one that uses pstricks ... if there is a solution WITH NODES AND EDGES please feel free to mark this as duplicate and link to it. Otherwise, kindly read my question a bit more thoroughly. Also, thank you marmot for your helpful answer.

## marked as duplicate by Money Oriented Programmer, Phelype Oleinik, Raaja, Dox, user170109 May 28 at 15:02

• Use \tikzmath{\im = int(\i-1);} since otherwise you have numbers like 1.0 and so on, where .0 gets interpreted as the east anchor. – user121799 May 26 at 17:49
• \documentclass{standalone} \usepackage{tikz} \usetikzlibrary{math} \begin{document} \begin{tikzpicture} \tikzmath{\n = 7; \r = 2.5;} \begin{scope}[every node/.style={circle,thick,draw}] \foreach \i in {1,...,\n} { \node (\i) at ({360/\n * (\i - 1)}: \r) {}; } \end{scope} \foreach \i in {2,...,\n} { \tikzmath{\im = int(\i-1);} \path[-,thick,draw] (\im) edge (\i); } \path[-,thick,draw] (\n) edge (1); \end{tikzpicture} \end{document} – user121799 May 26 at 17:50
• It irks me that people keep marking this question as a duplicate, as they seem to have missed the point. I wanted a solution that used NODES AND EDGES, not one that uses circles, not one that uses pstricks ... if there is a solution WITH NODES AND EDGES please feel free to mark this as duplicate and link to it. – Emayla May 31 at 11:20
• I agree with you and voted to reopen the question. (I never voted to close it.) – user121799 May 31 at 14:57

Welcome to TeX-SE! This is a repeating question (at least at some level). The issue is that \tikzmath{\im = \i-1;} yields floating point numbers like 1.0, in which .0 gets interpreted as the east anchor. You can rectify this by using int.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}
\tikzmath{\n = 7; \r = 2.5;}
\begin{scope}[every node/.style={circle,thick,draw}]
\foreach \i in {1,...,\n}
{
\node (\i) at ({360/\n * (\i - 1)}: \r) {};
}
\end{scope}

\foreach \i in {2,...,\n}
{
\tikzmath{\im = int(\i-1);}
\path[-,thick,draw]
(\im) edge (\i);
}
\path[-,thick,draw] (\n) edge (1);
\end{tikzpicture}
\end{document}

There are also ways to simplify this. Assuming you want to keep \tikzmath you could introduce mod to obtain the same result without drawing a line separately.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}
\tikzmath{\n = 7; \r = 2.5;}
\begin{scope}[every node/.style={circle,thick,draw}]
\foreach \i in {1,...,\n}
{
\node (\i) at ({360/\n * (\i - 1)}: \r) {};
}
\end{scope}

\foreach \i in {1,...,\n}
{
\tikzmath{\im = int(mod(\i,\n)+1);}
\path[-,thick,draw]
(\im) edge (\i);
}
\end{tikzpicture}
\end{document}
• Yes, casting to int works! I know there have been questions in the sense of 'how to draw a cyclic graph' but I have not seen any solution with just plain old nodes and edges that didn't also have any arcs or anythings like that. I tried googling for it using a subset of the keywords cyclic, nodes, edges and the like, but came up empty, so I posted my question. I think it might help some other tikz newbie :) Thank you for your answer and the simplification, love it! – Emayla May 26 at 18:06

Just for fun: it's very simple with the pst-poly module of pstricks:

\documentclass[12pt, svgnames, border=4pt]{standalone}%
\usepackage{pst-poly}
\usepackage{auto-pst-pdf} %%% to be used to compile with pdflatex -shell-escape

\begin{document}

\begin{pspicture*}(-1.1,-1)(1.1,1.1)
\providecommand{\PstPolygonNode}{%
\psdots[dotstyle=o, dotsize=4pt, fillstyle=solid, fillcolor=LightSteelBlue!60](1;\INode)}
\rput(0,0){\PstHeptagon[PolyName=A, linecolor=SlateGray, linewidth=0.5pt]}
\end{pspicture*}

\end{document}

• If the question was on having a short code, TikZ would again win: \documentclass[tikz,border=3.14mm]{standalone} \usetikzlibrary{shapes.geometric} \begin{document} \begin{tikzpicture} \node[regular polygon,regular polygon sides=7,draw,inner sep=3cm,rotate=90/7] (poly){}; \foreach \X in {1,...,7} {\draw[fill=white] (poly.corner \X) circle[radius=1em];} \end{tikzpicture} \end{document}. My interpretation is that this was not the point of the question. – user121799 May 26 at 18:31
• That's why I specified it was for fun… – Bernard May 26 at 18:40
• Neat, haven't heard of pstricks before :) – Emayla May 28 at 10:11
\documentclass[pstricks,svgnames]{standalone}%
\usepackage{auto-pst-pdf} %%% to be used to compile with pdflatex -shell-escape
\begin{document}

\begin{pspicture*}(-1.1,-1.1)(1.1,1.1)
\degrees[7]
\pspolygon[dotstyle=o,dotsize=4pt,linecolor=SlateGray,linewidth=0.5pt,
showpoints](1;0)(1;1)(1;2)(1;3)(1;4)(1;5)(1;6)
\end{pspicture*}

\end{document}

It is a PSTricks solution only for fun purposes!

\documentclass[pstricks,margin=5mm]{standalone}
\usepackage{pst-node,pst-plot}
\def\N{7}
\degrees[\N]
\begin{document}
\begin{pspicture}[showpoints,dotscale=6](-4,-4)(4,4)
\curvepnodes[plotpoints=\numexpr\N+1]{0}{\N}{3 t PtoCrel}{A}
\psnpolygon[linecolor=red](0,\numexpr\Anodecount-1){A}
\end{pspicture}
\end{document}