# Coloring of grids in tikz

Each block in each grid need to have a tone color from the (white - black) color scheme. Hence, there will be 25 different tones of gray in each grid.

The grid below the arrows will be an exact copy of the directly grid above the arrow.

(Am still new to tikz so any help will be higly appreciated) My code is as follow:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc,math}
\usepackage{graphicx}

\begin{document}
\begin{figure}

\begin{tikzpicture}[
GRID/.pic={\draw (0,0) grid[step=0.2] (1,1);}]
\path[use as bounding box,draw] (12,18) rectangle (0,0);

\foreach \x in {1,3,5,9}
\pic at (\x,16) {GRID};

\pic at (5,14) {GRID};

\node[yshift = 17.4cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 17.4cm,xshift = 3.5cm] () {Cost = 122};
\node[yshift = 17.4cm,xshift = 5.5cm] () {Cost = 111};
\node[yshift = 17.4cm,xshift = 9.5cm] () {Cost = 101};

\draw[->] (3.5,15.5) -- (3.5,13.5) node [midway,text width=3cm,xshift = -1.0cm] {Biased chosen template $P_1$};
\draw[->] (9.5,15.5) -- (9.5,13.5) node [midway,text width=3cm,xshift = 1.6cm] {Biased chosen template $P_2$};

\node[yshift = 14.5cm,xshift = 7.6cm] () {Binary template};

\foreach \x in {3,5,7,9}
\pic at (\x,12) {GRID}; %binary

\node[yshift = 11.5cm,xshift = 6.5cm] () {$P_1 \cdot T + (1-T) \cdot P_2$};

\draw[->] (6.5,11.0) -- (6.5,10.5) node [midway,xshift = 1cm] {Offspring};

\pic at (6,9)  {GRID};
\draw[->] (6.5,8.5) -- (6.5,7.5) node [midway,xshift = 2cm] {Exponential mutated};
\pic at (6,6)  {GRID};

\draw[-] (6.5,5.5) -- (6.5,5);
\draw[-] (3.5,5) -- (6.5,5);
\draw[->] (3.5 , 5) -- ( 3.5 , 4.5);

\foreach \x in {1,3,5,9}
\pic at (\x,3) {GRID};

\node[yshift = 2.5cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 2.5cm,xshift = 3.5cm] () {Cost = 141};
\node[yshift = 2.5cm,xshift = 5.5cm] () {Cost = 122};
\node[yshift = 2.5cm,xshift = 9.5cm] () {Cost = 111};
\end{tikzpicture}
\end{figure}
\end{document}
• I suggest using MSpaint to draw a .jpg or .png or whatever and include it. Since it is grid, resolution is not an issue. Jul 26, 2017 at 16:52

You can achieve this by adding lines to your pic, which are drawn in different tones of gray. Properly positioned, they will appear in each square. With two \foreach loops and some calculation, this is done in three lines.

The gray tones are set with black!<mix>. To change their range, the pic has three parameters. The <mix> is calculated as #1 + <x> * #2 + <y> * #3. The parameters have to be integer numbers. Care must be taken to keep the result in a range from 0 to 100. Parameters #2 and #3 can also be negative.

Every \pic at (...) {GRID}; has to be changed to \pic at (...) {GRID={<#1>/<#2>/<#3>}};.

Here the code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc,math}
\usepackage{graphicx}

\begin{document}
\begin{figure}
\tikzset{
pics/GRID/.style args={#1/#2/#3}{%
code={%
\foreach \gx in {0,1,2,3,4}{%
\foreach \gy in {0,1,2,3,4}{%
\pgfmathparse{#1+\gx*#2+\gy*#3}
\draw[black!\pgfmathresult,line width=2mm] ($(0.2*\gx,0.2*\gy+0.1)$) -- ($(0.2*\gx+0.2,0.2*\gy+0.1)$);
}
}
\draw (0,0) grid[step=0.2] (1,1);
}
}
}
\begin{tikzpicture}
\path[use as bounding box,draw] (12,18) rectangle (0,0);

\foreach \x in {1,3,5,9}
\pic at (\x,16) {GRID={0/4/20}};

\pic at (5,14) {GRID={20/10/-2}};

\node[yshift = 17.4cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 17.4cm,xshift = 3.5cm] () {Cost = 122};
\node[yshift = 17.4cm,xshift = 5.5cm] () {Cost = 111};
\node[yshift = 17.4cm,xshift = 9.5cm] () {Cost = 101};

\draw[->] (3.5,15.5) -- (3.5,13.5) node [midway,text width=3cm,xshift = -1.0cm] {Biased chosen template $P_1$};
\draw[->] (9.5,15.5) -- (9.5,13.5) node [midway,text width=3cm,xshift = 1.6cm] {Biased chosen template $P_2$};

\node[yshift = 14.5cm,xshift = 7.6cm] () {Binary template};

\foreach \x in {3,5,7,9}
\pic at (\x,12) {GRID={0/4/20}}; %binary

\node[yshift = 11.5cm,xshift = 6.5cm] () {$P_1 \cdot T + (1-T) \cdot P_2$};

\draw[->] (6.5,11.0) -- (6.5,10.5) node [midway,xshift = 1cm] {Offspring};

\pic at (6,9)  {GRID={0/20/4}};
\draw[->] (6.5,8.5) -- (6.5,7.5) node [midway,xshift = 2cm] {Exponential mutated};
\pic at (6,6)  {GRID={100/-4/-20}};

\draw[-] (6.5,5.5) -- (6.5,5);
\draw[-] (3.5,5) -- (6.5,5);
\draw[->] (3.5 , 5) -- ( 3.5 , 4.5);

\foreach \x in {1,3,5,9}
\pic at (\x,3) {GRID={10/2/10}};

\node[yshift = 2.5cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 2.5cm,xshift = 3.5cm] () {Cost = 141};
\node[yshift = 2.5cm,xshift = 5.5cm] () {Cost = 122};
\node[yshift = 2.5cm,xshift = 9.5cm] () {Cost = 111};
\end{tikzpicture}
\end{figure}
\end{document}

And this is how it looks:

Edit: having random patterns and yet controll requires some coding.

The new code has some commads to define lists for the gray tones, either manually or randomly. With \DefineMixList{<listname>}{<numbers>} you have full control, but also have to enter all 25 values, seperated by ,. With \DefineRandomMixList the values are randomly defined. These lists can be reused as often as you like. They are also written to the log file, so they can be copied and reused later.

Be aware: the random lists will change with every compilation of the document.

Here every \pic at (...) {GRID}; has to be changed to \pic at (...) {GRID={<listname>}};. And in order to have control, you can't use the grids in a \foreach loop anymore.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc,math}
\usepackage{graphicx}

\makeatletter
% #1: name
% #2: comma seperated list of mix values
\newcommand*{\DefineMixList}[2]{%
\expandafter\def\csname @ml@#1\endcsname{#2}%
\typeout{new mix list '#1': \csname @ml@#1\endcsname}%
}

% #1: name
\newcommand*{\DefineRandomMixList}[1]{%
\pgfmathrandominteger{\@ml@entry}{0}{100}%
\expandafter\xdef\csname @ml@#1\endcsname{\@ml@entry}%
\foreach \@ml@ in {1,2,...,24}{%
\pgfmathrandominteger{\@ml@entry}{0}{100}%
\expandafter\xdef\csname @ml@#1\endcsname{\csname @ml@#1\endcsname,\@ml@entry}%
}
\typeout{new random mix list '#1': \csname @ml@#1\endcsname}%
}

% #1: name
\newcommand*{\InitMixFromList}[1]{%
\@ifundefined{@ml@#1}{%
\GenericError{%
(Mix list)\@spaces\@spaces\@spaces\@spaces
}{%
Mix list Error: undefined list #1
}{%
the list you requested wasn't defined
}{%
All squares in the grid affected by this will have the\MessageBreak
default gray tone.
}
}{%
% define a temporary list
\edef\@ml@UsedList{\csname @ml@#1\endcsname}%
}%
}

\newcommand*{\GetNextMixFromList}{%
\expandafter\@ml@next\@ml@UsedList,\@ml@end
}

\newcommand*{\@ml@next}{}
\def\@ml@next#1,#2\@ml@end{%
\def\@ml@temp{#1}\ifx\@ml@temp\@empty
\def\CurrentMix{50}% default, if there are not enough numbers
\else
\def\CurrentMix{#1}%
\fi
\xdef\@ml@UsedList{#2}% removes just used element from tempoary list
}

\newcommand*{\CurrentMix}{}
\makeatother

% defining the list
\DefineRandomMixList{cost154}
\DefineRandomMixList{cost122}
\DefineRandomMixList{cost111}
\DefineRandomMixList{cost101}
\DefineRandomMixList{cost141}
\DefineRandomMixList{cost154}
\DefineRandomMixList{cost222}
\DefineRandomMixList{cost333}
\DefineRandomMixList{cost444}
\DefineMixList{cost555}{12,57,43,68,75,91,3,36,47,58,64,33,78,82,29,44,50,63,77,46,21,95,21,8,10}

\begin{document}
\begin{figure}
\tikzset{
pics/GRID/.style={%
code={%
\InitMixFromList{#1}%
\foreach \gx in {0,1,2,3,4}{%
\foreach \gy in {0,1,2,3,4}{%
\GetNextMixFromList
\draw[black!\CurrentMix,line width=2mm] ($(0.2*\gx,0.2*\gy+0.1)$) -- ($(0.2*\gx+0.2,0.2*\gy+0.1)$);
}
}
\draw (0,0) grid[step=0.2] (1,1);
}
}
}
\begin{tikzpicture}
\path[use as bounding box,draw] (12,18) rectangle (0,0);

\pic at (1,16) {GRID={cost154}};
\pic at (3,16) {GRID={cost122}};
\pic at (5,16) {GRID={cost111}};
\pic at (9,16) {GRID={cost101}};

\pic at (5,14) {GRID={cost222}};

\node[yshift = 17.4cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 17.4cm,xshift = 3.5cm] () {Cost = 122};
\node[yshift = 17.4cm,xshift = 5.5cm] () {Cost = 111};
\node[yshift = 17.4cm,xshift = 9.5cm] () {Cost = 101};

\draw[->] (3.5,15.5) -- (3.5,13.5) node [midway,text width=3cm,xshift = -1.0cm] {Biased chosen template $P_1$};
\draw[->] (9.5,15.5) -- (9.5,13.5) node [midway,text width=3cm,xshift = 1.6cm] {Biased chosen template $P_2$};

\node[yshift = 14.5cm,xshift = 7.6cm] () {Binary template};

\pic at (3,12) {GRID={cost122}}; %binary
\pic at (5,12) {GRID={cost333}}; %binary
\pic at (7,12) {GRID={cost444}}; %binary
\pic at (9,12) {GRID={cost101}}; %binary

\node[yshift = 11.5cm,xshift = 6.5cm] () {$P_1 \cdot T + (1-T) \cdot P_2$};

\draw[->] (6.5,11.0) -- (6.5,10.5) node [midway,xshift = 1cm] {Offspring};

\pic at (6,9)  {GRID={cost555}};
\draw[->] (6.5,8.5) -- (6.5,7.5) node [midway,xshift = 2cm] {Exponential mutated};
\pic at (6,6)  {GRID={cost555}};

\draw[-] (6.5,5.5) -- (6.5,5);
\draw[-] (3.5,5) -- (6.5,5);
\draw[->] (3.5 , 5) -- ( 3.5 , 4.5);

\pic at (1,3) {GRID={cost154}};
\pic at (3,3) {GRID={cost141}};
\pic at (5,3) {GRID={cost122}};
\pic at (9,3) {GRID={cost111}};

\node[yshift = 2.5cm,xshift = 1.5cm] () {Cost = 154};
\node[yshift = 2.5cm,xshift = 3.5cm] () {Cost = 141};
\node[yshift = 2.5cm,xshift = 5.5cm] () {Cost = 122};
\node[yshift = 2.5cm,xshift = 9.5cm] () {Cost = 111};
\end{tikzpicture}
\end{figure}
\end{document}

And now it looks like this:

• It possible to have the different types of gray to be more random placed in the different grids? However, I still need to control on how the colors is placed. since e.g. the two grids to the far left needs to be identical, the same is true for the two grids with the cost 122 and cost 111. (top and buttom grids) The two grids between the exponetial muation is almost also identical, only 5 blocks must be different. Hope that you can help :) Jul 27, 2017 at 11:17
• @JesperH: see edit.
– Mike
Jul 27, 2017 at 22:28