8

I am trying to create figures of this kind enter image description here

Moreover, I would prefer to create a command with 9 arguments that specify whether each circle is colored in respective color or left white.

As an initial approximation, I thought of using the TIKZ graphs library for this purpose.

I copied this example from the manual:

\documentclass[11pt,a4paper,onecolumn,openright,final,oneside]{memoir}
%===============================================
\settrims{0.cm}{0.cm}
\setbinding{0cm} % space for binding
\setlrmarginsandblock{*}{3cm}{1} % spine = edge. Edge=3cm
\setulmarginsandblock{3cm}{*}{1} % upper=lower. Upper=3cm
\checkandfixthelayout
\usepackage{tikz}
\usetikzlibrary{graphs}
\begin{document}
\tikz
  \graph [nodes={draw, circle}, clockwise, radius=.75cm, empty nodes, n=8] {
    subgraph C_n [name=inner] <->[shorten <=1pt, shorten >=1pt]
    subgraph C_n [name=outer]
  };
\end{document}

I compiled this file with xelatex and this is what I got enter image description here

I was expecting something like this

enter image description here

Any help is appreciated!

11

I am not sure if I would use graph drawing for this. You seem to know very well how the result should look like, i.e. I do not see the need for a layout algorithm. A very simpleminded macro of the type you suggest is the following. (Fixed colors, modulos can be tricky. ;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\newcommand{\mymacro}[1]{
\begin{tikzpicture}
\pgfmathparse{\mycolors[1]}
\node[regular polygon,regular polygon sides=4,draw,minimum size=3cm,\pgfmathresult](square1){};
\pgfmathparse{\mycolors[0]}
\node[regular polygon,regular polygon sides=4,draw,minimum size=3cm,rotate=45,\pgfmathresult]
(square0){};
\foreach \X [count=\Y,evaluate=\Y as \Z using {int(mod(\Y+1,2))},
evaluate=\Y as \V using {int((\Y+1)/2)}] in {#1}
{\ifnum\Y=9
 \pgfmathsetmacro{\mycolor}{\mycolors[2]}
 \ifnum\X=1
  \filldraw[\mycolor] (0,0) circle (3mm);
 \else
  \draw[thick,fill=white,draw=\mycolor] (0,0) circle (3mm);
 \fi
\else
 \pgfmathsetmacro{\mycolor}{\mycolors[\Z]}
 \ifnum\X=1
  \filldraw[\mycolor] (square\Z.corner \V) circle (3mm);
 \else
  \draw[thick,fill=white,draw=\mycolor] (square\Z.corner \V) circle (3mm);
 \fi
\fi}
\end{tikzpicture}
}
\xdef\mycolors{{"black","red","cyan"}}
\mymacro{1,0,1,1,0,1,1,1,0}
\end{document}

enter image description here

The 0 and 1 entries determine if a given circle is filled, the 9th entry is the dot in the middle. The colors are stored in the \mycolors array.

  • 1
    This is an incredible answer: fast and precise! – yarchik Sep 11 '18 at 13:23
2

Here is a simple way to draw.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[thick]
\def\a{2}
\def\r{.5}
\fill[cyan] (0,0) circle(\r);

\begin{scope}[rotate=-135]
\draw[gray] (-\a,-\a) rectangle (\a,\a);
\filldraw (\a,-\a) circle(\r);
\filldraw (-\a,\a) circle(\r);
\filldraw (-\a,-\a) circle(\r);
\filldraw (\a,\a) circle(\r);
\end{scope}

\draw[red] (-\a,-\a) rectangle (\a,\a);
\filldraw[red] (\a,-\a) circle(\r);
\filldraw[red] (\a,\a) circle(\r);
\filldraw[red] (-\a,-\a) circle(\r);
\draw[red,fill=white] (-\a,\a) circle(\r);
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.