XParse: 명령을 생성하는 명령

XParse: 명령을 생성하는 명령

expl3SE, 최근에 조금 놀고 있는데 xparse다음과 같은 문제가 발생했습니다. 새 명령을 생성하는 명령을 만들고 싶습니다.이 게시물. 두 번째 명령에 인수가 없으면 이는 그리 어렵지 않습니다. 인수를 허용하는 명령을 생성하는 것은 좀 더 까다롭습니다.

내가 지금까지 얻은 것 :

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand\test{ m}
{
    \NewDocumentCommand#1 {m}
        {My name is \string#1, king of kings}
}

\begin{document}
\test{\ozymandias}
\ozymandias{\manthano}
\end{document}

이는 My name is ozymandias, king of kings올바른 소네트를 제공하지만 내가 원하는 것은 아닙니다. 문제가 어디에 있는지는 알지만 어떻게 해결해야 할지 모르겠습니다 :)

답변1

내부 가 궁금하네요 \string#1.

내부의 새로운 명령은 다음과 같이 구성할 수 있습니다.

\expandafter\NewDocumentCommand\csname #1\endcsname{...}{....}

즉, 새 시퀀스의 이름은 먼저 구성되어야 합니다 \expandafter(먼저 이름이 지정된 다음 \NewDocumentCommand실행됩니다). 전통적인 방식 \newcommand등에 대해서도 동일한 접근 방식을 취해야 합니다 \renewcommand.

그러나 명령은 ! 없이 \test로 사용해야 합니다 .\test{ozymandias}\

##1내부 매크로의 첫 번째 인수에 액세스하려면(원하는 경우) 사용해야 하며 후속 인수에는 번호가 매겨져 있습니다 ##2.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\manthano}{}{Manthano}

\NewDocumentCommand\test{ m}
{%
  \expandafter\NewDocumentCommand\csname #1\endcsname{m}{%
   My name is ##1, king of kings%
   }
}

\begin{document}
\test{ozymandias}
\ozymandias{\manthano}
\end{document}

편집하다:\test와 함께 사용하려면 \명령을 다음에 맞게 조정해야 합니다.

\makeatletter
\NewDocumentCommand\test{ m}
{%
  \expandafter\NewDocumentCommand\csname\expandafter\@gobble\string #1\endcsname{m}{%
   My name is ##1, king of kings%
   }
}
\makeatother

관련 정보