4

In the following MWE, the command \testStringNotEmpty tests if the argument is empty. This test is robust in the sense that the argument can contain a command or an environment... The environment monenv willy display "Empty" if the body (which i capture in \BODY) is empty, and "not empty" in the other case. My problem is that if the body of monenv is the command \macom which expands to "empty", then it has a different behaviour than the one i expected. This is probably because one should sort of completely expand \BODY before to call \testStringNotEmpty, and my attempt with the \expandafter does not seem to work...

\documentclass{article}
\usepackage{environ}
    \makeatletter
    \newcommand{\testStringNotEmpty}[1]{%
      \ifnum\pdfstrcmp{\unexpanded{#1}}{}=\z@
        \expandafter\@secondoftwo
      \else
        \expandafter\@firstoftwo
        \fi
    }
\makeatother

\NewEnviron{monenv}%
{%
\expandafter\testStringNotEmpty\expandafter{\BODY}%
{Environment = NOT empty}%
{Environment = Empty}%
}
\newcommand{\macom}{}

\begin{document}

\begin{monenv}
\end{monenv}

\begin{monenv}
\macom
\end{monenv}

\begin{monenv}
\macom
\macom
\end{monenv}


\end{document}

EDIT : Using the solution of egreg to expand \BODY in the environment monenv, i encounter now some problems if the content of monenv is more complicated (and reflect now my actual code).

So, i redefine monenv according to egreg's solution. I define the following command :

\newcommand{\hint}[1]{%
\testStringNotEmpty{#1}{hint : #1}{}} 

and i test it with :

\begin{monenv}
\hint{}
\end{monenv}

\begin{monenv}
\hint{}
\hint{}
\end{monenv}

which gives me "empty" for the first one (as expected) and "NOT empty" for the second one, which i don't want...

Loic Rosnay
  • 8,167

3 Answers3

3

The body is not expanding because of the \unexpanded in your definition, if you remove that you get empty for all these cases, but then some things would expand in the body that perhaps you don't want expanded. It all depends....

David Carlisle
  • 757,742
2
\makeatletter
\NewEnviron{monenv}
  {\begingroup\protected@edef\x{\endgroup
     \noexpand\testStringNotEmpty{\BODY}}%
   \x{Environment = NOT empty}{Environment = Empty}%
  }
\makeatother

In this way \BODY will be "almost completely expanded". One can't use \edef as would be more natural, because if something like \textbf happens to be in the body of the environment, \edef\x would fail miserably.

When \x is expanded you'll get

\endgroup\testStringNotEmpty{<expansion of \BODY>}{true}{false}

and the \endgroup will correspond to the previous \begingroup (and remove the definition of \x).

egreg
  • 1,121,712
1
\documentclass{article}
\usepackage{environ}    
\newsavebox\BBox
\NewEnviron{monenv}%
 {\savebox\BBox{\BODY}
  \ifdim\wd\BBox=0pt\relax 
  Environment = empty \else Environment = Not Empty: =\BODY= \fi}
\newcommand\macom{}
\begin{document}

\begin{monenv}
\end{monenv}

\begin{monenv}
\macom
\end{monenv}

\begin{monenv}
\macom
\macom
\end{monenv}

\end{document}
  • In my MWE i have a test "IF BODY not empty", but this was only for the MWE. In fact i have a more complicated test, this is why i can't use a "simple" \ifx ... \relax – Loic Rosnay Feb 08 '12 at 07:00
  • then save the contents in a box and compare its width. See edited answer. –  Feb 08 '12 at 14:58