[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,
Matthew

--------------------------
\documentclass{article}

\makeatletter

\usepackage{ifthen}

\newcommand{\gobbleLeadSpaces}[1]{
	\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}
	\else%
		\def\gobbleLeadSpaces at next{#1\empty}%
	\fi%
 	\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{ }

\newif\ifArgPresent%
\newcommand*{\blankArgTestE}[1]{
	\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}}%
	%
	\ArgPresenttrue%
	%
	\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%
}


\begin{document}

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

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

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

\testB

So far, so good, but...

% Following lines break document. 

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

%\blankArgTestE{\testC{10}}
%\ifArgPresent ArgPresent \else NoArg \fi

\end{document}



More information about the texhax mailing list