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

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

3 Answers 3

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

1
  • ok. I try to get now closer to my original problem, see my edit. Commented Feb 8, 2012 at 7:33
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....

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} 
2
  • 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 Commented Feb 8, 2012 at 7:00
  • then save the contents in a box and compare its width. See edited answer. Commented Feb 8, 2012 at 14:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.