I am trying to test if a parameter is "empty" in practice rather than just "technically". Consider the following MWE for my meaning:



1) \ansOne{}% This returns nothing as intended

2) \ansOne{{}}% This returns "correct" when I want it to return nothing

3) \ansOne{{}{}{}}% This returns "correct" when I want it to return nothing

4) \ansOne{correct}% This returns "correct" as intended

I found a great description of various tests to see if a parameter is empty Here which is where I got the above technique. After testing them however all of them seem to have this problem. My guess is that latex is expanding the outermost braces only, and then since the inside has "tokens" it counts as not empty and returns "correct".

To be clear; I need a macro that returns absolutely nothing if the interior is only braces or spaces (I know it will be balanced however, so no unbalanced ifs, braces, etc), and returns "correct" (or whatever) if there is anything else.

Is there a way to do some sort of recursive token expansion to discover if the braces are filled or empty eventually? And will it have a problem with, say, \ansOne{{}{ }}? Or will the whitespace get eaten as it usually does?

EDIT: Added Utility

A nice place to use this particular situation would be if you have a custom command that gives a \item with some formatting options, and takes an optional argument to have an effect. For example;

\newcommand{\choice}[2][]{\item You typed in #2. 
    \ifthenelse{\equal{#1}{correct}}{Your answer is correct! Good work!}{}
    \ifthenelse{\equal{#1}{}}{Your answer is incorrect, try again.}{}

\choice[correct]{This should mark as correct}
\choice[]{This should mark as incorrect}
\choice[Nonsense]{This should just be repeated, not marked correct or incorrect}
\choice[\ansOne{{{}}}]{This should be marked incorrect, ideally}
\choice[\ansOne{{{correct}}{}{}}]{This should be marked as correct, ideally}.

This fails currently because the \ansOne can't expand in time to get processed by the \choice command in some intelligent fashion it seems. Something about the cls file I'm trying to edit has formatting options that make \expandafter before the \choice command fail. In fact, if you compile the above code in my cls type, it returns the "something is missing, possible a \item" error one normally finds when you don't lead with \item. If I can get the \ansOne to immediately expand before \choice I'm fairly certain all these problems go away, but the only thing I know of to try is \expandafter which doesn't seem to be working. I have included the full multipleChoice environment and \choices command code below but it utilizes tex4ht which I don't really understand to hook it into html so read at your own risk :)

%% multiple choice environment

%% correct answers for multiple choice
\item #2\ifthenelse{\boolean{\choice@correct}}{\ifhandout \else \,\checkmark\,\setkeys{choice}{correct=false}\fi}{}}

%% Not really meant to be used outside this package (used in wordChoice)
%% \otherchoice outputs the item if correct and nothing if incorrect.

%setting default values for multipleChoice command key=value pairs

\begin{trivlist}\item[\hskip \labelsep\small\bfseries Multiple Choice:]\hfil\begin{enumerate}}

%multipleChoice* is for internal use only! (used in wordChoice)
%It displays all choices in a list separated by /
\ifthenelse{\equal{\mc@id}{}}{}{\immediate\write\idfile{var \mc@id;}}%
\begin{enumerate*}[before={\upshape{(\hspace{-.25em}}}, itemjoin={/\hspace{-.25em}}, after={\upshape{)}}, label={}]}

The full cls file can be found here

  • Etoolbox and \ifdefvoid might work – daleif Jun 20 '17 at 5:35

You can use expl3 to iterate over the subgroups and check if they are empty.



\NewDocumentCommand \ansOne { m }
  \bool_set_true:N \l_tmpa_bool
  \tl_map_inline:nn { #1 }
    \tl_if_blank:nF { ##1 } { \bool_set_false:N \l_tmpa_bool }
  \bool_if:NF \l_tmpa_bool { correct }


\ansOne{} % returns nothing
\ansOne{{}} % returns nothing
\ansOne{{}{}{}} % returns nothing

\ansOne{{}{ }} % returns nothing

\ansOne{{abc}{def}} % returns "correct"


You can do it in Lua as well. The good thing about this is that the level of nesting of empty groups does not matter (the other solution only supports one level) and the macro is fully expandable, i.e. after \edef\x{\ansOne{{abc}{def}}} the content of \x will be correct.


    local input = "\detokenize{#1}"
    % Remove all {, }, and space from input
    local output = string.gsub(input, "[{} ]", "")
    if output == '' then

    % Do nothing

\ansOne{} % returns nothing
\ansOne{{}} % returns nothing
\ansOne{{}{}{}} % returns nothing

\ansOne{{}{ }} % returns nothing

\ansOne{{abc}{def}} % returns "correct"

| improve this answer | |
  • If you use \bool_if:fF you can also get \ansOne{\empty} to return nothing. – Manuel Jun 18 '17 at 6:28
  • @Manuel You probably mean \tl_if_blank:fF. Also you have to generate that variant. It doesn't exist by default. – Henri Menke Jun 18 '17 at 6:32
  • Yes, that's what I meant. And yes, always generate such variants if needed, there's nothing wrong. I meant that with just a small change the behaviour could be extended, but I really don't know what OP has in mind. I will remove my comments, it was just nitpicking. And the question explicitly says spaces and braces only. – Manuel Jun 18 '17 at 6:33
  • This works great for what I needed. Upon using it in my code however, I encountered a situation where I'd like to use this in an itemize setting to pass an optional argument. I've edited the problem with the desired added utility. I'll mark this answer as correct but I'll give it a little time before I do so in case anyone wants to chime in with the other part. Either way, what you've written here is very useful for a bunch of use-cases I needed, so thanks! – Jason Jun 19 '17 at 1:32

Of course, you can do \setbox0=\hbox{\ignorespaces#1}\ifdim\wd0=0pt but this is not expandable test. The expandable test can be \isXempty{param}\iftrue defined here:

   \ifx\end#1\csname iftrue\expandafter\endcsname
   \else \if\string{#1\expandafter\expandafter\expandafter \isXemptyA
         \else \if\string}#1\isXemptyB 
               \else \isXemptyC
   \fi   \fi   \fi

% test:

\isXempty{}\iftrue YES\else NO\fi          % returns YES

\isXempty{{} {{ }}}\iftrue YES\else NO\fi  % returns YES

\isXempty{{x}}\iftrue YES\else NO\fi       % returns NO

\isXempty{{}{}{}}\iftrue YES\else NO\fi    % returns YES

| improve this answer | |
  • Also I like the \expandafter\endcsname trick. +1! – Henri Menke Jun 19 '17 at 21:18

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.