# Lost global definition after \ifthenelse using package xifthen

I created my MWE with the help of this Q&A.

# MWE:

\documentclass{article}
\usepackage{xifthen}% provides \isempty test

\newcommand{\GlobalDef}[1][]{ %
\ifthenelse{\isempty{#1}} %
{} %
{\xdef\GlobalDef{#1}}
} %

\begin{document}
This should be empty:\GlobalDef{}
\par
This should also be empty: \GlobalDef

This should say Argument: \GlobalDef{Argument}
\par
This should also say Argument: \GlobalDef
\end{document}


# Issues:

1. \xdef is not registering as a global definition in this \ifthenelse statement.
2. Why is there a mysterious space before \GlobalDef{Argument} in the 3rd line of this output?
• The space is in your \newcommand{\GlobalDef}[1][]{ %, i.e. before the % character ... and there are other spurious spaces as well
– user31729
Jan 10 '19 at 21:58
• Why should the fourth line also produce Argument? Jan 10 '19 at 21:58
• Note that with that definition \GlobalDef may redefine itself and so all subsequent calls after that may use a different definition. I'm not sure if that is intended. Jan 10 '19 at 21:59
• Ahh I see, \GlobalDef is defined as a macro with an optional argument. So it should be called as \GlobalDef[Argument] if Argument is to be the argument. As you call it now, you just call \GlobalDef without its only optional argument and then after that have either an empty group{} or {Argument}. That means that you always hit the branch that is called for an empty argument and you never execute the \xdef in the example. Try calling \GlobalDef[] and \GlobalDef[Argument] to see what happens. I'm not sure the output makes sense, but it does what you tell it to do, Jan 10 '19 at 22:01

1. All of the evaluations in your call to \GlobalDef results in being blank, since you defined \GlobalDef to take a single optional argument. So, calls to \GlobalDef is the same as \GlobalDef{} and \GlobalDef{Argument}, none of which use the optional argument.

2. The third call inserts an additional space as a result of spurious spaces within your definition. If you wish to avoid the spurious spaces, you'll have to strategically place %, something like this:

\newcommand{\GlobalDef}[1][]{%
\ifthenelse{\isempty{#1}}
{}
{\xdef\GlobalDef{#1}}%
}


Your \GlobalDef macro is defined to replace itself via the \xdef without printing its (optional) argument. So, you'll never truly see any output anyway.

However, if your goal is to check for an empty argument, consider the evaluation discussed in How to check if a macro value is empty or will not create text with plain TeX conditionals?, specifically

\if\relax\detokenize{#1}\relax
<EMPTY>%
\else
<NON EMPTY>%
\fi