リストの各要素に新しいコマンドを公開する

リストの各要素に新しいコマンドを公開する

パッケージ(の一部)をlVars使用して定義されたマクロがありますforeachpgfforPGF) として:

\newcommand{\lVars}[1]{\foreach \x [count=\ii] in {#1}{
  \ifnum\ii=1{} \else {,} \fi \lVar[\x]}
}

意図されている使用法は、 のような用語で\exists \lVars{a, b, c, d}、結果 を生成します\exists \lVar[a], \lVar[b], \lVar[c], \lVar[d]。 コマンドは、 という\lVar[a]形式の別のカスタム コマンドですが、すぐにもっと複雑になる可能性があります。次に、新しく定量化された要素を、より大きな式内で\newcommand{\lVar}[1][x]{\textsc{#1}}使用します。\lVar

しかし、私は非常に怠け者です。このマクロで、各リスト要素を などの形式の新しいコマンドとして公開し、それ自体が実際の基礎となる 、 ... に展開されるようにしたいです\lA\lBこれら\lVar[A]の新しいコマンドにより、数式表記が大幅に簡潔になります。これらの新しいコマンドは、 、... がマクロのリスト パラメータで\lVar[B]次に使用されるまで存続します。したがって、全体的な使用状況は次のようになります。ab,lVars

\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...

の本体内\newcommandおよび本体上でさまざまな組み合わせを試してみましたが、うまくいきませんでした。何かアドバイスやヒントはありますか?\defforeach

答え1

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

\ExplSyntaxOn

\NewDocumentCommand{\lVars}{m}
 {
  % cycle through the argument
  \clist_map_inline:nn { #1 }
   {
    % print the variable
    \lVar[##1]
    % if the variable is a, define \lA
    \tl_to_uppercase:n
     {
      \cs_gset_protected:cpn { \c_wright_prefix_tl ##1 }
     }
     { \lVar[##1] }
   }
 }
% We need to store the `l' prefix in a control sequence to hide it from \uppercase
\tl_const:Nn \c_wright_prefix_tl { l }
\ExplSyntaxOff

\newcommand{\lVar}[1][x]{\textsc{#1}}

\begin{document}
\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...
\end{document}

ただし、 の定義は\lC永久に存続します。 の異なるセルで使用する予定がある場合は、arrayグローバル定義が必要です。

ここに画像の説明を入力してください


\lFoo複数文字の変数を受け入れ、次のように定義するバージョン\lVars{foo}:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\lVars}{m}
 {
  % cycle through the argument
  \clist_map_inline:nn { #1 }
   {
    % print the variable
    \lVar[##1]
    % build the name with the first letter uppercase
    \wright_make_var_name:n { ##1 }
    \cs_gset_protected:cpn { \l__wright_var_name_tl } { \lVar[##1] }
   }
 }
\tl_const:Nn \c_wright_prefix_tl { l }
\cs_new:Npn \wright_make_var_name:n #1
 {
  % get the first letter and uppercase it
  \tl_to_uppercase:n
   {
    \tl_set:Nx \l__wright_var_name_tl { \tl_head:n {#1} }
   }
  % add the rest
  \tl_put_right:Nx \l__wright_var_name_tl { \tl_tail:n {#1} }
  % add the prefix
  \tl_put_left:Nx \l__wright_var_name_tl { \c_wright_prefix_tl }
 }
\ExplSyntaxOff
\newcommand{\lVar}[1][x]{\textsc{#1}}

\begin{document}
\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...

Now $\forall\lVars{foo}\;\lFoo=0$
\end{document}

ここに画像の説明を入力してください

関連情報