Drawing Grid Matrices with TikZ: A Labelled Node Example

This example was part of a personal project during my undergraduate studies in 2011. I wanted to share it with you as it might be of interest.

Introduction

Creating grid-based diagrams is a common need in mathematical documents, presentations, and educational materials. Whether you're illustrating matrix operations, graph theory concepts, or algorithm visualizations, TikZ provides powerful tools for creating clean, professional-looking grid matrices with labelled nodes.

In this post, I'll walk you through creating a sophisticated 4×4 grid with nested rectangular frames and highlighted connections between specific nodes. This example demonstrates several key TikZ techniques that you can adapt for your projects.

The Complete Code

Here's the full TikZ code we'll be analysing:

Certainly! Here's the formatted TikZ code for better readability:

\begin{center}
\begin{tikzpicture}[-, thick]
    \draw (0,0) rectangle (2,2);
    \draw (0,0) rectangle (0.5, 0.5);
    \draw (0,0) rectangle (1,1);
    \draw (0,0) rectangle (1.5,1.5);
    \draw (2,2) rectangle (1.5,1.5);
    \draw (2,2) rectangle (1,1);
    \draw (2,2) rectangle (0.5,0.5);
    \tikzstyle{b1} = [circle, fill = black, text = white, draw = none, 
                      text centered, minimum height = 0.75cm, 
                      node distance = 4cm, scale = 0.4]
    \node[b1, fill = blue] (A) [inner sep = 2pt] at (0.25, 1.75) {\textbf{a}};
    \node[b1] (B) [inner sep = 2pt] at (0.75, 1.75) {\textbf{b}};
    \node[b1] (C) [inner sep = 2pt] at (1.25, 1.75) {\textbf{c}};
    \node[b1] (D) [inner sep = 2pt] at (1.75, 1.75) {\textbf{d}};
    \node[b1] (E) [inner sep = 2pt] at (0.25, 1.25) {\textbf{e}};
    \node[b1] (F) [inner sep = 2pt] at (0.75, 1.25) {\textbf{f}};
    \node[b1, fill = blue] (G) [inner sep = 2pt] at (1.25, 1.25) {\textbf{g}};
    \node[b1] (H) [inner sep = 2pt] at (1.75, 1.25) {\textbf{h}};
    \node[b1] (I) [inner sep = 2pt] at (0.25, 0.75) {\textbf{i}};
    \node[b1, fill = blue] (J) [inner sep = 2pt] at (0.75, 0.75) {\textbf{j}};
    \node[b1] (K) [inner sep = 2pt] at (1.25, 0.75) {\textbf{k}};
    \node[b1] (L) [inner sep = 2pt] at (1.75, 0.75) {\textbf{l}};
    \node[b1] (M) [inner sep = 2pt] at (0.25, 0.25) {\textbf{m}};
    \node[b1] (N) [inner sep = 2pt] at (0.75, 0.25) {\textbf{n}};
    \node[b1] (O) [inner sep = 2pt] at (1.25, 0.25) {\textbf{o}};
    \node[b1, fill = blue] (P) [inner sep = 2pt] at (1.75, 0.25) {\textbf{p}};
    \draw[very thick, blue] (A) -- (G);
    \draw[very thick, blue] (A) -- (J);
    \draw[very thick, blue] (G) -- (P);
    \draw[very thick, blue] (J) -- (P);
\end{tikzpicture}
\end{center}

Breaking Down the Code

1. Setting Up the TikZ Environment

\begin{tikzpicture}[-, thick]

The [-] option removes arrowheads from lines by default, and thick sets the default line width for all drawing elements.

2. Creating the Nested Rectangle Framework

\draw (0,0) rectangle (2,2);
\draw (0,0) rectangle (0.5, 0.5);
\draw (0,0) rectangle (1,1);
\draw (0,0) rectangle (1.5,1.5);
\draw (2,2) rectangle (1.5,1.5);
\draw (2,2) rectangle (1,1);
\draw (2,2) rectangle (0.5,0.5);

This creates an interesting nested pattern:

  • The main rectangle spans from (0,0) to (2,2)

  • Smaller rectangles emanate from both the bottom-left corner (0,0) and top-right corner (2,2)

  • This creates concentric rectangular frames that add visual depth to the diagram

3. Defining Node Styles

\tikzstyle{b1} = [circle, fill = black, text = white, draw = none, text centered, minimum height = 0.75cm, node distance = 4cm, scale =0.4]

The b1 style creates our node appearance:

  • circle: Makes nodes circular

  • fill = black, text = white: Black background with white text

  • draw = none: Removes node borders

  • minimum height = 0.75cm: Sets consistent node size

  • scale = 0.4: Scales down the overall node size

4. Placing the Grid Nodes

The nodes are positioned in a regular 4×4 grid with 0.5-unit spacing. Each node has:

  • A unique identifier (A through P)

  • Precise coordinates (using the pattern: row and column × 0.5 + 0.25)

  • Bold text labels (a through p)

Key positioning pattern:

  • Row 1 (top): y = 1.75

  • Row 2: y = 1.25

  • Row 3: y = 0.75

  • Row 4 (bottom): y = 0.25

  • Column 1 (left): x = 0.25

  • Column 2: x = 0.75

  • Column 3: x = 1.25

  • Column 4 (right): x = 1.75

5. Highlighting Special Nodes and Connections

\node[b1, fill = blue] (A) [inner sep = 2pt] at (0.25, 1.75) {\textbf{a}};
\node[b1, fill = blue] (G) [inner sep = 2pt] at (1.25, 1.25) {\textbf{g}};
\node[b1, fill = blue] (J) [inner sep = 2pt] at (0.75, 0.75) {\textbf{j}};
\node[b1, fill = blue] (P) [inner sep = 2pt] at (1.75, 0.25) {\textbf{p}};

Four nodes (a, g, j, p) are highlighted in blue, and thick blue lines connect them to form a specific pattern.

Conclusion

This TikZ example demonstrates how to create professional-looking grid matrices with labeled nodes. The combination of geometric framing, systematic node placement, and selective highlighting creates a diagram that's both mathematically precise and visually appealing.

The techniques shown here—nested rectangles, styled nodes, systematic coordinate planning, and selective highlighting—form the foundation for creating a wide variety of grid-based mathematical diagrams. Whether you're illustrating algorithms, presenting research, or creating educational materials, these TikZ patterns will help you create clear, professional visualizations.

0
Subscribe to my newsletter

Read articles from Amirkiarash Kiani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amirkiarash Kiani
Amirkiarash Kiani

LaTeX enthusiastic. Beginner Ubuntu use and web developer.