
Latex3 で動的な名前付きマクロ (ループ内) を作成する方法を知りたいです。LaTeX2e ではすべて問題ありません。
私はexpl3でそのコードと同等のものを探しています
\stepcounter{N}
\expandafter\def\csname toto@num\Alph{N}\endcsname{#1}
expl3 のドキュメントを見てみましたが、ポイントは示されています\cs:w \cs_end
が、どのように動作させるかはわかりません。
前もって感謝します
答え1
\expandafter
expl3 の主なポイントの 1 つは、複雑なまたは構造から解放されることです\csname
。代わりに、基本関数に渡される前に引数を操作する引数修飾子が提供されます。@egreg が言ったように、csname 構造の修飾子は です。構築した csname を使用したいときはいつでも、c
コード内で の代わりにこれを使用します。N
例えば2eでは次のようにします
% setup
\def\foo#1{ <do something with #1> }
\expandafter\def\csname toto@num\Alph{N}\endcsname{ <whatever> }
% usage
\expandafter \foo \csname toto@num\Alph{N}\endcsname
または(さらに悪いことに)
%setup
\def\foobar#1#2{ <do something with #1 and #2> }
\expandafter\def\csname toto@num\Alph{N}\endcsname{ <whatever> }
\expandafter\def\csname toto@num\Roman{N}\endcsname{ <whatever else>}
%usage
\expandafter \foobar \csname toto@num\Alph{N}\expandafter\endcsname
\csname toto@num\Roman{N}\endcsname
一方、expl3では、
% base defs
\cs_new_protected:cpn { toto_num_ \Alph{N} } { <whatever> }
\cs_new_protected:cpn { toto_num_ \Roman{N} } { <whatever else> }
\cs_new:Npn \foo:N #1 { <do something with #1> }
\cs_new:Npn \foobar:N #1#2 { <do something with #1 + #2> }
% command variants as needed ..
\cs_generate_variant:Nn \foo:N { c }
\cs_generate_variant:Nn \foobar:NN { cc }
% application ...
\foo:c { toto_num_ \Alph{N} }
\foobar:cc { toto_num_ \Alph{N} } { toto_num_ \Roman{N} }
したがって、2e のセットアップはおそらくわずかに短いですが、使用方法が複雑で読みにくいですが、expl3 では使用方法が明確でシンプルです。
コマンド\cs:w
と は、\cs_end
これらすべてを実現する低レベルの TeX コマンドです (つまり、 2e では\csname
... \endcnsame
)。したがって、これらを と一緒に\exp_after:wN
使用して、2e の方法で 1-2-1 をプログラムできますが、 expl3 のポイントは、実際にはこれを行う必要がなくなったことです。そのため、これらのコマンドは、メカニズムを内部的に設定するためだけに存在します。
答え2
次のバリアントを使用できますc
:
\cs_new_protected:cpn { toto_num_ \Alph{N} } { <whatever> }