3

I am trying to use the etoolbox \ifblank (and its cousin \notblank), but I want to expand any macros in its test statement. For example:

\documentclass{article}
\usepackage{etoolbox}

\begin{document}

\newtoggle{longformt}
\newcommand{\longform}{\toggletrue{longformt}}
\newcommand{\iflong}[2]{\iftoggle{longformt}{#1}{#2}}

\newcommand{\quoteifnotblank}[1]{%
    \notblank{#1}{``#1''}{}%
}

% Try commenting this line.
\longform

This should be completely blank if the form is short:
\quoteifnotblank{\iflong{Some long quoted text}{}}

\end{document}

The problem here is that \notblank sees its argument of \iflong{Some long quoted text}{} as a string, not as code to be expanded, so the quotes always appear. How do I expand arguments before they are passed to \notblank?

3

\notblank does no expansion to its argument, so you have to do it yourself:

\documentclass{article}
\usepackage{etoolbox}

\newtoggle{longformt}
\newcommand{\longform}{\toggletrue{longformt}}
\newcommand{\iflong}[2]{\iftoggle{longformt}{#1}{#2}}

\makeatletter
\newcommand{\quoteifnotblank}[1]{%
  \protected@edef\@tempa{#1}%
  \expandafter\notblank\expandafter{\@tempa}{``#1''}{}%
}
\makeatother

\begin{document}

This should be completely blank if the form is short:
\quoteifnotblank{\iflong{Some long quoted text}{}}

\longform

This should be completely blank if the form is short:
\quoteifnotblank{\iflong{Some long quoted text}{}}

\end{document}

enter image description here

  • Awesome, thanks! What exactly is going on here? I've never used \expandafter. – thirtythreeforty Sep 25 '15 at 15:09
  • @thirtythreeforty We fully expand the argument saving what results in \@tempa; but, as I said, \notblank does not expand its argument, so we do it before it enters into action; the first \expandafter jumps over \notblank and makes the second \expandafter jump over the brace to finally expand \@tempa. – egreg Sep 25 '15 at 15:14
  • Hmm, I think I see. Why not just use \expandafter\notblank\expandafter{#1} directly? Won't \expandafter expand the #1? – thirtythreeforty Sep 25 '15 at 15:19
  • 1
    @thirtythreeforty Only exactly once, so you'll get the meaning of the first token in #1 (the definition of \iflong). – Joseph Wright Sep 25 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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