1

I would like to display a matrix surrounded by square brackets that has row and column labels outside the square brackets. In LaTeX, I would use blockarray like the following:

\documentclass[preview]{standalone}

\usepackage{blkarray}

\begin{document}
\begin{blockarray}{rcc}
      & C1 & C2 \\
  \begin{block}{r[cc]}
    R1 & -1 &  0 \\
    R2 & -1 &  0 \\
  \end{block}
\end{blockarray}
\end{document}

With the result:

Result with blockarray

I've been able to reproduce something similar in ConTeXt using block matrices, but I can't figure out how to go one step further and align two columns across different blocks:

\starttext

\definemathmatrix[smatrix][left={\left[}, right={\right]}]
\definemathmatrix[omatrix][left={\phantom{[}}, right={\phantom{]}}]

\startformula
\startmatrix
\NC 
\NC 
    \startomatrix[n=2] 
        \NC C1 \NC C2 \NR 
    \stopomatrix
\NR

\NC 
    \startmatrix[n=1] 
        \NC R1 \NR 
        \NC R2 \NR 
    \stopmatrix
\NC
\NC % Need an extra \NC, might be a bug
    \startsmatrix[n=2] 
        \NC -1 \NC 0 \NR
        \NC -1 \NC 0 \NR
    \stopsmatrix
\NR
\stopmatrix
\stopformula

\stoptext

With the result:

Result with matrix

It's actually not too bad, but the column labels are not aligned with the actual columns and this becomes more obvious with more complex column values.

I can hack together alignment using phantom elements, but is there a more general method to get column labels?

2 Answers 2

3

You can create this with the bbordermatrix environment which produces the same result as the normal bordermatrix environment but replaces the braces with brackets.

\starttext

\startformula
    \startbbordermatrix
    \NC    \NC C1 \NC C2 \NR
    \NC R1 \NC -1 \NC 0  \NR
    \NC R2 \NC -1 \NC 0  \NR
    \stopbbordermatrix
\stopformula

\stoptext

Bordermatrix with brackets

1
0

To expand on the answer by Metafox, for those with older installations of ConTeXt, it seems like bbordermatrix was also defined by Hans Hagen on the mailing list. The source can be found here: https://mailman.ntg.nl/pipermail/ntg-context/2017/089694.html.

For the sake of convenience, here is a copy:

\unprotect

\unexpanded\def\math_border_matrix#1#2#3% adapted plain
   {\begingroup
    %
    \pushmacro\cr
    %
    \setbox\scratchbox\hbox\bgroup
      \mr B%
    \egroup
    \scratchdimenone\wd\scratchbox
    %
    \scratchdimentwo.2\bodyfontsize
    %
    \mathsurround\zeropoint
    %
    \setbox\scratchboxone\vbox\bgroup
      \def\cr{%
        \crcr
        \noalign\bgroup
          \kern\scratchdimentwo
          \global\let\cr\endline
        \egroup
      }%
      \ialign{%
        \startimath\alignmark\alignmark\stopimath
        \hfil
        \kern\dimexpr\scratchdimentwo+\scratchdimenone\relax
        \aligntab
        \thinspace
        \hfil
        \startimath\alignmark\alignmark\stopimath
        \hfil
        \aligntab
        \aligntab
        \quad
        \hfil
        \startimath\alignmark\alignmark\stopimath
        \hfil
        \crcr
        \omit
        \strut
        \hfil
        \crcr
        \noalign{\kern-\baselineskip}%
        #3\crcr
        \omit
        \strut
        \cr
      }%
    \egroup
    \setbox\scratchboxtwo\vbox\bgroup
      \unvcopy\scratchboxone
      \global\setbox\globalscratchbox\lastbox
    \egroup
    \setbox\scratchboxtwo\hbox\bgroup
      \unhbox\globalscratchbox
      \unskip
      \global\setbox\globalscratchbox\lastbox
    \egroup
    \setbox\scratchboxtwo\hbox\bgroup
      \startimath
      \kern\dimexpr\wd\globalscratchbox-\scratchdimenone\relax
      \left#1\relax
      \kern-\wd\globalscratchbox
      \global\setbox\globalscratchbox\vbox\bgroup
        \box\globalscratchbox
        \kern\scratchdimentwo
      \egroup
      \vcenter\bgroup
        \kern-\ht\globalscratchbox
        \unvbox\scratchboxone
        \kern-\baselineskip
      \egroup
      \thinspace
      \right#2\relax
      \stopimath
    \egroup
    \null
    \thickspace
    \vbox\bgroup
      \kern\ht\globalscratchbox
      \box\scratchboxtwo
    \egroup
    %
    \popmacro\cr
    %
    \endgroup}

\unexpanded\def\bordermatrix {\math_border_matrix()}
\unexpanded\def\bbordermatrix{\math_border_matrix[]}

\protect

\starttext

A plain border matrix:

\startformula
\bordermatrix{
    a & b & c & d \cr
    e & f & G & h \cr
    i & j & k & l \cr}
\stopformula

\startformula
\bbordermatrix{
    a & b & c & d \cr
    e & f & G & h \cr
    i & j & k & l \cr}
\stopformula

\stoptext

You must log in to answer this question.

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