I'm trying to extract coordinates from a named coordinate, then to use it for an additional drawings in the next code
\documentclass[tikz, border=0.5]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{fmtcount}% http://ctan.org/pkg/fmtcount
\usetikzlibrary{calc, patterns, intersections}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% note,
% 0 - is an empty space; left uncolored
% 1 - is an other agent; filled with black
% 2 - is an edge; cross-hatched
% 3 - is an unspecified (obscured location); filled with gray
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% model parameters
\pgfmathtruncatemacro{\memory}{2}
\pgfmathtruncatemacro{\visibility}{2}
\pgfmathsetmacro{\maxtablelength}{8}
\begin{document}
\newcommand*{\ExtractCoordinate}[1]{\path (#1); \pgfgetlastxy{\XCoord}{\YCoord}}%
% calculate the sizes of the neighborhood
\newcommand{\GetNeighborhoodSizes}[1]{%
\xdef\toleft{0}
\xdef\toright{0}
\xdef\toup{0}
\xdef\todown{0}
\pgfmathtruncatemacro{\tableSize}{2 * \visibility + 1}
\foreach \columnoffset/\rowoffset[count=\i] in #1 {%
\ifnum\columnoffset<\toleft%
\xdef\toleft{\columnoffset}%
\fi%
\ifnum\columnoffset>\toright%
\xdef\toright{\columnoffset}%
\fi%
\ifnum\rowoffset<\todown%
\xdef\todown{\rowoffset}%
\fi%
\ifnum\rowoffset>\toup%
\xdef\toup{\rowoffset}%
\fi%
\coordinate (anchor_\i) at (\columnoffset, \rowoffset);
}
\xdef\xbase{\toleft}
\xdef\ybase{\todown}
\pgfmathtruncatemacro{\pxsize}{\toright - \toleft + 1}
\xdef\xsize{\pxsize}
\pgfmathtruncatemacro{\pysize}{\toup - \todown + 1}
\xdef\ysize{\pysize}
}
% draw this neighborhood
\newcommand{\DrawNeighborhood}[1]{%
\begin{scope}[shift={(-\xbase, -\ybase)}]
\draw[fill=black] (0.5, + 0.5) circle (3pt);
\draw (0, 0) rectangle +(1,1);
\foreach \value[count=\i] in #1 {%
\ExtractCoordinate{anchor_\i}
\begin{scope}[shift={(\XCoord, \YCoord)}]
\ifnum\value=0%
\draw (0, 0) rectangle +(1, 1);
\fi%
\ifnum\value=1%
\draw[fill=black!80] (0, 0) rectangle +(1, 1);
\fi%
\ifnum\value=2%
\draw[pattern=north west lines] (0, 0) rectangle +(1, 1);
\fi%
\ifnum\value=3%
\draw[fill=black!10] (0, 0) rectangle +(1, 1);
\node at (0.5, 0.5) {\bf ?};
\fi%
\end{scope}
}
\end{scope}
}
% set column and table num
\newcommand{\DrawAtIndex}[1]{%
\pgfmathtruncatemacro{\currentstate}{#1}
\pgfmathtruncatemacro{\tablenum}{ceil((\currentstate + 1) / \maxtablelength)}
\pgfmathtruncatemacro{\columnnum}{mod(\currentstate, \maxtablelength)}
\pgfmathsetmacro{\startoffset}{(1 - \xsize * \scalefactor) / 2}
\begin{scope}[shift={($(left_upper_corner_\tablenum) + (\columnnum + \startoffset, 0)$)}, scale=\scalefactor]
\DrawNeighborhood{\neighbors}
\end{scope}
}
% drawing parameters
\pgfmathsetmacro{\scalefactor}{0.4}
\begin{tikzpicture}
% common code
\pgfmathtruncatemacro{\tableheight}{2^(\memory)}
\pgfmathtruncatemacro{\numberofstates}{2}
\pgfmathtruncatemacro{\numberoftables}{ceil(\numberofstates / \maxtablelength)}
\draw (0, 0) grid (2, 4);
\coordinate (left_upper_corner_1) at (0, 4);
\newcommand{\neighborsCoordinates}{0/-1}
\newcommand{\neighbors}{}
\GetNeighborhoodSizes{\neighborsCoordinates}
\renewcommand{\neighbors}{0}
\DrawAtIndex{0}
\end{tikzpicture}
\end{document}
Basically, everything works fine, except that ExtractCoordinate macro adds this white space under the table.

I have tracked the problem to the \newcommand{\neighborsCoordinates}{0/-1} and then tried to exchange path to coordinate. It solves the white space problem, but breaks the correctness of the image produced (which I also do not understand why).

In the above command, using \newcommand{\neighborsCoordinates}{0/1} produces the right image (for 0/1 pair) and no white space.

How can I fix the issue?
overlayto the\path, i.e.\path [overlay] (#1);That means the bounding box calculation doesn't take that path into account. – Torbjørn T. Apr 12 '20 at 12:23