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 have created a macro like this:

\newcommand{\showsection}[2]{
    \ifstrequal{#2}{}{}{\section{#1} #2}
}

It should be used like this in the document:

\showsection{Books}{
    Do you know any good books?
}

If #2 is empty, as in this case below, the section title to not be shown:

\showsection{Books}{

}

Unfortunately, with some macros inside, which should sometimes appear blank, somehow something is getting through. Perhaps it is an extra space? No text is visible. I have put % after every line of the macros which I placed inside. How can I get the section titles to disappear in this case?

share|improve this question
    
Please have a look at the documentation of etoolbox. There are also described the commands \ifblank and \ifstrempty. Another mehtod is using the pacakge xparse. –  Marco Daniel Dec 19 '11 at 12:52
1  
Please mention important packages in the text, not just as tags. It is always good to post a add a minimal working example (MWE) that illustrates your problem exactly and lets people test their solutions easily. –  Martin Scharrer Dec 19 '11 at 12:55
add comment

1 Answer

up vote 13 down vote accepted

You have an end-of-line character after the opening { which causes a space. It might be better to check if the argument is a single space instead of being empty. You can add one space to the argument yourself to make sure the test is true for a real empty content.

Also you should add % after any { or } at the end of a line inside the macro definition.

\newcommand{\showsection}[2]{%
    \ifstrequal{ #2}{ }{}{\section{#1} #2}%
}
share|improve this answer
    
I posted an advanced solution for the follow-up question Macros for hiding empty sections do not work properly which is also interesting for this question. –  Martin Scharrer May 4 '12 at 10:43
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.