`ifnum` 引数内のコマンド呼び出しを無視する

`ifnum` 引数内のコマンド呼び出しを無視する

またはのいずれかを引数として\yesifone受け取り、これら 2 つのケースに応じて異なる動作をするコマンドがあります。 コマンド の出力をそれに渡したいのですが、 は出力を生成しない他のコマンドに依存しています(私の具体的なケースでは、変数を設定します)。 これを機能させようとする現在の試みは失敗します。おそらく、 には比較に数値 だけではなくが含まれているためでしょう。呼び出しを無視して出力テキストのみをチェックするなど、これを機能させるにはどうすればよいのでしょうか。10\one\one\blahifnum\blah{}1\blah{}

つまり、次のように出力したいのですyes

\documentclass{minimal}

\begin{document}

\newcommand{\blah}{}

\newcommand{\one}{
\blah{}
1
}

\newcommand{\yesifone}[1]{%
    \ifnum1=#1\relax{yes}\else{no}\fi
}

\yesifone{\one} % error: Missing number, treated as zero.

\end{document}

答え1

コードで\yesifone{\one}トークンを生成します
\yesifone{1\one}2

これは に展開されます。
\ifnum112=12\one\relax{1y11e11s11}2\else{1n11o11}2\fi

\ifnumの2番目のTeX-⟨に属するトークンを収集するとき番号⟩量、つまり後ろの数字、トークンは拡張され、次のようなものが得られます。=12\one

\ifnum112=1210\blah{1}21011210\relax{1y11e11s11}2\else{1n11o11}2\fi

すぐ後ろのスペース トークンは削除され、拡張されて、置換テキストが空になるため消えます。つまり、次のようになります。10=12\blah

\ifnum112=12{1}21011210\relax{1y11e11s11}2\else{1n11o11}2\fi

\ifnumそこで、の2番目のTeX-⟨に属するトークンを収集する時点で番号⟩量、つまり の後ろの数字がない場合、TeXは空の括弧グループ、つまり明示的な文字トークンとを見つけます。これは、有効なTeX-⟨を形成するトークンのシーケンスの始まりを形成することは絶対にありません。=12{1}2番号⟩-量。


TeX が目と消化管を持つ獣であるという Knuth の喩えについて考えてみましょう。

  • 拡張可能なトークンの拡張は、たとえば、パラメータ テキストや -assignment の置換テキストを形成するトークンの場合のように拡張が抑制されない限り、ある種の逆流プロセスによって食道内で行われます\def。(LaTeX の\newcomand\NewDocumentCommandなどは、 を呼び出すための洗練されたラッパーです\def。)
  • 割り当ては胃の中で行われます。

したがって、タイプ設定以外の作業を行うために胃を必要とする「変数」への値の割り当てなどのタスクを、拡張可能なトークンの食道/拡張で十分であり、食道の後ろの消化器官がタイプ設定のみに使用されるタスクから分離します。

\documentclass{minimal}

% Introduce/initialize things used as variable whose value is
% to be set via assignments that take place in the stomach:
\newcommand\VariableRelatedToBlah{}

% Define macros for tasks that involve digestive organs behind the gullet for 
% for non-typesetting-tasks, e.g.,_setting_ values of variables via assignments:
\newcommand\SetValueOfVariableRelatedToBlah[1]{%
  \def\VariableRelatedToBlah{#1}%
}

% Define macros for tasks that involve only the gullet, e.g., 
% _retrieving_ values of variables, or additionally to the gullet
% involve digestive organs behind the gullet only for typesetting:
\newcommand\RetrieveValueOfVariableRelatedToBlah{%
  \VariableRelatedToBlah
}
\newcommand\firstofone[1]{#1}%
\newcommand{\yesifone}[1]{%
  \ifnum1=\expandafter\firstofone\expandafter{\number#1} yes\else no\fi
}

\begin{document}

% Now you can keep work that involves the stomach for non-typesetting
% separated from work where the gullet is sufficient/where tokens
% delivered by the gullet can directly be used for typesetting:

\SetValueOfVariableRelatedToBlah{0}%
\yesifone{\RetrieveValueOfVariableRelatedToBlah}

\SetValueOfVariableRelatedToBlah{1}%
\yesifone{\RetrieveValueOfVariableRelatedToBlah}

\end{document}

ここに画像の説明を入力してください

関連情報