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 other words, is bool{testB} evaluated at all in this example code?

\newbool{testA}
\newbool{testB}
\booltrue{testA}
\booltrue{testB}
\ifboolexpr{ bool{testA} or bool{testB} }{OK}{Something's wrong...}

(I am too lazy (pun intended;)) to check in the source, the documentation seems to say nothing about it – maybe someone just knows that and doesn't have to analyze the source (which I did look into, but didn't understand it at a first glance)? If it isn't something obvious, I'll be happy to do my homework, dig into the source and try to establish this.)

share|improve this question

2 Answers 2

up vote 14 down vote accepted

The evaluation is not lazy, revealed by scanning the .log when tracing the macro expansions:

%\begingroup
\tracingmacros1
\ifboolexpr{ bool{testA} or bool{testB} }{OK}{Something's wrong...}
%\endgroup

That is, a complete evaluation of bool{testA} is followed by an equivalent evaluation of bool{testB}, followed by an evaluation of or, to output \@firstoftwo{OK}{Something's wrong...}.

Depending on your inspection of the macro expansion, you may require the use of a group to limit the output to the .log.

share|improve this answer
3  
I'd have gone for a faster route: \ifboolexpr{ bool{testA} or bool{\ERROR} } would work with testA true and lazy evaluation, but fails so evaluation can't be lazy :-) –  Joseph Wright Apr 2 at 7:21
5  
@JosephWright: I see, the lazy approach to detecting whether the evaluation is lazy. :) –  Werner Apr 2 at 7:23

The answer has been given by Werner. xintexpr knows about etoolbox booleans and toggles. Different syntaxes allow different levels of laziness:

[sorry I got wrong the way "lazy" was to be interpreted; thus I switch to "silly"]

  1. silly: booleans are replaced by 1 or 0 depending on whether they evaluate to true or false, and then the logical or does not stop scanning these 1's or 0's even if it has found already a true one.
  2. semi-silly: the booleans are evaluated but the or stops when it has found a true one
  3. not silly: booleans are not evaluated unless really needed.

In the code snippet below, the three different syntaxes realize the three possibilities described above:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xintexpr}

\begin{document}
\newbool{testA}
\newbool{testB}
\newbool{testC}
\booltrue{testA}
\booltrue{testB}
\booltrue{testC}

%\tracingmacros1

% silly
\xintifboolexpr{ bool(testA) | bool(testB) | bool(testC) }{OK}{Something's wrong...}

% semi-silly
\xintifboolexpr{ any(bool(testA),bool(testB),bool(testC)) }{OK}{Something's wrong...}

% not silly
\xintifboolexpr{ bool(testA)?1{bool(testB)?1{bool(testC)}} }{OK}{Something's wrong...}

\end{document}
share|improve this answer
2  
I rolled-back twice my edits... :-( I realized I had interpreted "lazy" as the opposite of its signification in the OP, then I changed my mind, then I again changed my mind. (this is all because "lazy" has a pejorative connotation, while in fact it is the intelligent thing to do; perhaps even generally ;-)) –  jfbu Apr 2 at 8:36

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.