# Drawing arbitrary shaped grids in TikZ

I would like to reproduce grids like the following (taken from here, p.5):

So far, I've come up with the following TikZ code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}

\tikzset{
square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={
rectangle,
draw=black,
minimum height=#1,
anchor=center,
align=center,
text width=#1,
text height=2ex,
text depth=0.5ex,
inner sep=0pt,
}
},
square matrix/.default=1.2em
}

\matrix[square matrix]
{
&&&&                      1 & 2       & 2       & 1 \\
\ldots & 1 & 1    & 1   & 2 & $\star$ & $\star$ & 3       & 1 \\
\ldots & 1 & $x'$ & $x$ & 2 & $x'$    & $\star$ & $\star$ & 2 \\
\ldots & 1 & 1    & 1   & 1 & 2       & $x$     & $\star$ & 2 \\
&&&&&                         1       & 2       & 2       & 1 \\
&&&&&                         1       & $x'$    & 1 \\
&&&&&                         1       & $x$     & 1 \\
&&&&&                         1       & 1       & 1 \\
&&&&&                         \vdots  & \vdots  & \vdots \\
};

\end{tikzpicture}
\end{document}


The following details are still missing:

• The X-> labels
• The cells containing the dots should be open to one side

In addition, it would be nice to make the outer border a little bit thicker than the inner lines. Also, I'm not quite sure if I set the correct options for the nodes. They should be exact squares with equal baselines for the text inside.

If there's a totally different but easier way of drawing this whole thing, please tell me.

-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[line cap=rect]

\tikzset{
square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes={
rectangle,
draw=gray,
minimum height=#1,
anchor=center,
align=center,
text width=#1,
text height=2ex,
text depth=0.5ex,
inner sep=0pt,
outer sep=0pt,
}
},
square matrix/.default=1.2em
}

\matrix(m)[square matrix]
{
&&&&                      1 & 2       & 2       & 1 \\
\ldots & 1 & 1    & 1   & 2 & $\star$ & $\star$ & 3       & 1 \\
\ldots & 1 & $x'$ & $x$ & 2 & $x'$    & $\star$ & $\star$ & 2 \\
\ldots & 1 & 1    & 1   & 1 & 2       & $x$     & $\star$ & 2 \\
&&&&&                         1       & 2       & 2       & 1 \\
&&&&&                         1       & $x'$    & 1 \\
&&&&&                         1       & $x$     & 1 \\
&&&&&                         1       & 1       & 1 \\
&&&&&                         \vdots  & \vdots  & \vdots \\
};
\draw[white,very thick] (m-9-6.south west) -- (m-9-8.south east);
\draw[white,very thick] (m-2-1.north west) -- (m-4-1.south west);
\draw[thick] (m-2-1.north west) -- (m-2-4.north east) -- (m-1-5.north west) -- (m-1-8.north east) --
(m-1-8.south east) -- (m-2-9.north east) -- (m-5-9.south east) -- (m-5-9.south west) --
(m-9-8.south east);
\draw[thick] (m-4-1.south west) -- (m-4-5.south east) -- (m-9-6.south west);
\node[anchor=south] (x) at (m-2-1.north east) {X};
\draw[->] (x.east) --  +(2em,0);
\node[anchor=west] (xx) at (m-7-8.east) {X};
\draw[->] (xx.south) --  +(0,-2em);
\end{tikzpicture}
\end{document}


-

This is a alternative answer; it seems you want to do cellular automata. I like this following method for grids. It's very clear code and good looking, I think, but at once it gives you maximum control over every aspect and boundary combination. It's nicely combined with other methods. It does not require a matrix environment.

1)

You'll have to make a folder. You have 6 different tiles I see; you need to make 6 + 1 = 7 .tex document in that case.

Each of the tile documents has code of of this kind:

\documentclass{standalone}%
\usepackage[usenames,dvipsnames]{xcolor}% this must be loaded prior \usepackage{tikz}
% general package
\usepackage{amsfonts,amsmath}%
\usepackage{tikz}%
% specific package

\begin{document}
\begin{tikzpicture}[auto]
\tikzstyle{every node}=[font=\Large]

\draw [-,line width=1pt,red,solid] (0,0) edge (0,1);
\draw [-,line width=1pt,red,solid] (0,1) edge (1,1);
\draw[dotted](0.5,0.5) node {$x$};
\draw [-,line width=1pt,black,solid] (1,1) edge (1,0);
\draw [-,line width=1pt,black,solid] (1,0) edge (0,0);

\end{tikzpicture}
\end{document}


Simply replace the line parameters and the text inside $...$

Lets suppose you make two tiles, one called anicon.tex, the other ohicon.tex and compile them

The final main document is where you work:

\documentclass{article}%
\usepackage[usenames,dvipsnames]{xcolor}%
% general package
\usepackage{amsfonts,amsmath}%
\usepackage{tikz}%
% specific package
\usepackage{float}

% a grid point
\def\gp#1#2{\node at (#1) {\includegraphics{\csname #2\endcsname}};}

\begin{document}
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[auto]

\gp{0,4}{R}
\gp{0,3}{x}\gp{1,3}{R}
\gp{-1,2}{R}\gp{0,2}{x}
\gp{0,1}{R}\gp{1,1}{R}

\draw[->,thick] (-1,0) -- (2,0);
\draw[solid](-1.5,0) node {$\textbf{F}$};
\draw[->,thick] (-1,3) -- (-2,3);
\draw[solid](-2.5,3) node {$\textbf{E}$};

\end{tikzpicture}
\end{center}
\caption{FIGURE CAPTION GOES HERE}
\label{fig:FIGURE NAME GOES HERE}
\end{figure}
\end{document}


You can very rapidly construct arbitrary and good looking automata diagrams

You can change color to white of any box side to make it transparent (or make a highlighted side) and recompile with a click and then a click in your main document. All boxes automatically update at once, so you don't have to change them manually if you decide to change a function rule.

2)

If you want to, you can further save filesize and reduce number of files to 2 documents, and also reduce the amount you need to type to the minimum possible.

In this example, we have 4 different functions = 4 different boxes, but they are side by side in one file. Our texture file is named iconset and we crop parts of it and rearrange them in our automata file:

% iconset
\documentclass{standalone}%
\usepackage[usenames,dvipsnames]{xcolor}
% general package
\usepackage{amsfonts,amsmath}%
\usepackage{tikz}%
% specific package

\begin{document}
\begin{tikzpicture}[auto]
\tikzstyle{every node}=[font=\large]

\draw [-,line width=1pt,red,solid] (0,0) edge (0,1);
\draw [-,line width=1pt,red,solid] (0,1) edge (1,1);
\draw[dotted](0.5,0.5) node {$X$};
\draw [-,line width=1pt,black,solid] (1,1) edge (1,0);
\draw [-,line width=1pt,black,solid] (1,0) edge (0,0);

\draw [-,line width=1pt,red,solid] (0,1) edge (0,2);
\draw [-,line width=1pt,red,solid] (0,2) edge (1,2);
\draw[dotted](0.5,1.5) node {$\textbf{a}$};
\draw [-,line width=1pt,black,solid] (1,2) edge (1,1);
\draw [-,line width=1pt,black,solid] (1,1) edge (0,1);

\draw [-,line width=1pt,red,solid] (-1,1) edge (-1,2);
\draw [-,line width=1pt,red,solid] (-1,2) edge (0,2);
\draw[dotted](-0.5,1.5) node {$\textbf{b}$};
\draw [-,line width=1pt,black,solid] (0,2) edge (0,1);
\draw [-,line width=1pt,black,solid] (0,1) edge (-1,1);

\draw [-,line width=1pt,red,solid] (-1,0) edge (-1,1);
\draw [-,line width=1pt,red,solid] (-1,1) edge (0,1);
\draw[dotted](-0.5,0.5) node {$Y$};
\draw [-,line width=1pt,black,solid] (0,1) edge (0,0);
\draw [-,line width=1pt,black,solid] (0,0) edge (-1,0);

\end{tikzpicture}
\end{document}


Our work figure is this file

% automata
\documentclass{article}%
\usepackage[usenames,dvipsnames]{xcolor}%
% general package
\usepackage{amsfonts,amsmath}%
\usepackage{tikz}%
% specific package
\usepackage{float}

% a grid point
\def\ai#1{\node at (#1) {%
\includegraphics[trim = 0mm 10.1mm 10.1mm 0mm, clip]{iconset}};}
\def\bi#1{\node at (#1) {%
\includegraphics[trim = 10.1mm 10.1mm 0mm 0mm, clip]{iconset}};}
\def\Yi#1{\node at (#1) {%
\includegraphics[trim = 0mm 0mm 10.1mm 10.1mm, clip]{iconset}};}
\def\Xi#1{\node at (#1) {%
\includegraphics[trim = 10.1mm 0mm 0mm 10.1mm, clip]{iconset}};}

\begin{document}
\begin{figure}[H]
\begin{center}
\begin{tikzpicture}[auto]

\Yi{0,4}
\ai{0,3} \Xi{1,3}
\Xi{-1,2} \bi{0,2}
\Xi{0,1} \bi{1,1}

\draw[->,thick] (-1,0) -- (2,0);
\draw[solid](-1.5,0) node {$\textbf{F}$};
\draw[->,thick] (-1,3) -- (-2,3);
\draw[solid](-2.5,3) node {$\textbf{E}$};

\end{tikzpicture}
\end{center}
\caption{FIGURE CAPTION GOES HERE}
\label{fig:FIGURE NAME GOES HERE}
\end{figure}
\end{document}


This method is typically used in a game engine because it is the most processor efficient, requires least memory, has smallest filesize, and makes for the cleanest cost.

-