# How to draw two grids with inner grids with Tikz or any other package

I am trying to draw two grids side by side, each of size 8 by 8. Also these grids have one smaller grid 6 by 6 inside them. I want to do as nicely as possible, with clear border colors. I need major grids to be of light gray color and smaller inner grids to be filled with white color. White color should overlap. Also I need to be able to name cells...

This is what I got so far...

\begin{tikzpicture}[thick,scale=0.7, every node/.style={scale=0.6}]

\path (0,0); % <--- THIS LINE IS ADDED
\begin{scope}[shift={(0mm,0mm)}]
\foreach \xoffset in {0,8}
{
\begin{scope}[shift={(\xoffset,0)}]
\draw[xstep=1,ystep=1] (0,0) grid (8,8);
\end{scope}
}
\end{scope}

\path (0,0); % <--- THIS LINE IS ADDED
\begin{scope}[shift={(10mm,10mm)}]
\foreach \xoffset in {0,8}
{
\begin{scope}[shift={(\xoffset,0)}]
\draw[xstep=1,ystep=1, white] (0,0) grid (6,6);
\end{scope}
}
\end{scope}

\end{tikzpicture}


Cannot figure out how to fill cells with color (inner square must be white, outside square light gray, inner square color should overlap), also not sure how to add labels..

This is what I currently have

Thanks for help!

• Welcome to TeX.SX!! Rather than posting code fragments it is better to post a full minimal working example. This makes it much easier for people to help you --- and much more likely that they will! It would be helpful if you also said in more detail what you mean by "naming" cells. – Andrew May 28 '16 at 3:45

I am not sure what you mean by the "inner square color should overlap", but perhaps you just mean that there shouldn't be a black line between the inner and outside squares. If so, you probably want something like the following:

This was produced by:

\documentclass[border=5mm,tikz]{standalone}
\usepackage{mwe}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\draw[fill=gray!30!white](0,0) rectangle (16,1);
\draw[fill=gray!30!white](0,7) rectangle (16,8);
\draw[fill=gray!30!white](0,0) rectangle (1,8);
\draw[fill=gray!30!white](7,0) rectangle (9,8);
\draw[fill=gray!30!white](15,0) rectangle (16,8);
\foreach \y in {0,,8} {
\draw[very thick](0,\y)--++(16,0);
}
\foreach \x in {0,8,16} {
\draw[very thick](\x,0)--++(0,8);
}
\foreach \x in {1,2,...,15} {
\draw[very thick](\x,0)--++(0,1);
\draw[very thick](\x,7)--++(0,1);
}
\foreach \y in {1,2,...,7} {
\draw[very thick] (0,\y)--++(1,0);
\draw[very thick] (7,\y)--++(2,0);
\draw[very thick](15,\y)--++(1,0);
}
% internal grid
\foreach \x [evaluate=\x as \xx using \x+8] in {2,3,...,6} {
\draw[thick, gray](\x,1)--++(0,6);
\draw[thick, gray](1,\x)--++(6,0);
\draw[thick, gray](\xx,1)--++(0,6);
\draw[thick, gray](9,\x)--++(6,0);
}
% writing labels in the positions
\foreach \x in {1,2,...,16} {
\foreach \y in {1,2, ..., 8} {
\node at (\x-0.5,\y-0.5){\x,\y};
}
}
\end{tikzpicture}

\end{document}


Here's something with an "other package", namely Metapost, which you can use either on its own to produce an external graphic, or integrated with LaTeX via the gmp or luamplib packages.

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

% some spacing parameters
numeric xgap, ygap, zgap, cell_x, cell_y;
xgap = ygap = 0;
zgap = 13;
cell_x = 21; cell_y = 21;

% first define an array of points in the right places
pair p[];
numeric n;
n = 0;
for z=0 upto 1:
for y=0 upto 7:
for x=0 upto 7:
p[incr n] = (x*(cell_x+xgap) + z*(zgap+8*(cell_x+xgap)), y*(cell_y+ygap));
endfor
endfor
endfor

% now draw/fill each cell - note the order to get the desired grid lines
path box;
box = unitsquare shifted -(1/2,1/2) xscaled cell_x yscaled cell_y;
for x=0,7,1,2,3,4,5,6:
for y=0,7,1,2,3,4,5,6:
for z=0,1:
n := 1+x+8y+64z;
if (x=0) or (x=7) or (y=0) or (y=7):
fill box shifted p[n] withcolor .8 white;
draw box shifted p[n] withpen pencircle scaled 1;
else:
fill box shifted p[n] withcolor .98 white;
undraw box shifted p[n] withpen pencircle scaled 1;
fi
endfor
endfor
endfor

% label the cells
defaultfont := "phvr8r";
label("A", p[1]);

% you could continue one-at-a-time,
% or you could use a loop
for i=2 upto 128:
label(decimal i, p[i]);
endfor

endfig;
end.