如果我调用 \MAT{A, 1, 2, 3, 4},我想要访问主体内部的 \arg[n] 列表而不是 \A[n]?

如果我调用 \MAT{A, 1, 2, 3, 4},我想要访问主体内部的 \arg[n] 列表而不是 \A[n]?

我想使用以下代码设置矩阵:

\newcommand{\MAT}[1]{
\setsepchar{,}
\readlist\arg{#1}
\arg[1]=
\begin{pmatrix}
\arg[2]& \arg[3] \\
\arg[4]& \arg[5]
\end{pmatrix}
}

如果我调用 \MAT{A, 1, 2, 3, 4},那么在主体内部可以访问的不是列表 \arg[k],而是 \A[k]。

我试过:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{listofitems}

\newcommand{\MAT}[1]{
\setsepchar{,}
\readlist\arh{#1}
\def\arg{\arh[1]}
\readlist\arg{#1} % I want here instead of string "arg" ==>>> the value in \arh[1]
\arg[1]=
\begin{pmatrix}
\arg[2]& \arg[3] \\
\arg[4]& \arg[5]
\end{pmatrix}
}

\begin{document}
$\MAT{A, 1, 2, 3, 4}$
$\A[1]$
\end{document}

答案1

我不确定这有多大用处,但你是法官。

ID不是使用这样一个简短的名称,因为您可能会覆盖重要的命令。

\documentclass{article}
\usepackage{amsmath,xparse}

\ExplSyntaxOn

\NewDocumentCommand{\MAT}{mm}
 {
  \clist_clear_new:c { l__lindomar_matrix_#1_clist }
  \clist_set:cn { l__lindomar_matrix_#1_clist } { #2 }
  \cs_set_protected:cpn { #1 } { \PRINTMAT { #1 } } %%%% <--- risky
 }

\NewDocumentCommand{\PRINTMAT}{mo}
 {
  \IfValueTF { #2 }
   {% just one entry
    \clist_item:cn { l__lindomar_matrix_#1_clist } { #2 }
   }
   {% the whole matrix
    \begin{pmatrix}
    \clist_item:cn { l__lindomar_matrix_#1_clist } { 1 } &
    \clist_item:cn { l__lindomar_matrix_#1_clist } { 2 } \\
    \clist_item:cn { l__lindomar_matrix_#1_clist } { 3 } &
    \clist_item:cn { l__lindomar_matrix_#1_clist } { 4 }
    \end{pmatrix}
   }
 }

\ExplSyntaxOff

\MAT{A}{1,2,3,4}

\begin{document}

\[
A=\A \qquad A_{12}=\A[2]
\]

\end{document}

在此处输入图片描述

相关内容