# Increasing thickness of borders in a square grid

Here is the code for a diagram that I have just generated:

\begin{tikzpicture}[fill=black]
\matrix[matrix of nodes, nodes={draw,minimum size=1cm}, nodes in empty cells,column sep=-\pgflinewidth,row sep=-\pgflinewidth](M){
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\

& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
};
\end{tikzpicture}

Basically this code helps me populate my 9 by 9 board with white and black squares. Now, decomposing this board into 9 3 by 3 squares, I wonder if there is a way to create distinctive thick borders around the boundaries of each 3 by 3 square in the block? That is, I want the boundaries of each 3 by 3 square to be a more solid shade of black compared to the other lines. (This is because I am trying to prove a result and doing the above would help me illustrate my point better).

Edit: Hmm so based on the responses that I have obtained, there are apparently a few ways to do this: one is to create a matrix list and then to individually specify whether you want each cell to be black or white. Indeed, this method is much more intuitive, and relatively feasible to carry out as long as your board is not too large. The second method involves a neat application of modular arithmetic, and though it takes a little bit of time to understand, its main merit is that you can easily populate the entire board with colours, which is advantageous when you have a larger board. At least, this is what I have managed to gather.

I did not know if you want to draw only one border or all, so I added codes for both situations. The basic point is that the nodes in the matrix have names, so you can use

\draw[blue,thick] (M-1-1.north west) rectangle (M-3-3.south east);

Full code:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes, nodes={draw,minimum size=1cm}, nodes in empty cells,column sep=-\pgflinewidth,row sep=-\pgflinewidth](M){
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\

& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
};
\draw[blue,thick] (M-1-1.north west) rectangle (M-3-3.south east);
\end{tikzpicture}

\begin{tikzpicture}
\matrix[matrix of nodes, nodes={draw,minimum size=1cm}, nodes in empty cells,column sep=-\pgflinewidth,row sep=-\pgflinewidth](M){
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\

& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
&          &   |[fill]|   &      &     &   |[fill]|   &          &    &   |[fill]|     \\
& |[fill]| &              &              &      |[fill]|     &              &     &      |[fill]|  &      \\
|[fill]| &         &         &        |[fill]|      &         &              &    |[fill]|      &       &      \\
};
\draw[blue,thick] foreach \X in {1,2,3}
{foreach \Y in {1,2,3}
{(M-\the\numexpr3*\Y-3+1\relax-\the\numexpr3*\X-3+1\relax.north west) rectangle
(M-\the\numexpr3*\Y-3+3\relax-\the\numexpr3*\X-3+3\relax.south east)}};
\end{tikzpicture}
\end{document}

• Excellent! The second one was what I needed, thank you so much! Apr 24, 2020 at 1:27

An other way with syntax x ? y : z (if x then y else z). The thickness is freely controled by [line width]. The option [shift={(1,1)}] is due to definition of the grid command.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\n{9}
\foreach \i in {1,...,\n}
\foreach \j in {1,...,\n}
{
\pgfmathparse{mod(\i+\n-\j,3) ? "white" : "black"}
\edef\colour{\pgfmathresult}
\fill[\colour] (\i,\j) rectangle +(1,1);
}
\draw (1,1) grid (\n+1,\n+1);
\draw[magenta,line width=1pt,shift={(1,1)}] (0,0) grid[step=3cm] (\n,\n);
\end{tikzpicture}
\end{document}
• Wow, this is nice! Indeed a more concise code. Apr 24, 2020 at 6:32
• Thanks for the reply! Apr 24, 2020 at 6:42
• @ONGSEEHAI you are welcome! Apr 24, 2020 at 11:04