# How to create a matrix with rows and columns labeled (at the right side)?

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.

-
Possible duplicate: Write elements above and right outside the matrix. And then there's a more generic answer in Where is the \matrix command? –  Werner Mar 4 '12 at 2:56
I looked quite hard, but didn't find that post. Thanks, Werner. I posted a minimal example above. I had to use some ugly tricks to get a nicer spacing. Maybe that can be improved. –  Marius Hofert Mar 4 '12 at 3:17
You don't have to place \, in a \phantom, since it is only white space anyway. I've added an answer to reflect some possible spacing alternatives. –  Werner Mar 4 '12 at 5:43

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

\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.

-
Perfect, many thanks, Werner. –  Marius Hofert Mar 4 '12 at 7:48
How to replace parenthesise with brackets [] in the matrix? –  Rasoul Sep 6 '13 at 15:13
@Rasoul: Replace the use of (...) inside the inner block with [...]. –  Werner Sep 6 '13 at 16:11