[Tex/LaTex] How to make a square grid in latex

diagramsgraphs

I want to make the following square grid in latex. Can anyone please help me on this ? .enter image description here

\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.4, vertices/.style={draw, fill=black, circle, inner sep=0.5pt}]
\node[vertices, label=left:{$a_{13}$}] (a) at (1,1) {};
\node[vertices, label=below:{$a_{14}$}] (b) at (5,1) {};
\node[vertices,label=below:{$a_{15}$}] (c) at (18,1) {};
\node[vertices,label=right:{$a_{13}$}] (d) at (22,1) {};
\node[vertices,label=left:{$a_{11}$}] (e) at (1,5) {}; 
\node[vertices, label=right:{$a_{12}$}] (f) at (5,5) {};
\node[vertices, label=left:{$a_{16}$}] (g) at (18,5) {};
\node[vertices, label=right:{$a_{11}$}] (h) at (22,5) {};
\node[vertices, label=left:{$a_{8}$}] (i) at (1,13) {};
\node[vertices, label=right:{$a_{10}$}] (j) at (5,13) {};
\node[vertices, label=left:{$a_{7}$}] (k) at (1,17) {};
\node[vertices, label=right:{$a_{9}$}] (l) at (5,17) {};
\node[vertices, label=left:{$a_{2}$}] (m) at (1,21) {};
\node[vertices, label=right:{$a_{3}$}] (n) at (5,21) {};
\node[vertices, label=left:{$a_{1}$}] (o) at (1,25) {};
\node[vertices, label=above:{$a_{5}$}] (p) at (5,25) {};
\node[vertices, label=above:{$a_{6}$}] (q) at (9,25) {};
\node[vertices, label=right:{$a_{4}$}] (r) at (9,21) {};
\node[vertices, label=above:{$a_{20}$}] (s) at (18,25) {};
\node[vertices, label=right:{$a_{1}$}] (t) at (22,25) {};
\node[vertices, label=left:{$a_{19}$}] (u) at (18,21) {};
\node[vertices, label=right:{$a_{2}$}] (v) at (22,21) {};
\node[vertices, label=left:{$a_{18}$}] (w) at (18,17) {};
\node[vertices, label=right:{$a_{7}$}] (x) at (22,17) {};
\node[vertices, label=left:{$a_{17}$}] (y) at (18,13) {};
\node[vertices, label=right:{$a_{8}$}] (z) at (22,13) {};
\foreach \to/\from in
{a/b,a/e,e/f,b/f,c/d,c/g,g/h,h/d,k/i, k/l, l/j, i/j, k/j, o/m, o/p, o/n, m/n, n/p, m/k, m/l,     l/n, p/q, p/r, q/r, r/n, s/u, s/t, u/v, t/v, s/v, u/w,v/x, w/x, u/x, w/y, x/z, y/z, z/w, e/b, g/d} \draw [-] (\to)--(\from);
\foreach \to/\from in
{b/c,f/g, e/i,j/f, q/s, r/u, l/w, y/g, z/h, j/y} \draw [dashed]     (\to)--     (\from);
\end{tikzpicture}
\end{document}

Best Answer

There might be easier ways, but I think a Tikz matrix is a good approach.

Output

enter image description here

Code

\documentclass[tikz,margin=10pt]{standalone}

\usetikzlibrary{matrix}

\tikzset{
    basic/.style={
        rectangle,
        minimum size=1.8cm,
        text depth=0.5ex,
        text height=2ex,
        inner sep=0pt,
        outer sep=0pt,
    },
    table nodes/.style 2 args={
        draw=black,
        append after command={
            \pgfextra{\draw (\tikzlastnode.north west) -- (\tikzlastnode.south east) 
                node[midway, anchor=north east, minimum size=7mm] {#1}
                node[midway, anchor=south west, minimum size=7mm] {#2};
            }
        }
    },      
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={   
            basic        
        },
        execute at empty cell={\node{};}
    }
}

\newcommand\dcell[2]{%
    |[table nodes={#1}{#2}]|
}

\newcommand\link[3]{%
    \def\switchrow{row}
    \def\switchcol{col}
    \def\argi{#1}
    \ifx\argi\switchrow
        \draw[gray, dashed] (#2.south east) -- (#3.south west);
        \draw[gray, dashed] (#2.north east) -- (#3.north west);
    \else\ifx\argi\switchcol
        \draw[gray, dashed] (#2.south east) -- (#3.north east);
        \draw[gray, dashed] (#2.south west) -- (#3.north west); 
    \fi
    \fi
}

\begin{document}
\begin{tikzpicture}

\matrix (m) [table]
{
    \dcell{$A_1$}{$A_2$} & \dcell{$B_1$}{$B_2$} & & & \dcell{$C_1$}{$C_2$} \\
    \dcell{$C_3$}{$C_4$} &   & & & \dcell{$C_9$}{$C_10$} \\
    \dcell{$C_5$}{$C_6$} &   & & & \dcell{}{} \\
                        &   & & &  \\
                        &   & & &  \\
    \dcell{$C_7$}{$C_8$} &   & & & \dcell{}{} \\
};

\link{row}{m-1-2}{m-1-5}
\link{row}{m-3-1}{m-3-5}
\link{row}{m-6-1}{m-6-5}

\link{col}{m-3-1}{m-6-1}
\link{col}{m-3-5}{m-6-5}

\end{tikzpicture}
\end{document}
Related Question