Latex3 等效 csname endcsname

Latex3 等效 csname endcsname

我想知道如何在 Latex3 中建立動態命名巨集(在循環中)。 LaTeX2e 一切正常。

我用 expl3 找出與該程式碼等效的內容

\stepcounter{N}
\expandafter\def\csname toto@num\Alph{N}\endcsname{#1}

我查看了 expl3 文檔,他們指出\cs:w \cs_end但我不知道如何使其工作。

先致謝

答案1

expl3 的要點之一是讓您從複雜的結構中解脫\expandafter出來\csname。作為替代,它為您提供了參數修飾符,可以在將參數傳遞給基底函數之前對其進行操作。正如 @egreg 所說,csname 建構的修飾符是c。如果N您希望使用建構的 csname,則在程式碼中使用此名稱(而不是)。

例如,在 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 命令,它們使這一切發生(即\csname...\endcnsame在 2e 中),因此\exp_after:wN您可以使用它們以 2e 方式編程 1-2-1,但 expl3 的要點實際上是您不再需要這樣做,所以他們只是在內部建立機制。

答案2

您可以使用以下c變體:

\cs_new_protected:cpn { toto_num_ \Alph{N} } { <whatever> }

相關內容