How can I use \CheckCommand with robust commands?


LaTeX provides \CheckCommand (only for use in the preamble) so you can ensure that a command has the expected definition:


will work fine without any warnings, but


will cause LaTeX to emit a warning:

LaTeX Warning: Command \hi  has changed.
           Check if current package is valid.

This warning system, however, is thwarted by the LaTeX protection mechanism for robust commands:


raises a warning because \hi is now \protect\hi, not hi. (See What is the difference between Fragile and Robust commands? for example.)

How can I use \CheckCommand with robust commands?


  • 1
    You can look at the documentation of xpatch or regexpatch to see something related to this problem. – egreg Jul 18 '12 at 7:41

I introduce a new command \DefineRobustCommand in place of LaTeX's \DeclareRobustCommand. Why? Well, you can see the reason if you compare the definition of \DefineRobustCommand with that of \DeclareRobustCommand. \DeclareRobustCommand uses a trailing space on the parent command to define a unique auxiliary command, but this is not the issue here. One reason is that \DeclareRobustCommand isn't meant for one-letter commands/definitions like




as seen in the definition of \DeclareRobustCommand, auxiliary commands defined by \DeclareRobustCommand can't distinguish between command symbols.

If you want to "check" a defined command, use \DefineRobustCommand instead of \DeclareRobustCommand. You can use \VerifyCommand to check all LaTeX-defined commands, except those defined by \DeclareRobustCommand.

\VerifyCommand will issue a warning if the command doesn't match previous definition, while \XVerifyCommand will issue an error in that case.

    \@latex@info{Redefining \string#1}%
  \def\am@ch@ckeqcmd{Command \noexpand#3 has changed.
    \MessageBreak Check that there is nothing wrong}%

% Tests

\newcommand\hiy[2]{Hi #1, #2}
\XVerifyCommand\hiy[2]{Hi #1, #2}
% \XVerifyCommand\hiy{Hi, -changed-}

\DefineRobustCommand\hiz[2][x]{Hi (#1***#2)}
\VerifyCommand\hiz[2][x]{Hi (#1***#2)}
% \VerifyCommand\hiz[2][x]{Hi-changed [#1***#2]}
\XVerifyCommand\hiz[2][x]{Hi (#1***#2)}
% \XVerifyCommand\hiz[2][x]{Hi-changed [#1***#2]}

% \| is defined as a delimiter. Defining | as a robust command 
% will not change that:
  % \XVerifyCommand|[2][x]{-changed-\def\x{#1***#2}}
  • Thank Ahmed. I will have to look a bit more closely at this. Could you please let me know how this code came to be: is it just a simple hacked modification of the LaTeX internals, or a more sophisticated rewrite? I was hoping for something a little more "standard", but maybe it is not possible. Will wait a bit to see if a simpler solution comes up before accepting. – mforbes Jul 18 '12 at 5:17
  • 1
    \DefineRobustCommand may qualify as a hack, but not \VerifyCommand and \XVerifyCommand. What you wanted doesn't exist in the LaTeX kernel. I personally would have preferred eTeX's \protected for defining robust commands these days. There are issues with LaTeX's robust commands. For example, they expand under \edef or equivalent milieus. LaTeX resorted to \protect because there wasn't eTeX in the early days of TeX. You can still access optional arguments with \protected if you are familiar with \@testopt. – Ahmed Musa Jul 18 '12 at 18:01

As you probably know by now, \DeclareRobustCommand{\cs}{...} defines two commands: essentially it does


where I denote with a space in the command's name (which is ordinarily not possible). When LaTeX finds this in normal typesetting, \protect is equivalent to \relax, so the expansion of \cs• does that's requested. When LaTeX is writing into auxiliary files, \protect is \noexpand, so the effect is of writing the string \cs followed by a space (when the auxiliary file will be input, that space will not be considered part of the name, but that's irrelevant).

So, in order to check a robust command, you have to look at the expansion of \cs•, but you must know in advance whether the command is robust.

The situation is further complicated by the fact that the expansion of \1 after \DeclareRobustCommand\1{...} is


(again • denotest a space in the name).

In the packages xpatch and regexpatch I do a check on the various possibilities of the replacement text with the macro \xpatch_main:NN (xpatch) or \xpatch_main_check:N (regexpatch). You can see that the list is quite long.

If you know that the command has been defined with \DeclareRobustCommand then

}% (AM)

to be used just like \CheckCommand might be what you want.

A simple-minded version that only checks whether the "command with a trailing space" is defined can be

}% (AM)


The command \xCheckCommand can be used for commands defined by both \newcommand and \DeclareRobustCommand. (AM)

  • @AhmedMusa Thanks for spotting the missing braces. – egreg Jul 18 '12 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.