
考慮這個 MWE:
\documentclass[11pt]{book}
\def\entrytest{WORD}
\def\entrycurrent{NOWORD}
% \def\entrycurrent{WORD}
\ifx\entrytest\entrycurrent
\newif\ifsomething
\somethingfalse
\fi
\ifx\entrytest\entrycurrent
\ifsomething\typeout{Yes}\else\typeout{No}\fi
\fi
\begin{document}
\end{document}
在目前的情況下,我以為它\ifsomething\typeout...
會受到\ifx
包裝它的「保護」;但有些事情不對,因為程式碼停止了:
...
No
! Extra \fi.
l.15 \fi
?
當然,如果\def\entrycurrent{WORD}
所有的\ifx
s 都在“true”分支中運行,那麼一切都會按預期進行。
那麼我應該如何處理\newif
本身可能有條件定義的 s 的使用呢?
答案1
在第二個中\ifx\entrytest\entrycurrent
,\ifsomething
未定義,因此第一的 \fi
匹配\ifx
,而第二個\fi
不合適。
使某些內容成為條件的不是巨集名稱;而是巨集名稱。只是一個令牌定義的並且 相當於原始條件計數之一。
什麼\newif\ifsomething
是
\let\ifsomething\iffalse
\def\somethingtrue{\let\ifsomething\iftrue}
\def\somethingfalse{\let\ifsomething\iffalse}
你可能想要類似的東西
\newif\ifsomething
\somethingtrue
\ifx\entrytest\entrycurrent
\somethingfalse
\fi
\ifx\entrytest\entrycurrent
\ifsomething\typeout{Yes}\else\typeout{No}\fi
\fi
但目前尚不清楚您為什麼要這樣做。