# TikZ-equivalent of bordermatrix with automatic alignment of labels

I would like to create a TikZ matrix (containing pictures in the cells) and annotate rows/columns of this matrix is the same manner as \bordermatrix allows. This is (approximately) a picture of what I am after:

and this is the MWE that generated it:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\begin{document}
\begin{tikzpicture}
\matrix (M) [
matrix of nodes,
nodes in empty cells,
inner sep=0pt,
left delimiter={(},
right delimiter={)}
]{
\node[draw=none,fill=none] {
\begin{tikzpicture}
\path[use as bounding box] (0, 0) rectangle (1, 1);
\end{tikzpicture}
}; & \\
& \node[draw=none, fill=none] {
\begin{tikzpicture}
\path[use as bounding box] (0, 0) rectangle (2, 2);
\end{tikzpicture}
}; \\
};
\node[left=12pt of M-1-1.west] {$$A$$};
\node[above left=21pt and 27pt of M-2-1.west] {$$B$$};
\end{tikzpicture}
\end{document}


How can I get rid of the hard-coded relative positioning of the label nodes, and make them automatically (1) placed right outside of the left delimiter, and (2) centered to the row?

Update:

This is the solution by adapting percusse's example in "How can I add size of Tikz drawing into size of equation":

% corner node (unindented slightly to get away from parenthesis)
\node[anchor=south east, left=6pt] (M-0-0) at (M-1-1.north west) {};

% iterate over each row and their corresponding labels
\foreach[count=\i] \v in {$$A$$,$$B$$}{

% label of this row, based on size of diagonal element
\node (M-\i-0) at (M-0-0 |- M-\i-\i) {};

% put the text (specified in the loop) at the label
\path (M-\i-0.north) -- (M-\i-0.south) node [midway, left] { \v };
}

-
Here is one example I did some time ago and here is a little bit more complicated one. Is it automatic enough or you look for a macro or a similar type? – percusse Mar 29 '12 at 10:17
Hei, did you get anywhere with the questions that percusse and Marco Daniel linked to? If not, please edit your question to show where you ran into difficulties, otherwise it's hard to know how best to help. If they did help, we'll close this as a duplicate to help others with similar questions best find the help that they need. – Loop Space Apr 13 '12 at 7:51
@percusse : Please add an answer (which only needs to refer to the update based on your suggestion) – RolKau Apr 13 '12 at 8:18

The result can be achieved by using the OP's code and the linked answers e.g. using:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[baseline = (M.center),% center with respect to the matrix center
every left delimiter/.style={xshift=1ex},%tighter delimiter spacing
every right delimiter/.style={xshift=-1ex}]
\matrix (M) [matrix of nodes,left delimiter={(},right delimiter={)},nodes in empty cells
]{
|[draw,circle,inner sep=3mm,fill]| &  \\
& |[draw,circle,inner sep=6mm,fill]|\\
};
\node[anchor=south east, left=6pt] (M-0-0) at (M-1-1.north west) {};
% iterate over each row and their corresponding labels
\foreach[count=\i] \v in {$$A$$,$$B$$}{
% label of this row, based on size of diagonal element
\node (M-\i-0) at (M-0-0 |- M-\i-\i) {};
% put the text (specified in the loop) at the label
\path (M-\i-0.north) -- (M-\i-0.south) node [midway, left] { \v };
}
\end{tikzpicture}
\end{document}


gives

-