7

I'm using kbordermatrix to put some indices on my matrices. An example would be the following: basic example See the end of the post for the code behind this example.

The problem is that the spacing between the [ bracket of the kbordermatrix and the first "proper" columns seems much too large in comparison with the other matrices, and likewise for the right ].

One way I thought of to fix this is to redefine the left and right delimiters that kbordermatrix uses. It seems to work well; when using

\renewcommand{\kbldelim}{[\hspace{-1ex}}

we get the following result: first attempt That looks much better! However, now we try to do the same thing for the right bracket:

\renewcommand{\kbrdelim}{\hspace{-1ex}]}

But this gives an error:

ERROR: Missing delimiter (. inserted).

--- TeX said ---
<to be read again> 
                   \let 
l.14   }

--- HELP ---
From the .log file...

I was expecting to see something like `(' or `\{' or
`\}' here. If you typed, e.g., `{' instead of `\{', you
should probably delete the `{' by typing `1' now, so that
braces don't get unbalanced. Otherwise just proceed.
Acceptable delimiters are characters whose \delcode is
nonnegative, or you can use `\delimiter <delimiter code>'.

The obvious

\renewcommand{\kbrdelim}{\delimiter \hspace{-1ex}]}

also doesn't work.

Any ideas how to solve this?

A minimal working example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{kbordermatrix}

\begin{document}
\begin{equation*}
  \kbordermatrix{
      & 1 & 2 \\
    1 & 0 & 1 \\
    2 & 1 & 0
  }
  \times
  \begin{bmatrix}
    a & b \\
    c & d
  \end{bmatrix}
  =
  \begin{bmatrix}
    c & d \\
    a & d
  \end{bmatrix}
\end{equation*}
\end{document}

To compile it, you do have to use the kbordermatrix.sty file: CTAN.

Koen
  • 149

2 Answers2

6

Redefining \kbldelim allows you to insert content after the left delimiter, but you're too late in trying to insert content before the right delimiter \kbrdelim via a redefinition. You'll have to patch \kbordermatrix for this (using etoolbox):

enter image description here

\documentclass{article}

\usepackage{amsmath}
\usepackage{kbordermatrix,etoolbox}

\renewcommand{\kbldelim}{[\hspace*{-\arraycolsep}}
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\kbordermatrix}{\right\kbrdelim}{\hspace*{-\arraycolsep}\right\kbrdelim}{}{}

\begin{document}

\[
  \kbordermatrix{
      & 1 & 2 \\
    1 & 0 & 1 \\
    2 & 1 & 0
  }
  \times
  \begin{bmatrix}
    a & b \\
    c & d
  \end{bmatrix}
  =
  \begin{bmatrix}
    c & d \\
    a & d
  \end{bmatrix}
\]

\end{document}
Werner
  • 603,163
4

You can use the package nicematrix. With the environment {bNiceMatrix} of nicematrix, the spaces are, by design, the same as in the environment {bmatrix} of amsmath.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\NiceMatrixOptions{code-for-first-col=\scriptstyle,code-for-first-row=\scriptstyle}
$\begin{bNiceMatrix}[first-col,first-row]
& 1 & 2 \\
1 & 0 & 1 \\
2 & 1 & 0 
\end{bNiceMatrix}
\times
\begin{bmatrix}
a & b \\
c & d 
\end{bmatrix}
=
\begin{bmatrix}
c & d \\
a & b
\end{bmatrix}$
\end{document}
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\NiceMatrixOptions{code-for-first-col=\scriptstyle,code-for-first-row=\scriptstyle}
$\begin{bNiceArray}{cc}[first-col,first-row]
& 1 & 2 \\
1 & 0 & 1 \\
2 & 1 & 0 
\end{bNiceArray}
\times
\begin{bmatrix}
a & b \\
c & d 
\end{bmatrix}
=
\begin{bmatrix}
c & d \\
a & b
\end{bmatrix}$
\end{document}

Output of the code above

The package nicematrix provides also other functionalities for drawing mathematical matrices.

F. Pantigny
  • 40,250
  • Such a great package! Thanks for suggesting it, especially given that kbordermatrix is not available on Overleaf. – psyguy Sep 08 '22 at 11:23