
\defineThisCommand
我會編寫一個定義新命令的巨集\TheCommand
,然後在本機上呼叫它。程式碼必須是這樣的:
\defineThisCommand{}{
\newcommand \ThisCommand{
#1
}
\ThisCommand
}
\begin{document}
\defineTopicOne{Some text printed}
\defineTopicTwo{and this too}
\end{document}
最後產生輸出
Some text printed
and this too
請問您能建議我解決這個問題的方法嗎?
答案1
答案2
當 TeX 找到控制序列時\foo
,首先要區分兩種情況:TeX 正在執行巨集擴展,或沒有。後一種情況與您的情況無關,因此我們看看前一種情況會發生什麼。案例有:
- 控制序列已被賦予含義,或者
- 控制序列未定義。
在第一種情況下,TeX 使用的含義應該適合上下文:如果\foo
是宏,則它將被擴展,如果它是標記,\chardef
則會列印相應的字符,等等。
在第二種情況下,TeX 將停止,發出錯誤訊息並忽略未定義的標記。
因此你不能像你想做的那樣,除非你之前定義了一堆宏
\newcommand\defineThisCommand[1]{\newcommand\ThisCommand{#1}\ThisCommand}
對於您需要的每個本地命令。這顯然違背了你的意圖:\defineOtherCommand
如果你之前沒有定義它,你就不能擁有它。
使用的策略是使用一個通用宏二論點:
\newcommand\define[2]{%
\newcommand#1{#2}% define the local command
#2% and also use it now
}
這可以讓你說
\define\ThisCommand{whatever}
做你想做的事。