
私は 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
これまでにお話しいただいたことから、問題を再現することはできないため、問題の原因を追跡/トレースすることは、ある程度、推測に頼ることになります。
まず、一般的なコメントをいくつか述べます。私の推測が正しく、私のコメントが役に立つことを願っています。
- トークンストリームからマクロや環境の引数を収集する際には、展開はない引き金になった。
- 存在する場合、(La)TeXは、全体展開時にその引数を置換テキストに挿入するときに引数を使用します。
CTANでConcProgクラスを「掘り起こした」後(https://ctan.org/pkg/concprog) では、環境がcomposition
4 つの引数を処理することがわかりました。
- ⟨著者⟩
- ⟨生年(および死亡年)⟩
- ⟨作曲タイトル⟩
- ⟨オプションの説明⟩
したがって
\begin{composition}{Folk Song}{}{Twinkle Twinkle Little Star}{Student Name}
⟨whatsoever environment-body⟩
\end{composition}
- -environmentの最初の引数は
composition
次のようになります:Folk Song
, - -environmentの2番目の引数
composition
は空になります。 - -environmentの3番目の引数は次
composition
のようになります:Twinkle Twinkle Little Star
, - -environmentの4番目の引数は
composition
次のようになります:Student Name
,
とともに
\begin{composition}\twinkle\studentname
\end{composition}
- -environmentの最初の引数は
composition
次のようになります:\twinkle
, - -environmentの2番目の引数
composition
は\studentname
、 - -environmentの3番目の引数は次
composition
のようになります:\end
, - -environmentの 4 番目の引数
composition
は次のようになりますcomposition
。
これは、拡大がないLaTeX がマクロまたは環境の引数を収集するときにトリガーされます。
LaTeX が - 環境の基になるマクロを実行/展開するときに引数がどのように挿入されるかに応じてcomposition
、この引数取得方法はあらゆる種類の奇妙/誤り/問題のある動作につながる可能性があります。
拡張を処理する、composition
と呼ばれる - 環境のカスタマイズされたバリアントを提供できます。mycomposition
実際には、環境は引数をまったく処理しません。代わりに、環境本体の次のトークンが展開可能かどうかをチェックします。展開可能であれば、展開不可能なトークンが見つかるまで展開が行われます。(中括弧は展開不可能なトークンでは{
ないことに注意してください。 ;-)) 次に、引数を取得します。4 つの引数を取得するために、これを 4 回実行します。次に、内部的に、これらの 4 つの引数を環境の基礎となるマクロに渡します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}