\@ifnextchar の挙動がおかしい

\@ifnextchar の挙動がおかしい

以下の MWE をコンパイルすると、最初の MWE\@ifnextchar,は常に false と評価されますが、これは最後のケースでは明らかに間違っています。ただし、2 番目 ( 内\punctuationcheck) は正常に動作します。同じ条件が False と評価されてから True と評価されるのはなぜでしょうか?

\documentclass{article}

\makeatletter
\newcommand{\punctuationcheck}{%
\@ifnextchar){I see a parens\relax}{%
\@ifnextchar]{I see a square bracket\relax}{%
\@ifnextchar.{I see a period\relax}{%
\@ifnextchar,{I see a comma\relax}{I didn' t see anything\ }}}}%
}
\newcommand{\ie}{\textit{i.e.}\@ifnextchar,{I saw a comma\relax}{I didn't see a comma,}\punctuationcheck}
\makeatother

\begin{document}
\ie) test

\ie] test

\ie. test

\ie, test
\end{document}

これをコンパイルすると次の結果が出力されます:

つまり、カンマは見ませんでしたが、括弧は見ました)テスト

つまり、カンマは見当たらず、角括弧が見える]テスト

つまり、カンマではなくピリオドが見えます。テスト

つまり、カンマが見えなかった、カンマが見えた、テスト

答え1

の構文\@ifnextchar

\@ifnextchar<test-token>{<true>}{<false>}<token>

<test-token>が と同じ場合<token><true>コードが挿入され、そうでない場合は<false>が挿入され、入力ストリームは次のいずれかになります。

<true><token>

または

<false><token>

<token>この段階では は削除されないことに注意してください。これを使用する正しい方法は\@ifnextchar<test-token>{<true>}{<false>}、 をマクロ コードの末尾の部分として使用することです。そうすれば、 は<token>マクロに続くものになります。

代わりに、コードでは<token>が既に指定されており であるため\punctuationcheck、TeX は常に<false>コード を使用するようになります。

解決:

\newcommand{\ie}{%
  \textit{i.e.}%
  \@ifnextchar,%
    {I saw a comma}%
    {I didn't see a comma,\punctuationcheck}%
}

関連情報