# Is there any efficient way to add cell identifier to each cell in TikZ?

I made a grid with TikZ. I would like to add the cell number in each cell (in the middle of each cell), I used this command

\draw (-4,2.5) node[below] {$Z_1$};


But this solution is time consuming. Is there any efficient way to add cell identifier to each cell?

\documentclass[tikz]{standalone}
\usepackage{graphicx} % for example images

\begin{document}
\begin{tikzpicture}[every label/.style={fill=white, inner sep=1mm}]

% styles
\tikzstyle{myLabel}=[draw=black, circle, fill=white]
\tikzstyle{myLine}=[draw=blue,  double]

\draw (0,0) node[inner sep=0] {\includegraphics[width=10cm]{example-image-a}};
\draw[step=1.2cm,blue,very thin] (-5,-2.5) grid (5,2.4);
\draw (-4,2.5) node[below] {$Z_1$};

\end{tikzpicture}
\end{document}


This what I want to draw:

For example:

\documentclass[tikz]{standalone} % tikz loads graphicx

\begin{document}
\begin{tikzpicture}[
declare function={boxW=1.2cm;},
box/.style={minimum size=boxW,draw},
]

\foreach \x in {0,...,15}
{
\ifnum\x=0
\node [box,fill=blue!20] at ({mod(\x,4)*boxW}, {-ceil((\x+1)/4)*boxW}) {$C_{\x}$};
\else
\node [box] at ({mod(\x,4)*boxW}, {-ceil((\x+1)/4)*boxW}) {$C_{\x}$};
\fi
}

\end{tikzpicture}
\end{document}


One way of placing this grid on/relative to an image:

\documentclass[border=5mm, tikz]{standalone} % tikz loads graphicx
\begin{document}
\begin{tikzpicture}[
declare function={boxW=1.2cm;},
box/.style={minimum size=boxW,draw},
boxfill/.style={}
]

\node [inner sep=0] (img) {\includegraphics{example-image}};

% define a coordinate where the upper left corner of the grid starts
\coordinate (gridstart) at (img.north west);

\foreach \x in {0,...,15}
{
\ifnum \x=0
\tikzset{boxfill/.style={fill=blue!20}}
\else
\tikzset{boxfill/.style={}}
\fi

\path (gridstart) ++ (0.5*boxW,0.5*boxW) ++ ({mod(\x,4)*boxW}, {-ceil((\x+1)/4)*boxW}) node [box,boxfill] {$C_{\x}$};
}

\end{tikzpicture}
\end{document}


This proposal combines two answers by @percusse:

1. This nice answer allows you to set up a matrix with \foreach loops.
2. This nice answer allows you to fill entries specified in a list.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{matrix}
\begin{document}
\newcommand{\setupmatrix}[3]{%
\let\mymatrixcontent\empty
\foreach \j in {1,...,#2}{
\foreach \i [evaluate=\i as \k using {int(#3*(\j-1)+\i-1)}]
in {1,...,#3} {%
\begingroup\edef\x{\endgroup
\noexpand\gappto\noexpand\mymatrixcontent{
\noexpand#1_{\k} \&}}\x
}%
\gappto\mymatrixcontent{\\}%
}
} % \setupmatrix{<entry>}{<number of rows>}{<number of columns>}
\setupmatrix{Z}{4}{8}
\tikzset{myfill/.style args={#1|#2}{row #2 column #1/.style={nodes={fill=blue!30}}}}
\begin{tikzpicture}[every label/.style={fill=white, inner sep=1mm}]

% styles
\draw (0,0) node[inner sep=0] {\includegraphics[width=10cm]{example-image-a}};
\draw[step=1.2cm,blue,very thin] (-5,-2.5) grid (5,2.4);
\matrix (a) [ampersand replacement=\&,matrix of math nodes,
nodes={minimum width=1.2cm,minimum height=1.2cm},
myfill/.list={1|1,3|2,6|3}]{
\mymatrixcontent
};

\end{tikzpicture}
\end{document}


• Why there is some cells that have different colors? – Heba Mohsen Feb 13 '19 at 0:44
• @HebaMohsen I thought that you wanted this because in your screen shot the top left cell is shaded. So I added a way to shade arbitrary cells by just specifying their positions. If you only want to shade the top left cell, say myfill={1|1} (or myfill/.list={1|1} which is the same in this case). – user121799 Feb 13 '19 at 0:47
• I tried to change the text inside each cell for example \texbf{Z}. I got error – Heba Mohsen Feb 13 '19 at 0:50
• @HebaMohsen \texbf is not a usual command, if you mean \textbf: You could do \setupmatrix{\noexpand\textbf{Z}}{4}{8}, which works. If you want me to, I will be happy to modify the command in such a way that you do not need the \noexpand. (I did that right now, with the new version \setupmatrix{\textbf{Z}}{4}{8} works. – user121799 Feb 13 '19 at 0:54

Since your MWE includes an image, with a grid that is superimposed on it, I considered that you wanted to draw the grid over this image.

This is what I get, is that what you want?

\documentclass[tikz]{standalone}
\usepackage{graphicx} % for example images

\begin{document}
\begin{tikzpicture}[every label/.style={fill=white, inner sep=1mm}]

% styles
\tikzstyle{myLabel}=[draw=black, circle, fill=white]
\tikzstyle{myLine}=[draw=blue,  double]

\draw (0,0) node[inner sep=0] {\includegraphics[width=10cm]{example-image-a}};
\draw[xstep=2.5cm,ystep=1.2,blue,very thin] (-5,-2.4) grid (5,2.4);
\foreach \x [evaluate=\x as \xx using \x*2.5-3.75]in {0,...,3}{
\foreach \y [evaluate =\y as \label using int(\x-4*\y),
evaluate=\y as \yy using \y*1.2+1.8]in {0,-1,...,-3}{

\draw (\xx,\yy) node[below] {$C_{\label}$};
} }

\end{tikzpicture}
\end{document}


You could use a \matrix with a convenient style to do the job for you.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\newcounter{countcells}

\tikzset{
resetcounter/.code={\setcounter{countcells}{0}},
mycell/.style={rectangle,
draw,
outer sep=0pt,
minimum size = 1cm,
inner sep=0pt},
mygrid/.style={matrix of nodes,
nodes in empty cells,
nodes={mycell, anchor=center,
node contents = $C_{\thecountcells}$\stepcounter{countcells}},
row sep = -\pgflinewidth,
column sep= -\pgflinewidth,
resetcounter},
}
\begin{document}

\begin{tikzpicture}
\matrix[mygrid] (C) {
&&&\\
&&&\\
&&&\\
&&&\\};

\matrix[mygrid, right= of C.east] {
&&&&\\
&&&&\\
&&&&\\};
\end{tikzpicture}
\end{document}


Update: The matrix over a figure:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}
\newcounter{countcells}

\tikzset{
resetcounter/.code={\setcounter{countcells}{0}},
mycell/.style={rectangle,
draw,
outer sep=0pt,
minimum size = 1cm,
inner sep=0pt},
mygrid/.style={matrix of nodes,
inner sep=0pt,
outer sep=0pt,
nodes in empty cells,
nodes={mycell, anchor=center,
node contents = $C_{\thecountcells}$\stepcounter{countcells}},
row sep = -\pgflinewidth,
column sep= -\pgflinewidth,
resetcounter},
}
\begin{document}

\begin{tikzpicture}

\node[outer sep=0pt, inner sep=0pt] (A) {\includegraphics{example-image-a}};

\matrix[mygrid, below right=0pt of A.north west] (C) {
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
&&&&&&&&&&&\\
};
\end{tikzpicture}
\end{document}


• But in my question, I have a picture that I have a picture that I need to draw that matrix over it. – Heba Mohsen Feb 13 '19 at 21:17
• @HebaMohsen It's not a problem to place the matrix over the included figure. Just select the correct anchor and placement option. See the updated answer. – Ignasi Feb 14 '19 at 8:20