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.

I just tried to write some commands which used its argument to check whether they were empty (isempty{...} from package ifthen) or not. The problem was that the text to check contained a linebreak, and that screwed up the whole system and caused it to produce very strange error messages.

Is there a way to work around this and make it safe to pass text containing linebreaks as parameter to a command or environment?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

The etextools package provides an expandable \ifempty{<text>}{<true>}{<false>} macro which works with newlines.

If you don't need the test to be expandable you can also use the following code:

\newcommand{\mymacro}[1]{%
   \begingroup
   \def\temp{#1}%
   \ifx\temp\empty
      \endgroup
       % empty
   \else
      \endgroup
       % not empty
   \fi
}
share|improve this answer
1  
Why do you work with begingroup ... endgroup ? –  Marco Daniel May 9 '11 at 15:49
2  
@Macro: to make \temp local –  Leo Liu May 9 '11 at 15:56
1  
@Macro: As Leo already said, it is to make the assignment to \temp local, which is generally a good design decision. –  Martin Scharrer May 9 '11 at 16:00
    
@Leo: I can't understand the background. \def works only inside \mymacro. During the writing of this comment I think the attention is to use \temp once more inside \mymacro. EDIT: Thanks Martin –  Marco Daniel May 9 '11 at 16:03
    
@Macro: If you use several such macros uses \temp nestly, it is necessary. –  Leo Liu May 10 '11 at 18:20
add comment

The \isempty test expands its argument; you may try

\ifthenelse{\isempty{\unexpanded{#1}}}{<true>}{<false>}

or resort to a different test like

\ifx\relax\detokenize{#1}\relax
  <true>%
\else
  <false>%
\fi

In both cases #1 stands for the argument you want to test.

share|improve this answer
add comment

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.