Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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...

share|improve this question
    
There is a space between the two \hint{}: this makes the contents non empty. –  egreg Feb 8 '12 at 10:07
    
What would be your advice, for removing all the empty spaces from \BODY ? I think about \DTLsubstituteall... –  nicolas roy Feb 8 '12 at 13:10
    
It's really quite hard to understand what you want to achieve. –  egreg Feb 8 '12 at 13:15

3 Answers 3

up vote 2 down vote accepted
\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).

share|improve this answer
    
ok. I try to get now closer to my original problem, see my edit. –  nicolas roy Feb 8 '12 at 7:33

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....

share|improve this answer
\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}
share|improve this answer
    
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 –  nicolas roy Feb 8 '12 at 7:00
    
then save the contents in a box and compare its width. See edited answer. –  Herbert Feb 8 '12 at 14:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.