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

\expandafterexpl3의 주요 포인트 중 하나는 복잡 하거나 복잡한 \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 명령입니다(예: 2e에서 \csname... \endcnsame). 따라서 \exp_after:wN이 명령을 사용하여 2e 방식으로 1-2-1을 프로그래밍할 수 있지만 expl3의 요점은 실제로 더 이상 이 작업을 수행할 필요가 없으므로 내부적으로 메커니즘을 설정하는 데에만 사용됩니다.

답변2

다음 변형을 사용할 수 있습니다 c.

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

관련 정보