2

I am trying to produce an nxn bmatrix that has both column and row headings. By following this manual with amsmath package and the code below:

\begin{figure}
\[\begin{bmatrix}
a_{11}&a_{12}&\cdots &a_{1n} \\
a_{21}&a_{22}&\cdots &a_{2n} \\
\vdots & \vdots & \ddots & \vdots\\
a_{n1}&a_{n2}&\cdots &a_{nn}
\end{bmatrix}\]
\caption{Correlation Matrix}\label{fig:vulnmatrix}
\end{figure}

I can produce this:

enter image description here

However, I aim at producing a bmatrix with column and row headings like this:

enter image description here

So far I have researched and all the examples I have encountered do not have headings. Can anyone suggest a solution for me?

3
  • 1
    the package blkarray is your friend. similar question was asked many times here.
    – Zarko
    Commented Mar 28, 2018 at 13:05
  • @Zarko can I have the labels to the left ? most examples have them on the right like those one provided in this answer (tex.stackexchange.com/questions/59517/…). My matrix isn't an equation. I did like this (tex.stackexchange.com/a/346979/61339), but in with labels on the left.
    – SyCode
    Commented Mar 28, 2018 at 13:23
  • yes, you can. Werners answer in the first link in you comment show, how to do this.
    – Zarko
    Commented Mar 28, 2018 at 14:09

1 Answer 1

3

A solution with blkarray, and another with gauss:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{blkarray, bigstrut}
\usepackage{gauss}

\newenvironment{mygmatrix}{\def\mathstrut{\vphantom{\big(}}\gmatrix}{\endgmatrix}

 \begin{document}%

 \begin{figure}[!ht]
\[
\begin{blockarray}{ccccc}
 & C_1 & C_2 & \dots & C_n \\
\begin{block}{c[cccc]}
N_1 & a_{11}&a_{12}&\cdots &a_{1n}\bigstrut[t] \\
N_2 & a_{21}&a_{22}&\cdots &a_{2n} \\
 & \vdots & \vdots & \ddots & \vdots\\
N_n & a_{n1}&a_{n2}&\cdots &a_{nn}\bigstrut[b]\\
\end{block}
\end{blockarray}\vspace*{-1.25\baselineskip}
\]
\caption{Correlation Matrix}\label{fig:vulnmatrix}
\end{figure}

\begin{figure}[! htb]
    \[
    \begin{gmatrix}[b]
    \mathllap{N_1\quad}a_{11}&a_{12}&\cdots &a_{1n} \\
     \mathllap{N_2\quad}a_{21}&a_{22}&\cdots &a_{2n} \\
    \vdots & \vdots & \ddots & \vdots\\
     \mathllap{N_n\quad}a_{n1}&a_{n2}&\cdots &a_{nn}\colops
\def\colmultlabel#1{\makebox[1.2em]{$#1$}}
\mult0{C_1}
\mult1{C_2}
\mult3{C_n}
     \end{gmatrix}
    \]
\caption{Correlation Matrix}\label{fig:vulnmatrix}
\end{figure}

\end{document} 

enter image description here

3
  • this works but not exactly since I am using an cloud latex engine (overleaf) which does not support kbordermatrix as stated here (tex.stackexchange.com/questions/249484/…). Thanks :)
    – SyCode
    Commented Mar 28, 2018 at 14:44
  • 1
    @SyCode: But I don't use kbordermatrix!
    – Bernard
    Commented Mar 28, 2018 at 14:54
  • you are absolutely correct @Bernard , sorry for the erroneous comment. I don't know how I got kbordermatrix into my code. Your answer solves my issue well !!! Many thanks.
    – SyCode
    Commented Mar 28, 2018 at 15:10

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