[Tex/LaTex] How to create a matrix with rows and columns labeled (at the right side)

bordermatrixmatrices

I would like to create a matrix with rows and columns being labeled. I would
like the matrix elements to be right aligned, the row labels being displayed to
the right of the matrix, and the column labels above the matrix. I tried:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[american,ngerman]{babel}

\begin{document}
% does work:
$\bordermatrix{%
 & 1 & 2 \cr
1 & x1 & x2 \cr
2 & x3 & x423 \cr
3 & x5 & x6
}$
% does not work (see error below):
% $\bordermatrix*{%
%   x1&x2&1\cr
%   x3&x4&2\cr
%   x5&x6&3\cr
%   1&2 6 }$
\end{document}

% ERROR: Misplaced alignment tab character &.

% --- TeX said ---
% l.17   x1&
%           x2&1\cr
% --- HELP ---
% The special character &, which should be used only to separate items
% in an array or tabular environment, appeared in ordinary text. You
% probably meant to type \&.

To get at least the row labels displayed at the correct side of the matrix, I
wanted to use the starred version of \bordermatrix, but that did not work (as
opposed to given in Voss's mathmode guide). Also, the matrix entries are not
right aligned. How can I create such a matrix? Is there a way to do this with amsmath?

UPDATE

Thanks to the link by Werner, a minimal example is this:

\documentclass{article}
\usepackage{blkarray}
\newcommand{\matindex}[1]{\mbox{\scriptsize#1}}
\begin{document}
\[
  A=\begin{blockarray}{rrl}
    \matindex{1} & \matindex{2} & \\
    \begin{block}{(rr)l}
      \,100 & b\phantom{\,} & \matindex{row 1} \\
      2 & 12 & \matindex{row 2} \\
      e & f &  \matindex{ABDC} \\
    \end{block}
  \end{blockarray}
\]
\end{document}

It's just a quick hack to get a bit more horizontal space between the parentheses and the matrix entries.

Best Answer

Here is a way to insert your matrix indices on the top (for columns) and right (for rows):

enter image description here

\documentclass{article}
\usepackage{blkarray}% http://ctan.org/pkg/blkarray
\newcommand{\matindex}[1]{\mbox{\scriptsize#1}}% Matrix index
\begin{document}
\[
  A=\begin{blockarray}{c@{\hspace{5pt}}rr@{\hspace{5pt}}cl}
    & \matindex{1} & \matindex{2} & & \\
    \begin{block}{(c@{\hspace{5pt}}rr@{\hspace{5pt}}c)l}
      & 100 &  b & & \matindex{row 1} \\
      &   2 & 12 & & \matindex{row 2} \\
      &   e &  f & & \matindex{ABDC} \\
    \end{block}
  \end{blockarray}
\]
\end{document}

To improve the spacing between the matrix and the surrounding braces, insert an extra column on either side. Additionally, specify the exact column gap using the @{...} specifier. In the above example, two "dummy" centered columns were added with an \hspace{5pt} gap.

Related Question