\xpatchcmd 定義套用於未定義指令時的宏

\xpatchcmd 定義套用於未定義指令時的宏

我偶然發現了這個功能(這是一個更大問題的一部分,但我將其分為兩個問題):

假設我們想要使用\xpatchcmd(from xpatch) 或\patchcmd(from etoolbox) 並將這些命令(例如,意外地)應用到未定義的命令序列上,那麼這種修補當然會失敗。

但是,以下程式碼運行並產生\someundefinedmacroas \relax,其中\patchcmdfor\someotherundefinedmacro可以工作,但不會將其返回為\relax(它仍然是未定義的)

我必須承認,在嘗試修補之前應該檢查這一點。或\GenericError在修補巨集的失敗分支中使用一些。

這裡有一個簡短的 MWE

\documentclass{article}

\usepackage{xpatch}

% Patching an undefined macro... must fail as patching operation, of course (there's nothing to patch ;-))
\xpatchcmd{\someundefinedmacro}{Ministry of Silly Walks}{Mr. Gumby}{\typeout{Well, this is strange}}{\typeout{Nobody expects the Spanish Inquisition}}

%This does 'nothing'  
\patchcmd{\someotherundefinedmacro}{Ministry of Silly Walks}{Mr. Gumby}{\typeout{Well, this is strange}}{\typeout{Nobody expects the Spanish Inquisition}}


%\show\someundefinedmacro  -> relax
\begin{document}
\someundefinedmacro%  Does nothing, since it's \relax'ed

 % It will fail here (since `\someotherundefinedmacro` is really undefined
\someotherundefinedmacro%  
\end{document}

答案1

是的,作為處理的一部分,檢查的巨集仍然定義為\relax;因此,如果

\xpatchcmd{\foo}{<search>}{<replace>}{<true>}{<false>}

嘗試,\foo\foo•項目符號代表空格),如果不是一開始,\\foo則保持定義為放鬆。\\foo•

這是錯誤的,將在下一個版本中修復xpatch

\foo然而,該巨集將保持定義狀態\relax,就像嘗試對未定義的巨集進行修補一樣:為了避免這種情況,需要更改套件的太多內容。將在文件中新增警告。

但是,在修補巨集之前,通常應檢查該巨集是否存在且可修補。

相關內容