[texhax] Finding blank argument to macro

Matthew pruchek at gmail.com
Wed May 26 05:55:43 CEST 2010

Hi everyone,

I am trying to solve a problem in Latex that I thought would be relatively
straightforward.  I would like to make a macro that will evaluate its
argument and tell me whether the result is blank or not.  I started with the
simplest possible approach by using the ifthen package:
	\ifthenelse{\equal{#1}{}}{true action}{false action}

This actually worked pretty well for a long time.  However, I recently
started tinkering with some of my macros to solve a formatting problem, and
the \ifthenelse construction no longer worked.  In the process of
troubleshooting that, I began to learn about expandability issues.  I
eventually concluded that Tex would be better suited to the types of nested
calls I was making.

I managed to come up with a Tex macro that handles different types of
"blank" pretty well.  It properly recognizes an empty argument, empty
braces, spaces, etc.  It even works on another macro that evaluates to a
blank, so I thought I was home-free.  However, as soon as I fed it a macro
that takes an argument, bad things happened.  I've attached a simple
document below that shows the problem.  

I am relatively new to Tex, and I suspect this is an indication that I am
failing to understand something deeply important about how TeX evaluates its
arguments.  Could anyone explain what I have done wrong and/or suggest a way
I could achieve what I'm trying to do?

Many thanks,




	\gobbleLeadSpaces at gobble#1\empty%
\def\gobbleLeadSpaces at gobble#1{%
	\def\gobbleLeadSpaces at arg{#1}%
	\def\gobbleLeadSpaces at space{ }%
	\ifx\gobbleLeadSpaces at arg\gobbleLeadSpaces at space%
		\def\gobbleLeadSpaces at next{\gobbleLeadSpaces at gobble}
		\def\gobbleLeadSpaces at next{#1\empty}%
 	\gobbleLeadSpaces at next%

% As long as \gobbleLeadSpaces is called now, it can be used
% within \blankArgTestE's definition later. Without this line,
% \blankArgTestE causes errors.
\gobbleLeadSpaces{ }

	\edef\blankArgTestE at arg{#1}%
	\edef\blankArgTestE at empty{\empty}%
	\edef\blankArgTestE at braces{{}}%
	\edef\blankArgTestE at argG{\gobbleLeadSpaces{#1\empty}}%
	\edef\blankArgTestE at emptyG{\gobbleLeadSpaces{\empty\empty}}%
	\edef\blankArgTestE at bracesG{\gobbleLeadSpaces{{}\empty}}%
	\ifx\empty\blankArgTestE at arg\ArgPresentfalse\fi%
	\ifx\blankArgTestE at empty\blankArgTestE at arg\ArgPresentfalse\fi%
	\ifx\blankArgTestE at blank\blankArgTestE at arg\ArgPresentfalse\fi%
	\ifx\blankArgTestE at emptyG\blankArgTestE at argG\ArgPresentfalse\fi%
	\ifx\blankArgTestE at blankG\blankArgTestE at argG\ArgPresentfalse\fi%


\newcommand{\testA}{\ifnum10=10 \empty\else A\fi}
\newcommand{\testC}[1]{\ifnum#1=10 \empty\else A\fi}

\blankArgTestE{        }
\ifArgPresent ArgPresent \else NoArg \fi
\ifArgPresent ArgPresent \else NoArg \fi
\ifArgPresent ArgPresent \else NoArg \fi

\ifArgPresent ArgPresent \else NoArg \fi
\ifArgPresent ArgPresent \else NoArg \fi


So far, so good, but...

% Following lines break document. 

%\ifArgPresent ArgPresent \else NoArg \fi

%\ifArgPresent ArgPresent \else NoArg \fi


More information about the texhax mailing list