3

I created a matrix using easybmat and blkarrary and I would like to label the columns above the matrix. This is the code I am using and what the matrix looks like at the moment:

\usepackage{easybmat}
\usepackage{amsmath}
\usepackage{multirow,bigdelim}
\usepackage{blkarray}
\begin{document}
\[  \mathbb{X} =  \begin{array}{c@{}c} 
\left[   
\begin{blockarray}{cccccc}
\boldsymbol{\beta}  & x_1 & z_1 & \dots & z_{k-1}\\
\begin{BMAT}[3pt]{ccccc}{ccccccccc}
1 & x_{11} & 1 & \dots  & 0 \\   
1 & x_{21} & 1 & \dots  & 0 \\      
\vdots & \vdots &  \vdots & \ddots & \vdots \\     
1 & \vdots & 1 & \dots  & 0\\        
\vdots & & &  & \vdots \\ 
1  & \vdots & 0 & \dots  & 0\\   
1 & \vdots & 0 & \dots  & 0\\   
\vdots & \vdots & \vdots & \ddots & \vdots \\ 
1 & x_{n1} & 0 & \dots  & 0\\ 

\end{BMAT}   
\end{blockarray}

\right] 
&  
\begin{array}{l}  
\\[-17mm] \rdelim\}{4}{6mm}[$ \hspace{2mm} Category \hspace{2mm} 1$] \\ \\ 
\\[17mm]  \rdelim\}{4}{6mm}[$\hspace{2mm} Category \hspace{2mm} k$] \\ \\ 
\end{array} \\[-1ex] 
\end{array}  
\]
\end{document} 

enter image description here

I would like the first row in the current matrix , to be the headings of the columns. Can anyone point out what I am doing wrong please?

  • Welcome to TeX.SX! Unlike any other programming languages, it makes a lot of difference if you change the preamble of your document in terms of the output, such as clashing packages or page settings changed by some detail in the code and so on. That's why we need to have a complete example together with the relevant parts of your preamble included. Otherwise we might not be able to reproduce your problem. In particular, I do not know where BMAT comes from. – JP-Ellis Feb 23 '16 at 9:37
  • Is the empty space under x_1 z_1 ... z_{k-1} intentional? – JP-Ellis Feb 23 '16 at 9:52
  • no, I am trying to place those heading on the lest side ( as headings to the columns of the matrices – user120768 Feb 23 '16 at 9:53
4

Here's what I could come up with. The only thing I don't like about this solution is that I'm having to use \vphantom in the very top row of the matrix so that the .north anchors line up. I'm not sure how to fix that whilst still keeping the baseline of the matrix elements lined up. If anyone else has a solution, please feel free to pitch in.

This little annoyance has been fixed. The height of a node in TikZ can be overwritten with the text height option. This is much better than having to use \vphantom.

I'm not completely familiar with easybmat and blkarray, so instead, I opted to use TikZ' matrix library which allow additional features to be drawn quite easily on a matrix. Here's the code for what you want:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{
  matrix,
  positioning,
  decorations,
  decorations.pathreplacing
}

\begin{document}
\begin{equation*}
  \mathbb{X} = 
\begin{tikzpicture}[baseline=(m.center)]
  \matrix (m) [
      matrix of math nodes,
      left delimiter={[},
      right delimiter={]},
      row 1/.style={nodes={text height=1ex}}
    ] {
      1      & x_{11} & 1       & \cdots & 0      \\
      1      & x_{21} & 1       & \cdots & 0      \\
      \vdots & \vdots &  \vdots & \ddots & \vdots \\
      1      & \vdots & 1       & \cdots & 0      \\
      \vdots &        &         &        & \vdots \\
      1      & \vdots & 0       & \cdots & 0      \\
      1      & \vdots & 0       & \cdots & 0      \\
      \vdots & \vdots & \vdots  & \cdots & \vdots \\
      1      & x_{n1} & 0       & \cdots & 0      \\
    };

    \node [above=1ex of m-1-1] {\(\boldsymbol{\beta}\)};
    \node [above=1ex of m-1-2] {\(x_{1}\)};
    \node [above=1ex of m-1-3] {\(z_{1}\)};
    % \node [above=1ex of m-1-4] {\\(\cdots\)};
    \node [above=1ex of m-1-5] {\(z_{k-1}\)};

    \draw [decoration={brace}, decorate] 
          ([xshift=3ex]m-1-5.north east) -- ([xshift=3ex]m-4-5.south east)
          node [pos=0.5, right=1ex] {Category 1};
    \draw [decoration={brace}, decorate] 
          ([xshift=3ex]m-6-5.north east) -- ([xshift=3ex]m-9-5.south east)
          node [pos=0.5, right=1ex] {Category \(k\)};
  \end{tikzpicture}
\end{equation*}
\end{document}

output

To illustrate specifically the issue with the top row, compare the following two images which shows the node boundaries.

With \vphantom: with vphantom Without \vphantom: without vphantom

For completeness-sake, the original code had:

      right delimiter={]},
    ] {
      1      & \vphantom{1}x_{11} & 1       & \vphantom{1}\cdots & 0      \\
      1      & x_{21} & 1       & \cdots & 0      \\
  • nodes={anchor=center} – percusse Feb 23 '16 at 12:52
  • @percusse I'm aware of that, but that doesn't work as it doesn't align the baselines of the nodes. This results in x_{11} being too high compared to the other symbols. – JP-Ellis Feb 23 '16 at 12:55
  • You can add text depth to account for subscripts – percusse Feb 23 '16 at 12:55
  • @percusse The issue actually was the text height, not the text depth; but thanks for pointing me in right direction :) – JP-Ellis Feb 23 '16 at 13:11
  • You will need text depth eventually later – percusse Feb 23 '16 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.