在物理包中,\pmqty{}
给出一个带圆括号的矩阵。将其与\xmat*{}{}{}
您可以在第一个参数中选择一个字母,然后选择行数,然后选择列数,然后对它们进行索引。
最小工作示例如下:
\documentclass[12pt,a4paper]{article}
\usepackage{physics}
\begin{document}
$\pmqty{\xmat*{x}{4}{1}}$
\end{document}
输出结果如下:
这文档没有具体说它可以执行上部索引,但我想知道是否有技巧可以让它做到这一点?
否则,我是否必须尝试定义一个\newcommand{\Xmat}
说法来做到这一点?
答案1
\xmatrix
我复制了as的定义\Xmatrix
并将_
其更改为^
。
\documentclass[12pt,a4paper]{article}
\usepackage{xparse}
\usepackage{physics}
\makeatletter
\DeclareDocumentCommand\Xmatrix{ s m m m }
{
{
\newtoks\matrixtoks
\global\matrixtoks = {}
\newcount\rowcount
\newcount\colcount
\loop
\colcount = 0
\advance \rowcount by 1
{
\loop
\advance \colcount by 1
\edef\addtoks{\ifnum \colcount = 1 \else & \fi #2 \IfBooleanTF{#1}{^{\ifnum #3 > 1 \the\rowcount \fi \ifnum #4 > 1 \the\colcount \fi}}{}}
\global\matrixtoks = \expandafter{\the\expandafter\matrixtoks\addtoks}
\ifnum \colcount < #4
\repeat
}
\ifnum \rowcount < #3
\global\matrixtoks = \expandafter{\the\matrixtoks \\ }
\repeat
}
\the\matrixtoks
}
\DeclareDocumentCommand\Xmat{}{\Xmatrix}
\makeatother
\begin{document}
$\pmqty{\xmat*{x}{4}{1}}\cdot\pmqty{\Xmat*{x}{4}{1}}$
\end{document}
答案2
您可以修改\xmatrix
宏来接受^
以下选项*
:
\documentclass[12pt,a4paper]{article}
\usepackage{xparse}
\usepackage{physics}
\newtoks\matrixtoks % not inside the macro!!!!
\newcount\rowcount % not inside the macro!!!!
\newcount\colcount % not inside the macro!!!!
\DeclareDocumentCommand\xmatrix{ s t^ m m m }
{%
{%
\global\matrixtoks = {}%
\loop
\colcount = 0
\advance \rowcount by 1
{%
\loop
\advance \colcount by 1
\edef\addtoks{%
\ifnum \colcount = 1
\else
&%
\fi
#3%
\IfBooleanT{#1}{\sb{\ifnum #4 > 1 \the\rowcount \fi \ifnum #5 > 1 \the\colcount \fi}}%
\IfBooleanT{#2}{\sp{\ifnum #4 > 1 \the\rowcount \fi \ifnum #5 > 1 \the\colcount \fi}}%
}%
\global\matrixtoks = \expandafter{\the\expandafter\matrixtoks\addtoks}%
\ifnum \colcount < #5\relax
\repeat
}
\ifnum \rowcount < #4\relax
\global\matrixtoks = \expandafter{\the\matrixtoks \\ }%
\repeat
}
\the\matrixtoks
}
\DeclareDocumentCommand\identitymatrix{m}
{%
{%
\global\matrixtoks = {}
\loop
\colcount = 0
\advance \rowcount by 1
{%
\loop
\advance \colcount by 1
\edef\addtoks
{%
\ifnum \colcount = 1 \else & \fi
\ifnum \colcount = \rowcount 1 \else 0 \fi
}%
\global\matrixtoks = \expandafter{\the\expandafter\matrixtoks\addtoks}%
\ifnum \colcount < #1\relax
\repeat
}%
\ifnum \rowcount < #1\relax
\global\matrixtoks = \expandafter{\the\matrixtoks \\ }%
\repeat
}%
\the\matrixtoks
}
\begin{document}
$\pmqty{\xmat*{x}{4}{1}}\cdot\pmqty{\xmat^{x}{4}{1}}$
\end{document}
我借此机会修复了宏中的几个严重错误:\newtoks
和\newcount
声明必须在宏之外,否则每次调用时都会浪费一个新的令牌寄存器和两个计数器\xmatrix
(这允许使用简写\xmat
)。我也\identitymatrix
按照同样的思路进行了修复。
请提交错误报告。