\newcommand 輸入環境變數

\newcommand 輸入環境變數

我使用 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的第一個參數compositionFolk Song
  • -environment的第二個參數composition將為空,
  • -environment的第三個參數compositionTwinkle Twinkle Little Star
  • -environment的第四個參數compositionStudent Name

當與

\begin{composition}\twinkle\studentname
\end{composition}
  • -environment的第一個參數composition\twinkle
  • -environment的第二個參數composition\studentname
  • -environment的第三個參數composition\end
  • -environment的第四個參數composition是: composition

這是因為擴充是不是當 LaTeX 收集巨集或環境的參數時觸發。

根據 LaTeX 執行/擴展環境下的巨集時插入參數的方式composition,這種獲取參數的方式可能會導致各種奇怪/錯誤/有問題的行為。

我可以提供一個composition名為 的環境的自訂變體mycomposition,它負責擴充:

實際上環境根本不處理任何參數。相反,它檢查其環境主體中的下一個標記是否可擴展。如果是,則會進行擴展,直到找到不可擴展的令牌。 (請注意,大括號{不是可擴展的標記。;-))然後它將取得一個參數。它執行此操作四次以獲取 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}

在此輸入影像描述

相關內容