# Constructing a figure showing a ring of connected nodes using TikZ

I want to reproduce this figure (see below) with 7 nodes and 8 nodes, but since I've never constructed a figure in LaTeX so I'm clueless.

• Hey, and welcome to tex.stackexchange. Could you post a screenshot instead of the link? It is not so clear what you're looking for, as the link points to google books
– sheß
Feb 26 '19 at 16:08
• Please insert the figure here. We don't follow external links. For example, I got this after following your link: "You have either reached a page that is unavailable for viewing or ...".
– user156344
Feb 26 '19 at 16:08
• ok, no problem.
– Math
Feb 26 '19 at 16:09
• Welcome to TeX - LaTeX! Usually you should show us what coding you have tried so far. The documentation of tikz / pgf contains several examples close to your problem. So I would suggest you try to follow those, and then ask us again when you run into problems. Feb 26 '19 at 16:18
• One purpose of stackexchange is to create a knowledge data base that will be useful for others. This is why it is always appreciated if your question is posed in a way that will render it easy to find for people with similar issues and easy to answer. I'm suggesting and edit for your question that will make sure this is fulfilled. For your next question you can try and pose more clear question yourself.
– sheß
Feb 26 '19 at 16:53

This is one out of a million ways to do this. You can use polar coordinates. This is the figure you provided, your task now is to reduce the number of edges in each polygon, i.e. do some changes in \foreach loop. This only requires very simple maths, right?

\documentclass[tikz,margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \i in {90,54,...,-234} {
\draw (\i:2)--({\i-36}:2);
}
\foreach \i in {90,18,...,-198} {
\draw[fill=black] (\i:2) circle (1mm);
}
\foreach \i in {54,-18,...,-234} {
\draw[fill=white] (\i:2) circle (1mm);
}
\begin{scope}[xshift=5cm]
\foreach \i in {90,50,...,-230} {
\draw (\i:2)--({\i-40}:2);
\draw[fill=black] (\i:2) circle (1mm);
}
\end{scope}
\end{tikzpicture}
\end{document}


### Edit 1:

Is this what you want?

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\foreach \i in {90,54,...,-234} {
\draw (\i:2)--({\i-36}:2);
}
\foreach \i in {90,18,...,-198} {
\draw[fill=black] (\i:2) circle (1mm);
}
\foreach \i in {54,-18,...,-234} {
\draw[fill=white] (\i:2) circle (1mm);
}
\begin{scope}[xshift=5cm]
\foreach \i in {90,50,...,-230} {
\draw (\i:2)--({\i-40}:2);
\draw[fill=black] (\i:2) circle (1mm);
}
\end{scope}
\end{tikzpicture}
\caption{Some caption}
\label{my:figure}
\end{figure}
\lipsum[2]
\end{document}


• I like this variant of the many answers provided. as I want this to be a figure, I want it to be centralised, how can I do this?
– Math
Feb 27 '19 at 12:29
• I haven't tried this, but I put [ and ] before \begin{tikzpicture} and after \end{tikzpicture} and it worked but let me try what you suggested
– Math
Feb 27 '19 at 12:33
• this completely is the opposite what I wanted! but thanks for suggesting. if you see what I did you will understand
– Math
Feb 27 '19 at 12:34
• @Math Ok, you should add what you want to the question, and I will have a look at it!
– user156344
Feb 27 '19 at 12:35
• Mhm, but this not what I want. what I want is the image to be central
– Math
Feb 27 '19 at 12:35
\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture} %draw 8 nodes with alternating colors
\foreach[count=\i] \fillc in {black,white,black,white,black,white,black,white} {
\draw[shorten <=2mm,shorten >=2mm] (\i/8*360:2)-- ({(\i-1)/8*360}:2);
\node[minimum size=4mm,inner sep=0,circle,draw,fill=\fillc] (point\i) at ({\i/8*360}:2) {};
}
\end{tikzpicture}

\begin{tikzpicture} %draw 7 nodes, all black
\foreach[count=\i] \fillc in {black,black,black,black,black,black,black} {
\draw[shorten <=2mm,shorten >=2mm] (\i/7*360:2)-- ({(\i-1)/7*360}:2);
\node[minimum size=4mm,inner sep=0,circle,draw,fill=\fillc] (point\i) at ({\i/7*360}:2) {};
}
\end{tikzpicture}

\end{document}


• +1 Maybe a simple \ifodd is a more straightforward way to achieve the alternation. Of course, your way is more general in that you can have arbitrary patterns.
– user121799
Feb 26 '19 at 19:00

Using TikZ library shapes.

\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\tikzset{whitecirc/.style={fill=white,draw=black,thick},
blackcirc/.style={draw=black,thick}
}
\begin{document}

\begin{tikzpicture}
\def\ptsize{1.5pt}
\node[draw,regular polygon,minimum height=3cm,minimum width=3cm,regular polygon sides=7](P) {};
\foreach \t in {1,3,5,7}{
\fill[whitecirc] (P.corner \t) circle (\ptsize);}
\foreach \t in {2,4,6}{
\fill[blackcirc] (P.corner \t) circle (\ptsize);}

\begin{scope}[xshift=4cm]
\node[draw,regular polygon,minimum height=3cm,minimum width=3cm,regular polygon sides=8](P) {};
\foreach \t in {1,2,...,8}{
\fill[blackcirc] (P.corner \t) circle (\ptsize);}
\end{scope}
\end{tikzpicture}

\end{document}


• @marmot, thank you for (+1). Your suggestion is cool.
– user31034
Feb 26 '19 at 20:51