\bordermatrix 使用方括号 [ ] 代替圆括号 ( )

\bordermatrix 使用方括号 [ ] 代替圆括号 ( )

我有以下代码:

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

但是这段代码会创建一个矩阵(),我希望它与[] 我如何做到这一点?

答案1

几乎直接修改\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}

我对\left(和采取了行动\right),但也将赋予的值更改为更\@tempdima小,因为括号比圆括号更窄。

如果你不想让代码这么混乱,那就这样做

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

做完全相同的改变。

在此处输入图片描述

略有不同的版本,其中设置了边框条目\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}

在此处输入图片描述

答案2

继 Herbert Voss 的出色mathmode文档,您可以重新定义该bordermatrix命令以采用确定分隔符的可选参数

在此处输入图片描述

请注意,Herbert 还详细说明了该amsmath提供相同或更好的宏/环境

\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}

答案3

该软件包nicematrix提供了编写表格的工具和数学工具。使用该软件包,使用方括号编写矩阵的外部行和列没有任何问题。

\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}

上述代码的输出

相关内容