# Iterating over characters of a string during a for each loop in a Tikz picture

I am trying to make a command that renders a Tikz picture in which there is a square surrounded by a number of smaller squares on the boundary. The first parameter defines the size of the picture, the second defines the number of smaller surrounding squares and the third and fourth define the color of the squares.

For example, this command:

\gtile{0.8\textheight}{10}{white!60!orange}{white!60!red}


should give me this image: (excuse the thick lines caused by scaling the image in paint)

I've got this working but I would like to be able to choose which, if any, of the surrounding red squares to render by providing a binary number string as a parameter. For example, if I provided the binary number 1001101101, I would like the square to be rendered like this:

My command for rendering the first string is provided below. How should I modify my code in order to implement this behavior? I've tried using \ifthenelse along with \StrMid from the xparse package to test the characters in a string for equality with 1; however, Tikz does not seem to like this and throws a host of errors regarding extra \fi and missing control sequences which I assume means that the environments are not compatible.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfmath}

\newcommand{\gtile}[4]{
\begin{tikzpicture}[x=#1, y=#1]
\pgfmathsetmacro{\VA}{1 / (#2 + 2)}
\pgfmathsetmacro{\VB}{1 - \VA}

\filldraw[fill=#3] (\VA,\VA) -- (\VA,\VB) -- (\VB,\VB) -- (\VB,\VA) -- (\VA,\VA);

\foreach \x in {1,...,#2}
{
\filldraw[fill=#4] (\VA*\x,0) -- (\VA*\x,\VA) -- (\VA*\x+\VA,\VA) -- (\VA*\x+\VA,0) -- (\VA*\x,0);
\filldraw[fill=#4] (\VA*\x,\VB) -- (\VA*\x,1) -- (\VA*\x+\VA,1) -- (\VA*\x+\VA,\VB) -- (\VA*\x,\VB);
\filldraw[fill=#4] (0,\VA*\x) -- (0,\VA*\x+\VA) -- (\VA,\VA*\x+\VA) -- (\VA,\VA*\x) -- (0,\VA*\x);
\filldraw[fill=#4] (\VB,\VA*\x) -- (\VB,\VA*\x+\VA) -- (1,\VA*\x+\VA) -- (1,\VA*\x) -- (\VB,\VA*\x);
}
\end{tikzpicture}
}

\begin{document}
\gtile{0.8\textheight}{10}{white!60!orange}{white!60!red}
\end{document}

• Hi, welcome! Could you please add the preamble such that your document starts with \documentclass and ends with \end{document} and can be compiled? And would you be fine with having a comma-separated list like {1,0,0,1,1,0,1,1,0,1} instead of your single binary number? – user121799 Dec 4 '18 at 23:45
• I think a comma separated list would be fine – Daniel H Dec 4 '18 at 23:50
• Thanks! I guess you are looking for this answer, aren't you? – user121799 Dec 4 '18 at 23:52
• Perfect! This is exactly what I needed. I'll post an answer to this question using that. Thanks. – Daniel H Dec 5 '18 at 0:00

There is a package called bitset which does exactly this.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfmath}
\usepackage{bitset}
\usepackage{ifthen}

\newcommand{\gtile}[5]{
\begin{tikzpicture}[x=#1, y=#1]
\pgfmathsetmacro{\VA}{1 / (#2 + 2)}
\pgfmathsetmacro{\VB}{1 - \VA}

\filldraw[fill=#3] (\VA,\VA) -- (\VA,\VB) -- (\VB,\VB) -- (\VB,\VA) -- (\VA,\VA);

\bitsetSetBin{mybitset}{#5}

\foreach \x in {1,...,#2}
{
\ifthenelse{\bitsetGet{mybitset}{\x-1}=1}{
\filldraw[fill=#4] (\VA*\x,0) -- (\VA*\x,\VA) -- (\VA*\x+\VA,\VA) -- (\VA*\x+\VA,0) -- (\VA*\x,0);
\filldraw[fill=#4] (\VA*\x,\VB) -- (\VA*\x,1) -- (\VA*\x+\VA,1) -- (\VA*\x+\VA,\VB) -- (\VA*\x,\VB);
\filldraw[fill=#4] (0,\VA*\x) -- (0,\VA*\x+\VA) -- (\VA,\VA*\x+\VA) -- (\VA,\VA*\x) -- (0,\VA*\x);
\filldraw[fill=#4] (\VB,\VA*\x) -- (\VB,\VA*\x+\VA) -- (1,\VA*\x+\VA) -- (1,\VA*\x) -- (\VB,\VA*\x);
}{}
}
\end{tikzpicture}
}

\begin{document}
\gtile{0.8\textheight}{10}{white!60!orange}{white!60!red}{1001101101}
\end{document}