# Tikz: draw a circle only in selected nodes

I created a table of nodes using tikz. And now I want to draw a circle inside every even node. I started from just trying to draw a circle in the second node. But my code is not working:

\begin{center}
\begin{tikzpicture}[->,shorten >=1pt,auto]
\hashcells{12}
\foreach [count=\i from 0] \number in {0, 0, 1, 0, 1, 0, 0}{
\node at (hashcell\i) {\number};
\if \i==2
\draw [fill=yellow] (0,0) circle (0.1cm) node (sun) {};
\fi
}
\end{tikzpicture}
\end{center}


Whereas the code below works fine and draws a circle in the first node. So I suppose there is something wrong with my if statement:

 \begin{center}
\begin{tikzpicture}[->,shorten >=1pt,auto]
\hashcells{12}
\foreach [count=\i from 0] \number in {0, 0, 1, 0, 1, 0, 0}{
\node at (hashcell\i) {\number};
\draw [fill=yellow] (0,0) circle (0.1cm) node (sun) {};
}
\end{tikzpicture}

\end{center}


If someone can suggest what I am doing wrong I will be grateful.

• Could you please provide a compilable MWE? – Nico Boni Sep 14 '14 at 14:18
• \ifnum\i=2? .. – cfr Sep 14 '14 at 14:23
• Try \ifodd\i\else...\fi or \ifnum\i=2...\fi. – Andrew Sep 14 '14 at 14:24
• Where is \hashcells from? It is not in the TiKZ manual. – cfr Sep 14 '14 at 15:27
• @Ziva The point is that you should post an example which people can compile. If the definition is not needed, that's fine - leave it out of the code you post in your question. – cfr Sep 14 '14 at 15:46

Previous note: Please, next time provide a COMPLETE minimal working (or not) example: starting from \documentclass... and finishing with \end{document} with all libraries and personal functions you use to test your code.

Answer: I'm not sure about what you want to do but if you just want to add some filled circles to previous defined nodes you can do it outside the foreach loop. Node names are known after foreach so you can make reference to them. There's no need for if inside the loop to add color to a particular node. If you don't know the particular name or want to calculate it forget my answer.

If you draw a filled circle at a node center you'll cover its contents, but you can use backgrounds library to add the filled circle behind the node. Both examples are shown in next code.

Final note: I've suposed that \hashcell was related to your previous question Tikz: define label font size under the node

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{backgrounds}
\tikzset{
cell/.style = {draw, minimum width=0.5cm, minimum height=0.5cm, minimum size = 0.5cm}
}

\begin{document}

\begin{tikzpicture}

\foreach [count=\i from 0] \j  in {1,...,7} {
\node[cell,label=below:{\scriptsize \i}] (cell\i) at (\i*0.5,0) {};}

\foreach [count=\i from 0] \number in {0, 0, 1, 0, 1, 0, 0}{
\node at (cell\i) {\number};
}

\draw [fill=yellow] (0,0) circle (0.2cm) node (sun) {};

\begin{scope}[on background layer]
\draw [fill=yellow] (cell2.center) circle (0.2cm) node (sun) {};
\end{scope}

\end{tikzpicture}
\end{document}


I think an easy approach is to use a matrix of nodes to draw the initial table. This automatically asigns a name to each cell, in the form matrix_name-Row-Column so it is easy to later add the circle via a loop.

\usetikzlibrary{matrix}
\begin{tikzpicture}
\tikzset{
cell/.style = {inner sep=2pt, minimum width=2em, minimum height=2em, draw}
}
\matrix[matrix of nodes, every node/.append style=cell] (M) {
0 & 0 & 1 & 0 & 1 & 0 & 0 \\
};

\foreach \cell in {2,4,6}
\draw[fill=yellow, fill opacity=0.4] (M-1-\cell) circle(.8em);
\end{tikzpicture}


Result:

Here another approach using the pic-environment (available since pgf 3.0). I got two code examples using even/odd or custom positions for the yellow circles. This way the elements are predefined and can easily be reused in the \foreach statement.

The code should be self-explanatory, in doubt just ask.

Info: The both examples just differ in the \foreach-loop and a \usepackage{xifthen}. Style definitions are all the same. I just put it in two separate examples for a better understanding.

Even/Odd-Approach

\documentclass[tikz, border=5mm]{standalone}

\begin{document}
\begin{tikzpicture}
\newcommand{\mysize}{.5cm}
\tikzset{
base/.style={minimum size=\mysize, draw, rectangle, label={below:{\scriptsize \x}}},
cell/.pic={ %
\node [base] at (0,0) {\lbl};
},
sun/.pic={ %
\draw [fill=yellow] (0,0) circle (.2cm);
\pic at (0,0) {cell};
},
}

\foreach \x\lbl [count=\i] in {1/,2/0,3/1,4/0,5/1,6/0,7/0} {
\newcommand{\mypos}{\x*\mysize, 0}
\ifodd\i % if you need even, just swap the pic-commands
\pic at (\mypos) {sun};
\else
\pic at (\mypos) {cell};
\fi
}
\end{tikzpicture}
\end{document}


Custom position approach (needs package xifthen)

\documentclass[tikz, border=5mm]{standalone}

\usepackage{xifthen}

\begin{document}
\begin{tikzpicture}
\newcommand{\mysize}{.5cm}
\tikzset{
base/.style={minimum size=\mysize, draw, rectangle, label={below:{\scriptsize \x}}},
cell/.pic={ %
\node [base] at (0,0) {\lbl};
},
sun/.pic={ %
\draw [fill=yellow] (0,0) circle (.2cm);
\pic at (0,0) {cell};
},
}

\foreach \x\lbl [count=\i] in {1/,2/0,3/1,4/0,5/1,6/0,7/0} {
\newcommand{\mypos}{\x*\mysize,0}
\ifthenelse { %
\i=1 \OR \i=3 \OR \i=6 % here go your x-positions
} { %
\pic at (\mypos) {sun};
} { %
\pic at (\mypos) {cell};
}
}
\end{tikzpicture}
\end{document}