\usepgfmodule{matrix}

\def\pgfmatrixlabelskeleton{
  \let\pgf@matrix@compute@origin\pgf@matrix@skeleton@compute@origin
  \let\pgf@matrix@shift@nodes@initial\pgf@matrix@skeleton@shift@nodes@initial
  \let\pgf@end@matrix\pgf@end@matrix@skeleton
}

\expandafter\def\expandafter\pgf@end@matrix@skeleton\expandafter{\pgf@end@matrix \pgf@matrix@create@skeleton}%

% Creates the skeleton

% orig contains the upper left corner of the different boxes

\def\pgf@matrix@create@skeleton{%
  \xdef\pgf@matrix@skel@innerxsep{\pgfkeysvalueof{/pgf/inner xsep}}%
  \xdef\pgf@matrix@skel@innerysep{\pgfkeysvalueof{/pgf/inner ysep}}%
  \pgfscope%
    \pgfgetlastxy\ck@shift@x\ck@shift@y
    \pgftransformshift{\pgfpoint{\ck@shift@x}{\ck@shift@y}}%
    \pgfset{inner sep=0pt}%
    \pgf@matrix@skeleton@create@cells%
    \pgf@matrix@skeleton@create@tiling@cells%
    \pgf@matrix@skeleton@create@rows%
    \pgf@matrix@skeleton@create@tiling@rows%
    \pgf@matrix@skeleton@create@columns%
    \pgf@matrix@skeleton@create@tiling@columns%
  \endpgfscope%
}

\def\pgf@matrix@skeleton@create@cells{%
  \newcount\pgf@matrix@skel@next@row
  \foreach \pgf@matrix@skel@row in {1, ..., \pgfmatrixcurrentrow} {%
    \pgf@matrix@skel@next@row=\pgf@matrix@skel@row
    \advance\pgf@matrix@skel@next@row by 1
    \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy\pgf@matrix@skel@row\endcsname + \csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname}%
    \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@miny\pgf@matrix@skel@row\endcsname - \pgflinewidth}%
    \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height - \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname}
    \foreach \pgf@matrix@skel@col in {1, ..., \pgf@matrix@numberofcolumns} {%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname - \pgflinewidth}%
      \ifnum\pgf@matrix@skel@col>1\relax%
        \pgfmathsetmacro{\pgf@matrix@skel@origx}{\pgf@matrix@skel@origx + \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
        \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width - \pgflinewidth - \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      \fi%
      {%
        \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
        \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
        \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-cell-\pgf@matrix@skel@row-\pgf@matrix@skel@col}{}%
      }
    }
  }
}

\def\pgf@matrix@skeleton@create@tiling@cells{%
  \newcount\pgf@matrix@skel@next@col
  \newcount\pgf@matrix@skel@next@row
  \foreach \pgf@matrix@skel@row in {1, ..., \pgfmatrixcurrentrow} {%
    \pgf@matrix@skel@next@row=\pgf@matrix@skel@row
    \advance\pgf@matrix@skel@next@row by 1
    \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy\pgf@matrix@skel@row\endcsname + \csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname}%
    \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@miny\pgf@matrix@skel@row\endcsname - \pgflinewidth}%
    \ifnum\pgf@matrix@skel@row=1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origy}{\pgf@matrix@skel@origy + \pgf@matrix@skel@innerysep}%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + \pgf@matrix@skel@innerysep}%
    \fi
    \ifnum\pgf@matrix@skel@row=\pgfmatrixcurrentrow\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + \pgf@matrix@skel@innerysep - \pgfmatrixrowsep}%
    \fi
    \ifnum\pgf@matrix@skel@row>1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origy}{\pgf@matrix@skel@origy + 0.5 * \csname pgf@matrix@row@sep@\pgf@matrix@skel@row\endcsname}
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + 0.5 * \csname pgf@matrix@row@sep@\pgf@matrix@skel@row\endcsname}
    \fi
    \ifnum\pgf@matrix@skel@row<\pgfmatrixcurrentrow\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height - 0.5 * \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname}
    \fi
    \foreach \pgf@matrix@skel@col in {1, ..., \pgf@matrix@numberofcolumns} {%
      \pgf@matrix@skel@next@col=\pgf@matrix@skel@col
      \advance\pgf@matrix@skel@next@col by 1
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname- \pgflinewidth}%
      \ifnum\pgf@matrix@skel@col=1\relax%
        \pgfmathsetmacro{\pgf@matrix@skel@origx}{\pgf@matrix@skel@origx - \pgf@matrix@skel@innerxsep}%
        \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + \pgf@matrix@skel@innerxsep}%
      \fi%
      \ifnum\pgf@matrix@skel@col=\pgf@matrix@numberofcolumns\relax%
        \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + \pgf@matrix@skel@innerxsep}%
      \fi%
      \ifnum\pgf@matrix@skel@col<\pgf@matrix@numberofcolumns%
        \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + 0.5 * \csname pgf@matrix@column@sep@\the\pgf@matrix@skel@next@col\endcsname}%
      \fi%
      \ifnum\pgf@matrix@skel@col>1%
        \pgfmathsetmacro{\pgf@matrix@skel@origx}{\pgf@matrix@skel@origx + 0.5 * \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
        \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width - 0.5 * \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      \fi%
      {%
        \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
        \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
        \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-tiling-cell-\pgf@matrix@skel@row-\pgf@matrix@skel@col}{}%
      }
    }
  }
}

\def\pgf@matrix@skeleton@create@rows{%
  \newcount\pgf@matrix@skel@next@row%
  \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@origx\the\pgf@matrix@numberofcolumns\endcsname + \csname pgf@matrix@maxx\the\pgf@matrix@numberofcolumns\endcsname - \pgflinewidth}%
  \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx1\endcsname + \csname pgf@matrix@minx1\endcsname}%
  \foreach \pgf@matrix@skel@row in {1, ..., \pgfmatrixcurrentrow} {%
    \pgf@matrix@skel@next@row=\pgf@matrix@skel@row\relax%
    \advance\pgf@matrix@skel@next@row by1\relax%
    \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy\pgf@matrix@skel@row\endcsname + \csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname}%
    \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@miny\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname - \pgflinewidth}%
    {%
      \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
      \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
      \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-row-\pgf@matrix@skel@row}{}%
    }
    \ifnum\pgf@matrix@skel@row<\pgfmatrixcurrentrow\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy\the\pgf@matrix@skel@next@row\endcsname + \csname pgf@matrix@maxy\the\pgf@matrix@skel@next@row\endcsname + \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname + 0.5 * \pgflinewidth}%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname}%
      {%
        \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
        \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
        \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-inter-row-\pgf@matrix@skel@row}{}%
      }
    \fi%
  }
}

\def\pgf@matrix@skeleton@create@tiling@rows{%
  \newcount\pgf@matrix@skel@next@row%
  \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@origx\the\pgf@matrix@numberofcolumns\endcsname + \csname pgf@matrix@maxx\the\pgf@matrix@numberofcolumns\endcsname - \pgflinewidth + 2 * \pgf@matrix@skel@innerxsep}%
  \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx1\endcsname + \csname pgf@matrix@minx1\endcsname - \pgf@matrix@skel@innerxsep}%
  \foreach \pgf@matrix@skel@row in {1, ..., \pgfmatrixcurrentrow} {%
    \pgf@matrix@skel@next@row=\pgf@matrix@skel@row\relax%
    \advance\pgf@matrix@skel@next@row by1\relax%
    \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy\pgf@matrix@skel@row\endcsname + \csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname}%
    \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@maxy\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@miny\pgf@matrix@skel@row\endcsname - \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname - \pgflinewidth}%
    \ifnum\pgf@matrix@skel@row=1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origy}{\pgf@matrix@skel@origy + \pgf@matrix@skel@innerysep}%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + \pgf@matrix@skel@innerysep}%
    \fi
    \ifnum\pgf@matrix@skel@row=\pgfmatrixcurrentrow\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + \pgf@matrix@skel@innerysep}%
    \fi
    \ifnum\pgf@matrix@skel@row>1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origy}{\pgf@matrix@skel@origy + 0.5 * \csname pgf@matrix@row@sep@\pgf@matrix@skel@row\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + 0.5 * \csname pgf@matrix@row@sep@\pgf@matrix@skel@row\endcsname}%
    \fi%
    \ifnum\pgf@matrix@skel@row<\pgfmatrixcurrentrow\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@height}{\pgf@matrix@skel@height + 0.5 * \csname pgf@matrix@row@sep@\the\pgf@matrix@skel@next@row\endcsname}%
    \fi%
    {%
      \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
      \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
      \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-tiling-row-\pgf@matrix@skel@row}{}%
    }
  }
}

\def\pgf@matrix@skeleton@create@columns{%
  \newcount\pgf@matrix@skel@prev@col%
  \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@origy1\endcsname + \csname pgf@matrix@maxy1\endcsname - \csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname - \pgfmatrixrowsep - \pgflinewidth}%
  \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy1\endcsname + \csname pgf@matrix@maxy1\endcsname}%
  \foreach \pgf@matrix@skel@col in {1, ..., \pgf@matrix@numberofcolumns} {%
    \ifnum\pgf@matrix@skel@col>1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname - \pgflinewidth}%
    \else%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname - \pgflinewidth}%
    \fi%
    {%
      \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
      \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
      \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-column-\pgf@matrix@skel@col}{}%
    }
    \ifnum\pgf@matrix@skel@col>1\relax%
      \pgf@matrix@skel@prev@col=\pgf@matrix@skel@col\relax%
      \advance\pgf@matrix@skel@prev@col by-1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\the\pgf@matrix@skel@prev@col\endcsname + \csname pgf@matrix@maxx\the\pgf@matrix@skel@prev@col\endcsname - 0.5 * \pgflinewidth}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      {%
        \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
        \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
        \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-inter-column-\the\pgf@matrix@skel@prev@col}{}%
      }
    \fi%
  }
}

\def\pgf@matrix@skeleton@create@tiling@columns{%
  \newcount\pgf@matrix@skel@next@col%
  \pgfmathsetmacro{\pgf@matrix@skel@height}{\csname pgf@matrix@origy1\endcsname + \csname pgf@matrix@maxy1\endcsname - \csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname - \pgfmatrixrowsep - \pgflinewidth + 2 * \pgf@matrix@skel@innerysep}%
  \pgfmathsetmacro{\pgf@matrix@skel@origy}{\csname pgf@matrix@origy1\endcsname + \csname pgf@matrix@maxy1\endcsname + \pgf@matrix@skel@innerysep}%
  \foreach \pgf@matrix@skel@col in {1, ..., \pgf@matrix@numberofcolumns} {%
    \pgf@matrix@skel@next@col=\pgf@matrix@skel@col\relax%
    \advance\pgf@matrix@skel@next@col by1\relax%
    \ifnum\pgf@matrix@skel@col>1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname - \pgflinewidth}%
    \else%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\csname pgf@matrix@origx\pgf@matrix@skel@col\endcsname + \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\csname pgf@matrix@maxx\pgf@matrix@skel@col\endcsname - \csname pgf@matrix@minx\pgf@matrix@skel@col\endcsname - \pgflinewidth}%
    \fi%
    \ifnum\pgf@matrix@skel@col=1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\pgf@matrix@skel@origx - \pgf@matrix@skel@innerxsep}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + \pgf@matrix@skel@innerxsep}%
    \fi
    \ifnum\pgf@matrix@skel@col=\pgf@matrix@numberofcolumns\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + \pgf@matrix@skel@innerxsep}%
    \fi
    \ifnum\pgf@matrix@skel@col>1\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@origx}{\pgf@matrix@skel@origx - 0.5 * \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + 0.5 * \csname pgf@matrix@column@sep@\pgf@matrix@skel@col\endcsname}%
    \fi
    \ifnum\pgf@matrix@skel@col<\pgf@matrix@numberofcolumns\relax%
      \pgfmathsetmacro{\pgf@matrix@skel@width}{\pgf@matrix@skel@width + 0.5 * \csname pgf@matrix@column@sep@\the\pgf@matrix@skel@next@col\endcsname}%
    \fi
    {%
      \pgfset{minimum width=\pgf@matrix@skel@width, minimum height=\pgf@matrix@skel@height}%
      \pgftransformshift{\pgfpoint{\pgf@matrix@skel@origx pt}{\pgf@matrix@skel@origy pt}}%
      \pgfnode{rectangle}{north west}{}{\pgf@matrix@par@name-tiling-column-\pgf@matrix@skel@col}{}%
    }
  }
}

% Compute the real positions of the origins

% We must now compute the real positions of the origins of all the
% small pictures. To this end, we need to compute prefix sums. After
% the procedure is done, the origx and the origy will contain the origin
% positions.

\def\pgf@matrix@skeleton@compute@origin{%
  %
  % Inverse prefix sum on the vertical positions
  %
  {%
    \ifnum\pgfmatrixcurrentrow>0\relax%
      \expandafter\gdef\csname pgf@matrix@origy\the\pgfmatrixcurrentrow\endcsname{0pt}%
    \fi%
    \c@pgf@counta=\pgfmatrixcurrentrow\relax%
    \loop%
    \ifnum\c@pgf@counta>1\relax%
      \pgf@y=\csname pgf@matrix@origy\the\c@pgf@counta\endcsname\relax%
      \advance\pgf@y by\csname pgf@matrix@maxy\the\c@pgf@counta\endcsname\relax
      \advance\c@pgf@counta by-1\relax%
      \pgf@ya=\csname pgf@matrix@miny\the\c@pgf@counta\endcsname\relax%
      \advance\pgf@y by-\pgf@ya\relax%
      \expandafter\xdef\csname pgf@matrix@origy\the\c@pgf@counta\endcsname{\the\pgf@y}%
    \repeat%
  }%
  %
  % Prefix sum on the horizontal positions
  %
  {%
    \ifnum\pgf@matrix@numberofcolumns>0\relax%
      \pgf@x=\csname pgf@matrix@minx1\endcsname\relax%
      \pgf@x=-\pgf@x%
      \expandafter\xdef\csname pgf@matrix@origx1\endcsname{\the\pgf@x}%
    \fi%
    \c@pgf@counta=1\relax%
    \loop%
    \ifnum\c@pgf@counta<\pgf@matrix@numberofcolumns\relax%
      \pgf@x=\csname pgf@matrix@origx\the\c@pgf@counta\endcsname\relax%
      \advance\pgf@x by\csname pgf@matrix@maxx\the\c@pgf@counta\endcsname\relax%
      \advance\c@pgf@counta by1\relax%
      \pgf@xa=\csname pgf@matrix@minx\the\c@pgf@counta\endcsname\relax%
      \advance\pgf@x by-\pgf@xa\relax%
      \expandafter\xdef\csname pgf@matrix@origx\the\c@pgf@counta\endcsname{\the\pgf@x}%
    \repeat%
  }%
}


% Shift the nodes to their origins

% The following procedure shifts all nodes in
% \pgf@matrix@node@list to their location inside a temporary
% picture. This picture will later be shifted again to its final
% position in the real picture.

\def\pgf@matrix@skeleton@shift@nodes@initial{%
  {%
  \pgfutil@for\pgf@matrix@node@name:=\pgf@matrix@node@list\do{%
    \ifx\pgf@matrix@node@name\pgfutil@empty%
    \else%
      \expandafter\ifx\csname pgf@matrix@node@visited@\pgf@matrix@node@name\endcsname\relax%
        \pgf@shift@node{\pgf@matrix@node@name}{%
          \pgf@x=\csname pgf@matrix@origx%
            \expandafter\expandafter\expandafter\pgfutil@secondoftwo\csname pgf@matrix@node@location@\pgf@matrix@node@name\endcsname\endcsname%
          \pgf@y=\csname pgf@matrix@origy%
            \expandafter\expandafter\expandafter\pgfutil@firstoftwo\csname pgf@matrix@node@location@\pgf@matrix@node@name\endcsname\endcsname%
          }%
        \expandafter\let\csname pgf@matrix@node@visited@\pgf@matrix@node@name\endcsname=\pgfutil@empty%
      \fi%
    \fi%
  }%
  }%
}


% End of line
\def\pgfmatrixendrow{%
  % if the cell contains nothing, the following \let will be at the
  % beginning (macro expansion has stopped here since neither \omit
  % nor \span was found)
  \let\pgf@matrix@signal@cell@end=\pgf@matrix@signal@cell@end%
  &\pgf@matrix@correct@calltrue%
  \global\pgf@matrix@fixedfalse%
  \pgf@y=0pt%
  \pgf@matrix@addtolength\pgf@y{\pgfmatrixrowsep}%
  \pgfutil@ifnextchar[{\pgfmatrixendrow@skip}{
  {
  \advance\pgfmatrixcurrentrow by1\relax % only temporary for the following:
  \expandafter\xdef\csname pgf@matrix@row@sep@\the\pgfmatrixcurrentrow\endcsname{\the\pgf@y}%
  }
  \pgf@matrix@finish@line}%
}

\def\pgfmatrixendrow@skip[#1]{%
  \pgf@matrix@addtolength\pgf@y{#1}%
  {
  \advance\pgfmatrixcurrentrow by1\relax % only temporary for the following:
  \expandafter\xdef\csname pgf@matrix@row@sep@\the\pgfmatrixcurrentrow\endcsname{\the\pgf@y}%
  }
  \pgf@matrix@finish@line%
}
