I've been using Herbert's answer in Check for empty macro argument to check if an argument is empty. I was wondering if it is possible to expand Herbert's solution to also detect if a macro argument is \empty. Failing this, I was hoping to learn a way to set a macro in so that \ifempty will behave like I want. The following code shows that \ifempty correctly detects an empty macro argument, but does not consider \empty to be an empty argument. Then, I give a couple of unsuccessful attempts to define an empty macro.




\ifempty{}{1. empty}{1. nonempty}

\ifempty{\empty}{2. empty}{2. nonempty}

\ifempty{\z}{3. empty}{3. nonempty}

\ifempty{\z}{4. empty}{4. nonempty}

  • Can you expand on exactly what is required for 'empty macros'. For example, are we looking for exactly one expansion or is it 'exhaustive': what should \def\a{\b}\def\b{}\ifempty{\b} yield? Can we assume one token in the input for a command? Is \def\a#1{stuff}\ifempty{\a} a danger? – Joseph Wright Dec 26 '13 at 9:39
  • I wonder if this is a duplicate of tex.stackexchange.com/questions/110726/… – StrongBad Dec 26 '13 at 11:28
  • @JosephWright Some context: I have been using the \ifempty macro within commands, and it has met my needs. Recently I wanted to test if a string option from kvoptions was empty. According to the documentation, "The initial contents of the macro can be given by the first optional argument <init>. The default is empty." It seems to me that "empty" means \@empty, and I would like to be able to test this. For my particular needs, the command does not need to be particularly robust, and a simple elaboration to the the case I just described would be nice. – Stirling Dec 26 '13 at 19:22
  • It seems like there should be a command of the form \def\z<???> that will make \z trigger the \ifempty macro. – Stirling Dec 26 '13 at 19:26

Detokenized \empty of course is not empty. Short comment to the four examples.

  1. Is as expected.

  2. Is nonempty as it was said at the beginning.

  3. You are defining \z, after which should be string % \ifempty. The definition is erroneus, but never used. Then there is {3. empty}{3. nonempty} and it is written as an ordinary text.

  4. Now \z is properly defined, but z is of course not empty.

If you want simply compare equivalence of macros, \ifx may be more suitable for you.

| improve this answer | |
  • Would some of the macros from etoolbox help? – daleif Dec 26 '13 at 9:36
  • I can use \ifx to compare an argument to \@empty: this was actually why I was trying to find a command to set a variable in such a way that it triggers \ifempty. I want to use \ifempty{\z} instead of \ifx\z\@empty because I think the former is clearer. – Stirling Dec 26 '13 at 19:46

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.