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 some package I have seen construct like this one:

\if\blank{\myMacro}%
  %do something
\else
  %do something else
\fi

I like it and I want to use it to test if call to a macro has empty result. I have tried it in my code and I have found out that the \blank macro does not work until I add \usepackage{conditionals} to preamble.

Now here is the problem: I have tried to find documentation to package "conditionals" but there seems to be no such package installed (nor available) on my MiKTeX distribution. I have tried search included in MiKTeX package manager but did not find it. Why is that? Where do I find documentation of that package?

Side note: I was looking for the documentation because I wanted find opposite macro something like \notblank

share|improve this question
3  
A search of my system reveals that the file conditionals.sty is part of the songbook package. However, there is no documentation with the latter about conditionals. I'd therefore suggest you want to look at something like etoolbox: perhaps you could focus the question on the test you need rather than what seems to be an obscure support package. –  Joseph Wright Jan 22 '13 at 21:49
    
Could this be what you are looking for?: tug.org/texlive/devsrc/Master/texmf-dist/doc/latex/base/… –  Dave Jan 22 '13 at 21:53
    
@Dave Many of us would take the line discussed in tex.stackexchange.com/questions/13866 that there are more powerful alternatives to ifthen. –  Joseph Wright Jan 22 '13 at 21:56
1  
@MartinScharrer could be that \blank{\myMacro} expands to 01 or 11 depending on whether \myMacro is blank or not... (or rather the contrary) –  jfbu Jan 22 '13 at 22:11
1  
@jfbu: You are right, this way it would work with \if, and even make sense if you want to work with \if.. \else \fi instead with a \@if..{..}{..} construct. –  Martin Scharrer Jan 22 '13 at 22:13
show 4 more comments

2 Answers 2

The macros in the conditionals package are by Donald Arsenau and according to the commentary in the package they are public domain.

So here they are:

{\catcode`\!=8 % funny catcode so ! will be a delimiter
 \catcode`\Q=3 % funny catcode so Q will be a delimiter
\long\gdef\given#1{88\fi\Ifbl@nk#1QQQ\empty!}
\long\gdef\blank#1{88\fi\Ifbl@nk#1QQ..!}% if null or spaces
\long\gdef\nil#1{\IfN@Ught#1* {#1}!}% if null
\long\gdef\IfN@Ught#1 #2!{\blank{#2}}
\long\gdef\Ifbl@nk#1#2Q#3!{\ifx#3}% same as above
}

Surround the code by \makeatletter and \makeatother if you want to use them in your document.

Let's see what happens when \if\blank{<tokens>}<TRUE>\else<FALSE>\fi is expanded, concentrating on the \if\blank{<tokens>} part. Usually <tokens> will be the argument to a macro.

One has to know that \if continues expansion until it finds two unexpandable tokens after it, of which it compares the character code. So the first expansion is

\if88\fi\Ifbl@nk<tokens>QQ..!

and \if compares 8 with 8; the \fi closes the conditional which does nothing at all! It's just there to allow the construction to be nested with other conditionals. Now TeX finds

\Ifbl@nk<tokens>QQ..!

If <tokens> consists only of (zero or more) spaces, the search for the first (undelimited) argument to \Ifbl@nk will bypass them. In this case argument #1 will be Q3 (the subscript denotes the funny category code), argument #2 will be empty and argument #3 will be .., so finally we'll have

\ifx..<TRUE>\else<FALSE>\fi

which will choose the true branch. Suppose now that in <tokens> there is a non space token. The first such token (or braced group) will become the first argument to \Ifbl@nk, while the rest will be argument #2. So anything up to !8 will be argument #3, that is Q.. so TeX will see

\ifx Q..<TRUE>\else<FALSE>\fi

and the comparison is between Q3 and .12 which are different, so the trailing period will vanish with the ignored true branch and the false branch will be followed.

Note that no expansion of <tokens> takes place; this list of tokens disappears completely.

With \if\given{<tokens>}, after the same \if88\fi that disappears, TeX finds

\Ifbl@nk<tokens>QQQ\empty!

Again, if <tokens> consists of (zero or more) spaces, these will be bypassed and argument #1 will be Q, argument #2 will be empty and argument #3 will be Q\empty; so TeX will see

\ifx Q\empty<TRUE>\else<FALSE>\fi

thus following the false branch. If some non blank token appears in <tokens> the first one (or the first braced group) will be #1, the rest will be #2 and #3 will be QQ\empty. In this case TeX will see

\ifx QQ\empty<TRUE>\else<FALSE>\fi

so \empty starts the true branch, but its expansion is empty and is (almost) irrelevant.

Last, \if\nil{<tokens>}. After \if88\fi we get

\IfN@Ught<tokens>* {<tokens>}!

Now leading blank tokens in <tokens> will not be bypassed, because the first argument to \IfN@Ught is delimited by a space. Everything after a leading space will become #2 and at least * is there, so \blank{#2} will return false. If the first token in <tokens> is not a space, at least {<tokens>} will become part of #2, so that \blank{#2} will return false as well.

Thus the only way \if\nil{<tokens>} can return true is that <tokens> is really empty (no tokens at all).

Very clever macros indeed!

Alternative 1

Use etoolbox that provides

\ifstrempty{<tokens>}{<true>}{<false>}
\ifblank{<tokens>}{<true>}{<false>}
\notblank{<tokens>}{<true>}{<false>}

that are equivalent to

\if\nil{<tokens>}
\if\blank{<tokens>}
\if\given{<tokens>}

respectively, but with a different syntax (no \else and \fi). The footnote to the documentation in etoolbox says that the macros are based on code by Donald Arsenau, which quite probably is the same as in conditionals.sty.

Alternative 2

In LaTeX3 there are similar constructs:

\tl_if_empty:nTF {<tokens>}{<TRUE>}{<FALSE>}

is the same as \if\nil{<tokens>}, while

\tl_if_blank:nTF {<tokens>}{<TRUE>}{<FALSE>}

is the same as \if\blank{<tokens>}. Of course the syntax is similar to the one in etoolbox. There's actually no need for an analogous to \if\given{<tokens>}.

share|improve this answer
add comment

The conditionals package does in fact provide the opposite of blank:

 \if\blank{ } --- checks if parameter is blank (Spaces count as blank)
 \if\given{ } --- checks if parameter is not blank: like \if\blank{}...\else
 \if\nil{ }   --- checks if parameter is null (spaces are NOT null)

If you look in the log file of a document using \usepackage{conditionals} you will see the full path to the file. Each time TeX opens a file it writes a ( to the log file followed by the path to the file opened,and it writes a ) to the log file once it has finished reading the file.

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.