定義指令時為指令新增別名

定義指令時為指令新增別名

我想要一個命令\NewDocumentCommands,它允許使用相同的定義一次定義多個命令。例如:

\NewDocumentCommands { \powerset, \Powerset, \PowerSet } {} { \mathcal{P} }
\NewDocumentCommands { \mat, \bmat } { m } { \begin{bmatrix} #1 \end{bmatrix} }

我天真的嘗試:

\NewDocumentCommand { \NewDocumentCommands } { m m m }
  {
    \seq_set_split:Nnn \l_tmpa_seq { , } { #1 }
    \seq_map_inline:Nn \l_tmpa_seq
      {
        \NewDocumentCommand { ##1 } { #2 } { #3 }
      }
  }

TeX capacity exceeded但是,這會引發錯誤。

另一種嘗試:

\NewDocumentCommand
  { \CommandNewDocument }
  { m m m }
  { \NewDocumentCommand { #3 } { #1 } { #2 } }

\NewDocumentCommand
  { \NewDocumentCommands }
  { > { \SplitList { , } } m m m }
  { \ProcessList { #1 } { \CommandNewDocument { #2 } { #3 } } }

這會引發各種錯誤(Command '\s__tl_stop' already defined.Invalid argument typeCommand '\CommandNewDocument' already defined.


我知道我可以寫

\NewDocumentCommand { \powerset } {} { \mathcal{P} }
\NewDocumentCommand { \Powerset } {} { \powerset }
\NewDocumentCommand { \PowerSet } {} { \powerset }
\NewDocumentCommand { \mat } { m } { \begin{bmatrix} #1 \end{bmatrix} }
\NewDocumentCommand { \bmat } { m } { \mat { #1 } }

或者,或者,

\NewDocumentCommand { \powerset } {} { \mathcal{P} }
\let\Powerset\powerset
\let\PowerSet\powerset
\NewDocumentCommand { \mat } { m } { \begin{bmatrix} #1 \end{bmatrix} }
\let\bmat\mat

\NewDocumentCommands然而,如果定義許多具有多個別名的命令,那麼對我來說專用命令似乎更好。

答案1

這是一個雙倍的問題#。您也可以避免設定順序。

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand { \NewDocumentCommands } { m m m }
  {
    \tl_set:Nn \l_tmpa_tl { #3 }
    \clist_map_inline:nn { #1 }
      {
        \exp_args:NnnV \NewDocumentCommand { ##1 } { #2 } \l_tmpa_tl
      }
  }

\ExplSyntaxOff

\NewDocumentCommands { \powerset, \Powerset, \PowerSet } {} { \mathcal{P} }

\NewDocumentCommands { \mat, \bmat } { m } { \begin{bmatrix} #1 \end{bmatrix} }

\begin{document}

$\powerset\Powerset\PowerSet$

$\mat{a\\b}+\bmat{a\\b}$

\end{document}

在此輸入影像描述

相關內容