# Shading 25 squares within a square using \foreach

In a square of edge length 5*sqrt(2), I have 45 congruent squares of edge length sqrt(2) drawn. I would like to shade 25 of them - 5 of them in each row. I shaded the 5 squares in the bottom row and started shading the other rows. How can I do this using \foreach?

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}

\begin{tikzpicture}

%A square with edge length 5*sqrt(2) is drawn. With ten line segments of slope 1 and
%ten line segments of slope -1, 45 squares with edge length sqrt(2), sixteen isosceles
%right triangles with legs of length sqrt(2), and four isosceles right triangles with
%legs of length sqrt(2)/2 are drawn. Twenty-five of the squares are shaded.
%(The figure is scaled by 1/4.)
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},0) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(2*sqrt(2)/2)}) -- (0,{(1/4)*sqrt(2)/2}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},0) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},0) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2))}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(7*sqrt(2)/2)},0) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(9*sqrt(2)/2)},0) -- ({(1/4)*(10*sqrt(2)/2)},{(1/4)*sqrt(2)/2}) -- ({(1/4)*(9*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*(sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(4*sqrt(2)/2)}) -- (0,{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(2*sqrt(2)/2)}) -- ({(1/4)*(8*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- ({(1/4)*(7*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(3*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(6*sqrt(2)/2)}) -- (0,{(1/4)*(5*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(4*sqrt(2)/2)}) -- ({(1/4)*(6*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(5*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(8*sqrt(2)/2)}) -- (0,{(1/4)*(7*sqrt(2)/2)}) -- cycle;
\path[fill=gray!50] ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(6*sqrt(2)/2)}) -- ({(1/4)*(4*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- ({(1/4)*(3*sqrt(2)/2)},{(1/4)*(8*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(7*sqrt(2)/2)}) -- cycle;
%
\path[fill=gray!50] ({(1/4)*sqrt(2)/2},{(1/4)*(8*sqrt(2)/2)}) -- ({(1/4)*(2*sqrt(2)/2)},{(1/4)*(9*sqrt(2)/2)}) -- ({(1/4)*sqrt(2)/2},{(1/4)*(10*sqrt(2)/2)}) -- (0,{(1/4)*(9*sqrt(2)/2)}) -- cycle;
%
%
\draw (0,0) -- ({(1/4)*(5*sqrt(2))},0) -- ({(1/4)*(5*sqrt(2))},{(1/4)*(5*sqrt(2))}) -- (0,{(1/4)*(5*sqrt(2))}) -- cycle;
%
%
\foreach \x in {1,2,3,4,5} {\draw (0,{(1/4)*(2*\x - 1)*(sqrt(2)/2)}) -- ({(1/4)*(5*sqrt(2) - (2*\x - 1)*(sqrt(2)/2))},{(1/4)*5*sqrt(2)});}
\foreach \x in {1,2,3,4,5} {\draw ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},0) -- ({(1/4)*5*sqrt(2)},{(1/4)*5*sqrt(2) - (1/4)*(2*\x - 1)*(sqrt(2)/2)});}
\foreach \x in {1,2,3,4,5} {\draw (0,{(1/4)*(2*\x - 1)*(sqrt(2)/2)}) -- ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},0);}
\foreach \x in {1,2,3,4,5} {\draw ({(1/4)*(2*\x - 1)*(sqrt(2)/2)},{(1/4)*5*sqrt(2)}) -- ({(1/4)*5*sqrt(2)},{(1/4)*(2*\x - 1)*(sqrt(2)/2)},0);}

\end{tikzpicture}

\end{document}

• Do you know \draw (a,b) rectangle (c,d);? It could be useful. Jun 2, 2018 at 21:31
• Is it necessary to use exactly those lengths? Or only the shape is important? You can rotate the squares also. Jun 2, 2018 at 21:33

Is this what you want?

\begin{tikzpicture}
\foreach \y in {0,2,...,8}{%
\foreach \x in {0,2,...,8}{%
\draw[fill=black!50,xshift=\x cm, yshift=\y cm]%
(0:1cm) -- (90:1cm) -- (180:1cm) -- (270:1cm) -- cycle;
}%
}%
\draw (-1,-1) rectangle (9,9);
\end{tikzpicture}


Edit: the same visual result but with counters in range 0,1,2,3,4 which easily is seen to have 5 values.

\foreach \y in {0,1,...,4}{%
\foreach \x in {0,1,...,4}{%
\draw[fill=black!50,xshift=\x cm, yshift=\y cm]
(0:.5cm) -- (90:.5cm) -- (180:.5cm) -- (270:.5cm) -- cycle;
}%
}%
\draw (-.5,-.5) rectangle (4.5,4.5);

• @Sebastiano, I think you can improve and use your sqrt length. I used polar coordinates. Jun 2, 2018 at 21:59
• This is some seriously concise code for my diagram! Jun 3, 2018 at 1:13
• Do you have to add units to xshift and yshift? Jun 3, 2018 at 1:13
• The diagram is too big. Can I have xshift={(1/4)*\x} and yshift={(1/4)*\y}? Jun 3, 2018 at 1:14
• You can use \begin{tikzpicture}[scale=.5] for example, just to change the size. But, if you really need some exact measure, some adjustment should be done. Jun 3, 2018 at 1:16

How can I resist to this golf challenge !

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach~in{0,...,24}
\fill[shift={({mod(~,5)+.5},{div(~,5)})},gray,rotate=45]rectangle(45:1);
\draw rectangle(5,5);
\end{tikzpicture}
\end{document}


EDIT: A second code that is 2 characters longer but use a single loop with only 5 iterates.

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
\begin{tikzpicture}
\fill[gray,even odd rule,rotate=45]
foreach~in{1,3,...,9}{(~-10,-~)rectangle(10-~,~)};
\draw(45:-10)rectangle(45:10);
\end{tikzpicture}
\end{document}

• LOL, 30 days will go by quickly Jun 3, 2018 at 6:42
• +.5 for brevity, +.5 for clever username! Jun 7, 2018 at 18:06
• @StevenB.Segletes you can remove .5 now ;)
– Kpym
Jun 20, 2018 at 11:53

Similar to @Sigur's answer, just using your length conventions (in case you want to add something in the same conventions).

\documentclass{amsart}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \Y in {0,...,4}
{\foreach \X in {0,...,4}
{\draw[fill=gray!50] ({(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*(2*sqrt(2)/2)+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*(2*sqrt(2)/2)+(1/4)*(2*sqrt(2)/2)*\Y}) --
({(1/4)*(2*sqrt(2)/2)*\X},{(1/4)*sqrt(2)/2+(1/4)*(2*sqrt(2)/2)*\Y}) -- cycle;}}
%
\draw (0,0) -- ({(1/4)*(5*sqrt(2))},0) -- ({(1/4)*(5*sqrt(2))},{(1/4)*(5*sqrt(2))}) -- (0,{(1/4)*(5*sqrt(2))}) -- cycle;
%
\end{tikzpicture}
\end{document}


• Very nice! I appreciate that you kept so much of my code. Jun 3, 2018 at 21:04
• I did not know that \x and \y could be used in the \draw options of xshift and yshift. So, I am going to select Sigur's response. Jun 3, 2018 at 21:05

with use fit tikz library and rotated nodes:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}[
box/.style = {draw, fill=#1,
inner sep=0pt, outer sep=0pt, minimum size=0.707106cm,
rotate=45}
]
\foreach \x in {0,...,4}{
\foreach \y in {0,...,4}
\node (g\x\y)   [box=gray!50]  at (\x,\y) {};
}
\node[draw,inner sep=0pt,
fit=(g00.north west) (g00.south west)
(g44.north east) (g44.south east)] {};
\end{tikzpicture}
\end{document}


• Nice! With nodes it is easy to insert text also. Jun 2, 2018 at 22:30

196 characters

\documentclass[border=9,tikz]{standalone}
\begin{document}
\tikz{
\clip[draw]rectangle(45:5);\def~{.5cm}\pgfsetdash{~~}{-~/2}
\draw[line width=~,gray,rotate=45](0,-2)grid(5,2);
}
\end{document}


• @TikzerWoods I also found that \pgfsetdash{~}{-~/2} = \pgfsetdash~{-~/2} works. But this is not documented. Jun 8, 2018 at 13:42

à la LL+graphicx way

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\setlength{\unitlength}{1cm}
\setlength{\fboxsep}{0cm}% for checking
\noindent\fbox{%
\begin{picture}(5,5)(0,0)
\color{gray!50}%
\multiput(0,0)(0,1){5}{%
\begin{picture}(5,1)(0,0)
\multiput(0,0)(1,0){5}
{\rotatebox{45}
{\rule{0.707106\unitlength}{0.707106\unitlength}}%
}%
\end{picture}%
}%
\end{picture}%
}
\end{document}


Update: with contours as per request in comment.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\setlength{\unitlength}{1cm}
\setlength{\fboxsep}{0cm}% for checking
\noindent\fbox{%
\begin{picture}(5,5)(0,0)
\multiput(0,0)(0,1){5}{%
\begin{picture}(5,1)(0,0)
\setlength{\fboxsep}{-\fboxrule}%
\multiput(0,0)(1,0){5}
{\rotatebox{45}
{\fbox{\color{gray!50}%
\rule{0.707106\unitlength}{0.707106\unitlength}}}%
}%
\end{picture}%
}%
\end{picture}%
}
\end{document}


Yet better with this replacement

  \setlength{\fboxsep}{-.5\fboxrule}%


• Interesting. But unfortunately, no contour in squares. :) Jun 7, 2018 at 19:34
• @Sigur updated :)
– user4686
Jun 7, 2018 at 20:38
• btw, English term is 'border' not 'contour'. :) Jun 7, 2018 at 20:49
• @jfbu, perfect!! And amazing. Jun 7, 2018 at 20:50
• @AlanMunn, I confess I was not comfortable with that word but it was the only one which came to my mind... lol Jun 7, 2018 at 20:51

Just for comparison, this is how to draw the figure using MetaPost. I use ConTeXt, but the code should also work with LaTeX (using the emp package in pdflatex and mplib package in lualatex).

\starttext
\startMPpage[offset=2mm]
n := 5;

path box;      box   := unitsquare rotated 45 scaled 1cm;
numeric shift; shift := xpart(lrcorner box - llcorner box);

for i = 0 upto (n-1) :
for j = 0 upto (n-1) :
fill box shifted (i*shift, j*shift) withcolor 0.8white;
draw box shifted (i*shift, j*shift) withcolor black;
endfor
endfor

draw unitsquare scaled (n*shift) xshifted -shift/2;

\stopMPpage
\stoptext


Why not a matrix of nodes with diamond nodes?

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{shapes.geometric, matrix}

\begin{document}
\begin{tikzpicture}
\matrix[draw, inner sep=0pt,
matrix of nodes, nodes in empty cells,
column sep=0pt, row sep=0pt,
nodes={diamond, draw, fill=gray!50, minimum size={1cm*sqrt(2)}}]
{&&&&\\&&&&\\&&&&\\&&&&\\&&&&\\};
\end{tikzpicture}
\end{document}