16

Is there an intelligent way to label (or name) columns in a matrix? I would like to have something like

\[M=\left[\begin{array}{c|cc}
1 & 2 & 3\\
4 & 5 & 6
\end{array}\right],N=\left[\begin{array}{c|cc}
7 & 8 & 9\\
10 & 11 & 12
\end{array}\right]\]
\[A \ B \ C \ D \ E \ F\]

where A, B,..., F are the labels (or rather names) of the first, second, ..., sixth columns, respectively. What I want is to label A be exactly below the first column, label B be below the second column, etc., of course, nicely aligned. So far I have forced some extra spaces before the labels manually, and also experimented with the \phantom command.

David Carlisle
  • 757,742
Mats
  • 221

3 Answers3

13

The kbordermatrix package does a neat job in typesetting a matrix with indices. It has a format similar to (La)TeX's \bordermatrix.

enter image description here

\documentclass{article}
\usepackage{kbordermatrix}% http://www.hss.caltech.edu/~kcb/LaTeX.shtml
\newcommand{\noindex}{\hspace*{-0.8em}}%
\begin{document}
\[
  M=\kbordermatrix{%
      & A &        & B & C \\
    1 & 1 & \vrule & 2 & 3 \\
    2 & 4 & \vrule & 5 & 6
  },\quad
  N=\kbordermatrix{%
    \noindex &  D &        &  E &  F \\
    \noindex &  7 & \vrule &  8 &  9 \\
    \noindex & 10 & \vrule & 11 & 12
  }
\]
\end{document}

The vertical alignment with respect to the equation/expression is maintained well.

Werner
  • 603,163
10

You can use the blkarray package for this. It allows you to use blocks inside an array that can be formatted like independent arrays:

\documentclass{article}

\usepackage{blkarray}

\begin{document}

\[M=
\begin{blockarray}{ccc}
A & B & C \\
\begin{block}{[c|cc]}
1 & 2 & 3 \\
4 & 5 & 6 \\
\end{block}
\end{blockarray},\quad
N=
\begin{blockarray}{ccc}
D & E & F \\
\begin{block}{[c|cc]}
7 & 8 & 9 \\
10 & 11 & 12\\
\end{block}
\end{blockarray}
\]

\end{document}

David Carlisle
  • 757,742
Jake
  • 232,450
  • Just flip the indexes to the bottom (OP's request). – Werner Oct 07 '11 at 06:01
  • Well, on a second thought I don't like the vertical alignment of your solution. The matrix names M and N as well as the separating comma are aligned with the whole array (i.e. with its all three lines). – Mats Oct 07 '11 at 06:31
  • @Mats: Are you referring to the case when the column indexes are at the top, or bottom, or both? – Werner Oct 07 '11 at 07:05
  • 1
    I guess it does not matter where I place the indices. In either case, the equality sign is vertically aligned with the whole \blockarray environment, and not with the individual \block which represents the matrix, without its labelling. – Mats Oct 07 '11 at 07:47
0

Here is a solution with the environments of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions { code-for-first-row = \scriptstyle , code-for-first-col = \scriptstyle , }

$M = \begin{bNiceArray}{c|cc}[first-col,first-row] & A & B & C \ 1 & 1 & 2 & 3 \ 2 & 4 & 5 & 6 \end{bNiceArray}$,\qquad $N = \begin{bNiceArray}{c|cc}[first-row] D & E & F \ 7 & 8 & 9 \ 10 & 11 & 12 \end{bNiceArray}$

\end{document}

Output of the above code

F. Pantigny
  • 40,250