# Border matrix with multiple indices on top and bottom

How can I write a matrix where there is an index at the top and also at the bottom? \bordermatrix doesn't seem to have an option for multiple indices.

• Look for the package blkarray; there are examples on this site. Jun 18, 2012 at 13:38
• @ashpool The young package nicematrix does a very good job very easily. Aug 30, 2020 at 12:02

An example using the blkarray package:

\documentclass[12pt]{report}
\usepackage{blkarray}
\usepackage{amsmath}

\begin{document}

$\begin{blockarray}{cccccc} c_1 & c_2 & c_3 & c_4 & c_5 \\ \begin{block}{(ccccc)c} 1 & 1 & 1 & 1 & 1 \\ 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 \\ \end{block} d_1 & d_2 & d_3 & d_4 & d_5 \\ \end{blockarray}$

\end{document}


A TikZ based solution! By means of the comment percusse suggested, it is possible to make the foreach loop simpler: it iterates on rows/columns of the names that matrix of math nodes gives to each entry of the matrix and reuses that number also in the border index.

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}

\begin{align*}
P&=
\begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ]
\matrix(m)[matrix of math nodes,left delimiter=(,right delimiter=),inner sep=4pt,ampersand replacement=\&]
{
x_1 \&  y_1 \& s_1 \&  z_1 \\
x_2 \&  y_2 \& s_2 \&  z_2 \\
x_3 \&  y_3 \& s_3 \&  z_3 \\
x_4 \&  y_4 \& s_4 \&  z_4 \\
};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \s in {1,2,...,4}{
% bottom index
\node[red,shift=(m-4-\s.south),yshift=-0.4cm](0,0) {$a_{\s}$};
}
\foreach \n in {1,2,...,4}{
% top index
\node[blue,shift=(m-1-\n.north),yshift=0.4cm](0,0) {$b_{\n}$} ;
}
\end{tikzpicture}
\end{align*}

\end{document}


Following the strategy, it is also possible to define indices on the matrix's left or right side:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}

\begin{align*}
P&=
\begin{tikzpicture}[baseline=-\the\dimexpr\fontdimen22\textfont2\relax ]
\matrix(m)[matrix of math nodes,left delimiter=(,right delimiter=),inner sep=4pt,ampersand replacement=\&]
{
x_1 \&  y_1 \& s_1 \&  z_1 \\
x_2 \&  y_2 \& s_2 \&  z_2 \\
x_3 \&  y_3 \& s_3 \&  z_3 \\
x_4 \&  y_4 \& s_4 \&  z_4 \\
};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \s in {1,2,...,4}{
% bottom index
\node[blue,shift=(m-4-\s.south),yshift=-0.4cm,text height=1ex,](0,0) {$a_{\s}$};
}
\foreach \n in {1,2,...,4}{
% right index
\node[green!70!blue,shift=(m-\n-4.west),xshift=1.5cm,text height=1ex,](0,0) {$b_{\n}$} ;
}
\end{tikzpicture}
\end{align*}

\end{document}


The result:

• You can simplify the foreach loop via \node[blue,shift=(m-1-\n.north),yshift=0.4cm](0,0) {$b_{\n}$};. You also don't need align here. Jun 18, 2012 at 14:22
• Thanks for the nice suggestion: it's really smart ;). The align is just a remain of a previous answer, but I kept because maybe the OP needs to insert this matrix inside an math environment. Jun 18, 2012 at 14:30

The package nicematrix provides tools to do such things easily.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\NiceMatrixOptions{code-for-first-row = \color{red},
code-for-first-col = \color{blue},
code-for-last-row = \color{green},
code-for-last-col = \color{magenta}}
$\begin{pNiceArray}{cc|cc}[first-row,last-row=5,first-col,last-col,nullify-dots] & C_1 & \Cdots & & C_4 & \\ L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\ \Vdots & a_{21} & a_{22} & a_{23} & a_{24} & \Vdots \\ \hline & a_{31} & a_{32} & a_{33} & a_{34} & \\ L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\ & C_1 & \Cdots & & C_4 & \end{pNiceArray}$
\end{document}


Maybe this work

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\begin{document}
\begin{align*}
\begin{array}{c}
\textcolor{blue}{ \begin{array} {c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{-10pt}}}
y_1 & y_2 & y_3 & y_4 & y_5 & y_6
\end{array}}\\[5pt]
\begin{matrix}
x_1 \\ x_2 \\ x_3\\ x_4 \\ x_5
\begin{bmatrix}
0.9 &  0  &  0  &  0  &  0  &  0 \\
1  & 0.4 & 0.5 &  0  &  0  &  0 \\
0  &  0  &  1  &  0  &  0  &  0 \\
0  &  0  & 0.2 &  1  &  0  &  0 \\
0  &  0  &  0  & 0.4 & 0.5 & 0.2
\end{bmatrix}
\end{array}
\end{align*}
\end{document}


• In the last code edited by percusse, output is not matching with the code. I am also interested in the code that matches the output. Someone, please help me!
– user17403
Aug 9, 2012 at 5:02
• Maybe below answer helps. Aug 30, 2020 at 11:57
• Edited and amended. Sep 6, 2020 at 15:38
\begin{align*} P=\begin{array}{c} \textcolor{blue}{ \begin{array}{c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c@{\hspace{13pt}}c}
y_1 & y_2 & y_3 & y_4 & y_5 & y_6     \end{array}}\\[5pt] \begin{array}{c} a\\b\\c\\d\\e \end{array} \begin{bmatrix}
0.9 &  0  &  0  &  0  &  0  &  0 \\
1  & 0.4 & 0.5 &  0  &  0  &  0 \\
0  &  0  &  1  &  0  &  0  &  0 \\
0  &  0  & 0.2 &  1  &  0  &  0 \\
0  &  0  &  0  & 0.4 & 0.5 & 0.2
\end{bmatrix}   \end{array} \end{align*}