환경 변수를 입력하는 \new명령

환경 변수를 입력하는 \new명령

ConcProg 클래스를 사용하여 학생 연주회를 위한 콘서트 프로그램을 만듭니다. 어떤 곡이 연주되는지 반복되는 부분이 많아서 단축어를 만들고 싶습니다.

그래서 이것보다는

\begin{composition}{Folk Song}{}{Twinkle Twinkle Little Star}{Student Name}
    \end{composition}

이 같은.

\newcommand{\twinkle}{{Folk Song}{}{Twinkle Twinkle Little Star}}
\newcommand{\studentname}{{Student Name}}

\begin{composition}\twinkle\studentname
    \end{composition}

그러나 \newcommand는 특정 새 명령에 대한 인수로 사용될 때 여러 대괄호 세트만 선호하는 것 같습니다. 내가 알 수 있는 한, 독립형 대괄호(예: 특정 \newcommand에 대한 인수나 \newcommand 내에 배치된 명령과 관련 없는 대괄호)는 무시합니다. 이 사용자 정의 환경의 경우 오류가 발생합니다.

사용자 정의 환경을 사용하지 않는 또 다른 예는 다음과 같습니다.

%standard input 
\newcommand{\wbalTwo}[2] {
  This is the Wikibook about LaTeX
  supported by #1 and #2}
\item \wbalTwo{John Doe}{Anthea Smith}
%trying to use new command to input args. In this instance, it seems to ignore the internal braces and treats both internally-braced items as a single text string. 
\newcommand{\wbalTwo}[2] {
  This is the Wikibook about LaTeX
  supported by #1 and #2}
\newcommand{\passargs}

그렇다면 동일한 출력을 갖도록 하는 쉬운 방법이 있습니까?

답변1

지금까지 알려주신 내용에 따르면 문제를 재현할 수 없으므로 문제의 원인을 추적/추적하는 것은 어느 정도 추측과 관련이 있습니다.

먼저 몇 가지 일반적인 사항을 말씀드리겠습니다. 제 추측이 옳고 제 의견이 유용하길 바랍니다.

  1. 토큰 스트림에서 매크로나 환경에 대한 인수를 수집할 때 확장은~ 아니다트리거되었습니다.
  2. 존재하는 경우 (La)TeX는 중괄호의 가장 바깥쪽 수준을 제거합니다.전체확장 중에 해당 인수를 대체 텍스트에 삽입할 때 인수.

CTAN에서 ConcProg 클래스를 "파헤친" 후(https://ctan.org/pkg/concprog), 환경에서 composition다음 네 가지 인수를 처리하는 것으로 나타났습니다.

  1. ⟨작가⟩
  2. ⟨생년(및 사망)⟩
  3. ⟨작품명⟩
  4. ⟨선택적 설명⟩

따라서

\begin{composition}{Folk Song}{}{Twinkle Twinkle Little Star}{Student Name}
⟨whatsoever environment-body⟩
\end{composition}
  • -environment 의 첫 번째 인수는 composition다음과 같습니다 Folk Song.
  • -environment 의 두 번째 인수 composition는 비어 있습니다.
  • -environment 의 세 번째 인수는 composition다음과 같습니다 Twinkle Twinkle Little Star.
  • -environment 의 네 번째 인수는 composition다음과 같습니다 Student Name.

함께 있는 동안

\begin{composition}\twinkle\studentname
\end{composition}
  • -environment 의 첫 번째 인수는 composition다음과 같습니다 \twinkle.
  • -environment 의 두 번째 인수는 composition다음과 같습니다 \studentname.
  • -environment 의 세 번째 인수는 composition다음과 같습니다 \end.
  • -environment 의 네 번째 인수는 composition다음과 같습니다 composition.

확장이 되기 때문이죠.~ 아니다LaTeX가 매크로나 환경에 대한 인수를 수집할 때 트리거됩니다.

LaTeX가 -environment의 기초가 되는 매크로를 수행/확장할 때 인수가 삽입되는 방식에 따라 composition이러한 인수 가져오기 방식은 모든 종류의 이상한/잘못된/문제가 있는 동작으로 이어질 수 있습니다.

확장을 관리하는 composition이라는 -environment 의 사용자 정의 변형을 제공할 수 있습니다 .mycomposition

실제로 환경은 어떤 인수도 전혀 처리하지 않습니다. 대신 환경 본문의 다음 토큰이 확장 가능한지 여부를 확인합니다. 그렇다면 확장 불가능한 토큰을 찾을 때까지 확장이 진행됩니다. (중괄호 여는 중괄호 {는 확장 가능한 토큰이 아니라는 점에 유의하세요. ;-)) 그런 다음 인수를 가져옵니다. 4개의 인수를 얻기 위해 이 작업을 4번 수행합니다. 그런 다음 내부적으로 다음 네 가지 인수를 -environment의 기본 매크로에 전달합니다 composition.

\documentclass{ConcProg}

\makeatletter
\newcommand\UD@CheckWhetherNextExpandable[2]{%
  \def\UD@reserved@a{#1}%
  \def\UD@reserved@b{#2}%
  \UD@@CheckWhetherNextExpandable
}%
\newcommand\UD@@CheckWhetherNextExpandable{%
  \futurelet\UD@reserved@c\UD@@@CheckWhetherNextExpandable
}%
\newcommand\UD@@@CheckWhetherNextExpandable{%
  \ifx\UD@reserved@c\@sptoken\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {\afterassignment\UD@@CheckWhetherNextExpandable\let\UD@reserved@c= }%
  {%
    \expandafter\ifx\noexpand\UD@reserved@c\UD@reserved@c
      \expandafter\UD@reserved@b
    \else
      \expandafter\UD@reserved@a
    \fi
  }%
}%
\newcommand\UD@ExpandUntilFirstUnexpandableAndAddToUD@reserved@d[1]{%
   \ifx\relax#1\relax\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
   {\expandafter\endgroup\UD@reserved@d}%
   {\UD@CheckWhetherNextExpandable
      {\expandafter\UD@@CheckWhetherNextExpandable}%
      {\expandafter\UD@AddNextTo\expandafter{\@gobble#1}\UD@reserved@d}%
   }%
}%
\newcommand\UD@AddNextTo[3]{%
  % \edef..\the\toks@-route prevents halving of hashes.
  \toks@\expandafter{#2{#3}}%
  \edef#2{\the\toks@}%
  \UD@ExpandUntilFirstUnexpandableAndAddToUD@reserved@d{#1}%
}%
\newcommand\UD@ExpandAndAccumulateKArgsAndPassTo[2]{%
  \begingroup
  \def\UD@reserved@d{#2}%
  \expandafter\UD@ExpandUntilFirstUnexpandableAndAddToUD@reserved@d
  \expandafter{\romannumeral\number\number#1 000}%
}%
\newenvironment{mycomposition}%
               {\UD@ExpandAndAccumulateKArgsAndPassTo{4}{\composition}}%
               {\endcomposition}%
\makeatother


\newcommand{\twinkle}{{Folk Song}{}{Twinkle Twinkle Little Star}}
\newcommand{\studentname}{{Student Name}}


\begin{document}

\begin{composition}{Folk Song}{}{Twinkle Twinkle Little Star}{Student Name}%
environment body
\end{composition}

\begin{mycomposition} \twinkle\studentname
environment body
\end{mycomposition}

\begin{mycomposition} \twinkle {Student Name}
environment body
\end{mycomposition}

\begin{mycomposition} {Folk Song}{}{Twinkle Twinkle Little Star} \studentname
environment body
\end{mycomposition}

\begin{mycomposition}{Folk Song}{}{Twinkle Twinkle Little Star}{Student Name}
environment body
\end{mycomposition}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보