I have a problem with testing whether an argument is empty or not. The following sample code should return "correct" but does not.



\if #1{}


Comparing {}{} or with \empty has the same result. This only occurs with empty strings, as far as I know. Does anyone know why and what would be a proper Solution?


The most robust way of checking for an empty argument is \if\relax\detokenize{#1}\relax as far as I know (most likely egreg shows up and gives a way more detailed answer).

So a robust command to check for emptiness would be:



\myifempty{foo}{empty}{not empty}\\% yields "not empty"
\myifempty{}{empty}{not empty}% yields "empty"
| improve this answer | |
  • 2
    See tex.stackexchange.com/a/20064/4427 – egreg Sep 26 '17 at 15:45
  • 1
    the old-timers folklore says \if\relax\detokenize{#1}\relax was put forward by @HeikoOberdiek, but maybe he was not first (e-TeX was slowly digested by first-timers TeX macro-ers) – user4686 Sep 26 '17 at 15:47
  • @egreg so I was right, there is a way more detailed answer you provided :) – Skillmon likes topanswers.xyz Sep 26 '17 at 15:49

There are better ways to do that, but let me explain what's going wrong.

When you call \ifxnotworking{}, TeX will replace #1 with nothing (the braces are stripped off an undelimited argument), so the input stream will have

•\if {}•correct•\else false•\fi

(where represents a space token). Now \if compares the following unexpandable two tokens (after having performed full expansion until finding two of them, but here there's no expandable token): the two unexpandable tokens are { and } which have different character code, so the test returns false.

Note that \if compares character codes.

| improve this answer | |

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.