Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:



% does work:
 & 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 }$

% 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?


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

    \matindex{1} & \matindex{2} & \\
      \,100 & b\phantom{\,} & \matindex{row 1} \\
      2 & 12 & \matindex{row 2} \\
      e & f &  \matindex{ABDC} \\

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

share|improve this question
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

1 Answer 1

up vote 7 down vote accepted

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

enter image description here

\usepackage{blkarray}% http://ctan.org/pkg/blkarray
\newcommand{\matindex}[1]{\mbox{\scriptsize#1}}% Matrix index
    & \matindex{1} & \matindex{2} & & \\
      & 100 &  b & & \matindex{row 1} \\
      &   2 & 12 & & \matindex{row 2} \\
      &   e &  f & & \matindex{ABDC} \\

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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.