# Array of checkboxes in Manipulate

I try to produce an example where I can mark check boxes corresponding to 10 integer variables which can be 1 or 0 and the result should be the total of marked variables.

As an example for three variables I did the following:

Manipulate[n1 + n2 + n3, {n1, {1, 0}}, {n2, {1, 0}}, {n3, {1, 0}}] Question 1: How can the check boxes be aligned in a row?

Question 2: How can a certain number of check boxes (e.g. 10) be created at once?

Question 3: If I have a more complicated code how can I find out if a check box is marked and then do something depending on it?

• Something like a TogglerBar? Oct 26, 2016 at 10:37
• Or CheckboxBar? Oct 26, 2016 at 10:38
• Question 1, very simply: Manipulate[n1 + n2 + n3, Row[{Control@{n1, {1, 0}}, Control@{n2, {1, 0}}, Control@{n3, {1, 0}}}]]
– ktm
Oct 26, 2016 at 13:37

With[{n = 10},
Manipulate[
Length[x],
{{x, {}, ""},
Table[m -> Subscript["n", m], {m, n}],
ControlType -> CheckboxBar,
Appearance -> ("Horizontal" -> {Automatic, 5})}]] • What's the point of the rule m -> Subscript["n", m]? I checked and just having Subscript["n", m] seems to work just fine. Am I missing something?
– Pirx
Oct 26, 2016 at 20:38
• @Pirx - change the first line of the Manipulate to {x, Length[x]} and compare the values of x when the rules are used and when they are not. Which one is better depends on how else the values are to be used. Oct 26, 2016 at 20:45
• Thank you ... this example helps me a lot.
– lio
Oct 27, 2016 at 9:25

We can use Bob Hanlon's answer as the basis for figuring out how to answer your 3rd question.

With[{n = 10},
Manipulate[Row[{"checked: ", Length[boxes], "  values: ", boxes}],
{{boxes, {}, ""},
Table[m -> Subscript["n", m], {m, n}],
ControlType -> CheckboxBar,
Appearance -> ("Horizontal" -> {Automatic, 5})}]] This tells us that boxes is a list of the checked boxes, not sorted, but maintained in the order in which they were checked. There are many way to use this information. I will discuss a couple of them, both depending on the function MapAt.

If you want the same action to applied to all the checked items, a direct application of MapAt will do. The following will highlight any grid item in red when the corresponding checkbox is checked.

With[{n = 10},
Manipulate[
Grid[
TakeDrop[
MapAt[Style[#, Red] &, items[[All, 2]], {#} & /@ boxes],
Quotient[n, 2]],
Dividers -> All],
{{items, Table[m -> Subscript["n", m], {m, n}]}, None},
{{boxes, {}, ""}, items,
ControlType -> CheckboxBar,
Appearance -> ("Horizontal" -> {Automatic, 5})}]] A more general way would be create an association of actions. Using such an association requires a little deeper understanding of Mathematica, but isn't too hard. My example is somewhat contrived; it is a grid which shows the power of 2 for those powers indicated by a checked box.

With[{n = 10},
DynamicModule[{actions},
actions = AssociationThread[Range[n], Function /@ Table[#^i, {i, n}]];
Manipulate[
Grid[
{{"Powers of 2", SpanFromLeft}} ~ Join ~
TakeDrop[
(Composition @@ (MapAt[actions[#], #] & /@ boxes)) @ ConstantArray[2, n],
Quotient[n, 2]],
Dividers -> All],
{{items, Table[m -> "2"^m, {m, n}]}, None},
{{boxes, {}, ""}, items,
ControlType -> CheckboxBar,
Appearance -> ("Horizontal" -> {Automatic, 5})}]]] Here's an alternative that lets you choose the number of checkboxes with the slider:

g[n_] := TogglerBar[Dynamic[x], Range[n]]
Manipulate[If[x == {2}, x = 100]; Row[{g[n], x}], {n, 1, 10, 1}] I've added the If to show one way to use the boxes to change things -- in this case, whenever the second button (alone) is pressed, x displays 100, otherwise it just displays a vector of numbers corresponding to the checked boxes.