26

I have the following code:

\documentclass{article}
\begin{document}
$
M = \bordermatrix{~ & x & y \cr
              A & 1 & 0 \cr
              B & 0 & 1 \cr}
$
\end{document}

But this code makes a matrix with () and i would like it to be with [] How do i do this?

doncherry
  • 54,637
Andreas
  • 443
  • 2
  • 5
  • 7

3 Answers3

29

An almost direct modification of \bordermatrix:

\documentclass{article}
\makeatletter
\def\bbordermatrix#1{\begingroup \m@th
  \@tempdima 4.75\p@
  \setbox\z@\vbox{%
    \def\cr{\crcr\noalign{\kern2\p@\global\let\cr\endline}}%
    \ialign{$##$\hfil\kern2\p@\kern\@tempdima&\thinspace\hfil$##$\hfil
      &&\quad\hfil$##$\hfil\crcr
      \omit\strut\hfil\crcr\noalign{\kern-\baselineskip}%
      #1\crcr\omit\strut\cr}}%
  \setbox\tw@\vbox{\unvcopy\z@\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{\unhbox\@ne\unskip\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{$\kern\wd\@ne\kern-\@tempdima\left[\kern-\wd\@ne
    \global\setbox\@ne\vbox{\box\@ne\kern2\p@}%
    \vcenter{\kern-\ht\@ne\unvbox\z@\kern-\baselineskip}\,\right]$}%
  \null\;\vbox{\kern\ht\@ne\box\tw@}\endgroup}
\makeatother

\begin{document}
$
M = \bbordermatrix{ & x & y \cr
              A & 1 & 0 \cr
              B & 0 & 1 \cr}
$
\end{document}

I acted on \left( and \right), but also changed the value given to \@tempdima, smaller because brackets are slimmer than parentheses.

If you don't want such a messy code, just do

\usepackage{etoolbox}
\let\bbordermatrix\bordermatrix
\patchcmd{\bbordermatrix}{8.75}{4.75}{}{}
\patchcmd{\bbordermatrix}{\left(}{\left[}{}{}
\patchcmd{\bbordermatrix}{\right)}{\right]}{}{}

that does exactly the same changes.

enter image description here

A slightly different version where the border entries are set in \scriptstyle.

\documentclass{article}
\makeatletter
\def\bbordermatrix#1{\begingroup \m@th
  \global\let\perhaps@scriptstyle\scriptstyle
  \@tempdima 4.75\p@
  \setbox\z@\vbox{%
    \def\cr{%
      \crcr
      \noalign{%
        \kern2\p@
        \global\let\cr\endline
        \global\let\perhaps@scriptstyle\relax
      }%
    }%
    \ialign{$\make@scriptstyle{##}$\hfil\kern2\p@\kern\@tempdima
      &\thinspace\hfil$\perhaps@scriptstyle##$\hfil
      &&\quad\hfil$\perhaps@scriptstyle##$\hfil\crcr
      \omit\strut\hfil\crcr
      \noalign{\kern-\baselineskip}%
      #1\crcr\omit\strut\cr}}%
  \setbox\tw@\vbox{\unvcopy\z@\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{\unhbox\@ne\unskip\global\setbox\@ne\lastbox}%
  \setbox\tw@\hbox{$\kern\wd\@ne\kern-\@tempdima\left[\kern-\wd\@ne
    \global\setbox\@ne\vbox{\box\@ne\kern2\p@}%
    \vcenter{\kern-\ht\@ne\unvbox\z@\kern-\baselineskip}\,\right]$}%
  \null\;\vbox{\kern\ht\@ne\box\tw@}\endgroup}
\def\make@scriptstyle#1{\vcenter{\hbox{$\scriptstyle#1$}}}
\makeatother

\begin{document}
$
M = \bbordermatrix{
   & x & y \cr
 A & 1 & 0 \cr
 B & 0 & 1 \cr
}
$
\end{document}

enter image description here

egreg
  • 1,121,712
  • Is it possible to also patch the border text to be \scriptstyle? I.e. to replace $##$ with $\scriptstyle ##$ or to insert a \scriptsize after \ialign, e.g. \ialign{\scriptsize $##$ ...? I couldn't get it working with neither etoolbox nor xpatch. – Rasmus Apr 05 '17 at 17:40
  • 1
    @Rasmus Yes, it's possible. Added the hack, but not with xpatch, sorry. – egreg Apr 05 '17 at 21:23
20

Following Herbert Voss' excellent mathmode document, you can redefine the bordermatrix command to take an optional argument that determines the delimeters

enter image description here

Note that Herbert also details that the amsmath package offers the same or better macros/environments

\documentclass{article}
\makeatletter
\newif\if@borderstar
\def\bordermatrix{\@ifnextchar*{%
\@borderstartrue\@bordermatrix@i}{\@borderstarfalse\@bordermatrix@i*}%
}
\def\@bordermatrix@i*{\@ifnextchar[{\@bordermatrix@ii}{\@bordermatrix@ii[()]}}
\def\@bordermatrix@ii[#1]#2{%
\begingroup
\m@th\@tempdima8.75\p@\setbox\z@\vbox{%
\def\cr{\crcr\noalign{\kern 2\p@\global\let\cr\endline }}%
\ialign {$##$\hfil\kern 2\p@\kern\@tempdima & \thinspace %
\hfil $##$\hfil && \quad\hfil $##$\hfil\crcr\omit\strut %
\hfil\crcr\noalign{\kern -\baselineskip}#2\crcr\omit %
\strut\cr}}%
\setbox\tw@\vbox{\unvcopy\z@\global\setbox\@ne\lastbox}%
\setbox\tw@\hbox{\unhbox\@ne\unskip\global\setbox\@ne\lastbox}%
\setbox\tw@\hbox{%
$\kern\wd\@ne\kern -\@tempdima\left\@firstoftwo#1%
\if@borderstar\kern2pt\else\kern -\wd\@ne\fi%
\global\setbox\@ne\vbox{\box\@ne\if@borderstar\else\kern 2\p@\fi}%
\vcenter{\if@borderstar\else\kern -\ht\@ne\fi%
\unvbox\z@\kern-\if@borderstar2\fi\baselineskip}%
\if@borderstar\kern-2\@tempdima\kern2\p@\else\,\fi\right\@secondoftwo#1 $%
}\null \;\vbox{\kern\ht\@ne\box\tw@}%
\endgroup
}
\makeatother


\begin{document}
$
M = \bordermatrix[{[]}]{%
            & x & y \cr
          A & 1 & 0 \cr
          B & 0 & 1 }
$

$
M = \bordermatrix[\{\}]{%
            & x & y \cr
          A & 1 & 0 \cr
          B & 0 & 1 }
$
\end{document}
cmhughes
  • 100,947
11

The package nicematrix provides tools to compose tabulars and mathematical tools. With that package, there is no problem to compose exterior rows and columns for a matrix with square brackets.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$
M = 
\begin{bNiceMatrix}[first-row,first-col]
& x & y \\
A & 1 & 0 \\
B & 0 & 1 
\end{bNiceMatrix}
$
\end{document}

Output of the above code

F. Pantigny
  • 40,250