
當我編譯下面的 MWE 時,第一個\@ifnextchar,
總是評估為 false,這在最後一種情況下顯然是錯誤的。然而,第二個(在\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}%
}