6

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.

3
  • 1
    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
    Commented Mar 4, 2012 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. Commented Mar 4, 2012 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
    Commented Mar 4, 2012 at 5:43

2 Answers 2

11

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.

4
  • How to replace parenthesise with brackets [] in the matrix?
    – Rasoul
    Commented Sep 6, 2013 at 15:13
  • 1
    @Rasoul: Replace the use of (...) inside the inner block with [...].
    – Werner
    Commented Sep 6, 2013 at 16:11
  • Sorry, If i want to insert the matrix in \underbrace{}{}, there is a way to fix the distance?
    – Andrea Leo
    Commented Aug 28, 2015 at 18:22
  • I mean, the brace is very distance.
    – Andrea Leo
    Commented Aug 28, 2015 at 18:24
1

Here is a solution with {pNiceMatrix} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions
  {
    code-for-first-row = $\small$ , 
    code-for-last-col = $\small$ 
  }

$A = \begin{pNiceMatrix}[first-row,last-col,margin,r]
 1  & 2 \\
100 & b  & \text{row } 1 \\
  2 & 12 & \text{row } 2 \\
  e & f  & \text{ABDC}
\end{pNiceMatrix}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .