Tell me more ×
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.

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.

share|improve this question
3  
Look for the package blkarray; there are examples on this site. – egreg Jun 18 '12 at 13:38

3 Answers

up vote 5 down vote accepted

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}

enter image description here

share|improve this answer

A TikZ based solution:

\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/\stext in {m-4-1/1,m-4-2/2,m-4-3/3,m-4-4/4}{
% bottom index
\node[red,shift=(\s.south),yshift=-0.4cm](0,0) {$a_{\stext}$} ;
}
\foreach \n/\ntext in {m-1-1/1,m-1-2/2,m-1-3/3,m-1-4/4}{
% top index
\node[blue,shift=(\n.north),yshift=0.4cm](0,0) {$b_{\ntext}$} ;
}
\end{tikzpicture}
\end{align*}

\end{document}

enter image description here

IMPROVEMENT

In his comment percusse suggested a very nice way of make foreachs simpler: here is the previous example enhanced.

\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}
share|improve this answer
1  
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. – percusse Jun 18 '12 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. – Claudio Fiandrino Jun 18 '12 at 14:30

Maybe this work

\documentclas{article}
\usepackage{amsmath}
\usepackage{color}
\begin{document}
\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{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}

enter image description here

share|improve this answer
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 '12 at 5:02

Your Answer

 
discard

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.