Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to achieve the following: Three (dynamically updated) Checkboxes (a, b and c) can be toggled on or off individually. Depending on their status, a resulting list shall be set to {a,b,c}, {a,c}, {b,c}, etc.

I am currently using the following code:

{"a:" Checkbox[Dynamic[a]],
 "b:" Checkbox[Dynamic[b]], 
 "c:" Checkbox[Dynamic[c]], 
Dynamic[{If[a, "a",], If[b, "b",], If[c, "c",]}]}

But this yields, depending on the setting, a list of the kind {a,b,Null}, etc. - that is, if a checkbox is set to False, the list element is set to Null rather than left out entirely.

Is there any way to achieve my goal in a not-so-cumbersome way (i.e., avoiding something like a 8-cases Piecewise or Which function)? Thanks in advance!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

well, I think this might be a duplicate, see link below, but here we go:

In your setup, you want to make sure that the If evaluates to something meaningful (as you can see, just leaving the 3rd argument blank yields Null).

Thus, it makes sense to use either Unevaluated[Sequence[]] or shorter, just ##&[]:

{"a:" Checkbox[Dynamic[a]], "b:" Checkbox[Dynamic[b]], 
"c:" Checkbox[Dynamic[c]], 
Dynamic[{If[a, "a", Unevaluated[Sequence[]]], If[b, "b", ## &[]], 
If[c, "c", ## &[]]}]}

Then, if the If-clause evaluates to false, you are returned an unevaluated sequence, which will be used in further output (in your case, the list).

You might ask: why the Unevaluated? See Szabolcs explanation here: Why can't I use Sequence to perform a Select like task? (and note J.M.'s comment there, for the vanishing function)

As mentioned in the comments, you might e.g. use Pick for this task:

{"a:" Checkbox[Dynamic[a]], "b:" Checkbox[Dynamic[b]], "c:" Checkbox[Dynamic[c]],     
Dynamic@Pick[{"a", "b", "c"}, {a, b, c}]}
share|improve this answer
Great, thanks a lot! Should I delete my comments above, since they don't add anything to the question? –  Bernd May 28 '13 at 12:55
well, why not, let's clean up a bit :) –  Pinguin Dirk May 28 '13 at 12:59

It seemed that the question is asking how to display a list of selected strings. It seemed quite possible that underlying that, is how to pick variables in the same way, which takes a little more care. (You can substitute inside the Dynamic[resultingList = ...] the expression {a, b, c} or varList for labelList, if you want the list to be the variables.)

labelList = {"a", "b", "c"};
varList := {a, b, c};


You can use CheckboxBar, but the check boxes have to appear together.

x = {};
{CheckboxBar[Dynamic[x], {1 -> "a", 2 -> "b", 3 -> "c"}], 
 Dynamic[resultingList = labelList[[Sort@x]]]}



If you want to place the check boxes wherever, then you can use Parts of a List.

y = ConstantArray[False, 3];
{"a:" Checkbox[Dynamic[y[[1]]]], "b:" Checkbox[Dynamic[y[[2]]]], 
 "c:" Checkbox[Dynamic[y[[3]]]], Dynamic[resultingList = Pick[labelList, y]]}

More succinctly, and with Row to format the label-check-box pairs:

pickedVars = ConstantArray[False, 3];
{MapIndexed[Row[{#1, Checkbox[Dynamic[pickedVars[[First[#2]]]]]}] &,
  {"a: ", "b: ", "c: "}], 
 Dynamic[resultingList = Pick[labelList, pickedVars]]}

Row of Checkboxes

Technically in your original code you're using Times to format them. It seems to work because the order that Mathematica puts the factors in happens to be the one you desire. You can also use Labeled instead of Row, as well as several other commands.

You can also wrap the whole thing with Row and get rid of the List braces (modifying the list structure a little first).


In case you're open to alternatives, TogglerBar could be used instead, but you may already know that.

z = {};
{TogglerBar[Dynamic[z], labelList], Dynamic[resultingList = Sort@z]}


To get the variables, you can use the following:

z2 = {};
{TogglerBar[Dynamic[z2], {1 -> "a", 2 -> "b", 3 -> "c"}], 
 Dynamic[resultingList = varList[[Sort@z2]]]}
share|improve this answer
I actually ended up using ToggleBar and Sort myself, as you suggest, after I discovered those functions. Thanks for pointing out, though! –  Bernd May 28 '13 at 15:40

Your Answer


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.