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 a package that takes a long list of boolean flags (currently about 10) in its options, and I want to test if exactly one of these is set to true - something like an XOR for 10+ booleans.

The only way I can think of is to use the ifthen package and construct a gigantic statement combining \and and \or to test this, but it quickly gets real ugly, and will be incredibly hard to maintain.

Is there a better way to do this?

share|improve this question
2  
If one and only one of the flags should be set to true, then you probably shouldn't be using 10 booleans, but a key that can take 10 values... –  Willie Wong Nov 19 '10 at 16:35
3  
That's not an xor. An xor for a bunch of booleans tests whether an odd number of the booleans is true. –  Harald Hanche-Olsen Nov 19 '10 at 16:57
    
@Tomas: So you want to test if exactly one is true, right? Then it might be worth rewording the question. –  Hendrik Vogt Nov 19 '10 at 17:11
1  
Concatenate the booleans into a 10-digit binary number and then check if it is a power of 2. Easy. –  Seamus Nov 19 '10 at 21:54
    
@Tomas: I've slightly reworded the body of your question; OK? –  Hendrik Vogt Nov 20 '10 at 9:24
show 2 more comments

2 Answers

up vote 8 down vote accepted

You could create a counter, add one for each boolean that is true, then test if the counter has the value 1.

share|improve this answer
add comment

The question does not provide adequate information to provide an exact routine and as Willie Wong commented, it maybe trying to solve the problem with the wrong datastructure.

One approach is to place the booleans in a variable consisting of zeroes and ones. If anyone of the booleans is true then it will then be a number that is greater than zero.

\def\OneTrue#1{\ifnum#1<1 0\fi\number#1}

\OneTrue{1000101010}

One limitation of the macro above is that if the first boolean is 1 then the string can only be ten digits long, otherwise the number will be too big. Depending on the number of booleans one could extend the macro and split it into two to avoid this limitation and parse the two arguments.

What one does with it afterwards, it depends. One can actually use the first boolean that is true to trigger another action. In such a case we would have re-discovered the switch datastructure which is catered by ifcase in TeX!

share|improve this answer
    
I'm not sure what you're trying to do with your \OneTrue macro. Do you want to test if at least one of the booleans is true? That's not what the OP wants. –  Hendrik Vogt Nov 20 '10 at 9:28
    
@Hendrik that is exactly what he asked quoting `...I want to test if exactly one of these is set to true'. Have I missed something? –  Yiannis Lazarides Nov 20 '10 at 10:20
    
compare "at least one" with "exactly one". –  Hendrik Vogt Nov 20 '10 at 17:48
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.