[Tex/LaTex] TikZ-equivalent of bordermatrix with automatic alignment of labels

bordermatrixmatricespositioningtikz-pgf

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:

bordermatrix with image

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);
        \fill (0.5, 0.5) circle[radius=0.5];
      \end{tikzpicture}
    }; & \\
    & \node[draw=none, fill=none] {
      \begin{tikzpicture}
        \path[use as bounding box] (0, 0) rectangle (2, 2);
        \fill (1.0, 1.0) circle[radius=1.0];
      \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 };
}

Best Answer

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

enter image description here