# How can I add a checkbox to a Manipulate expression?

I'm looking for a simple example about how to add a simple On-Off switch in a manipulate function.

More specifically, I defined a curve with two parameters. One is a continuous parameter that is changed using the manipulate slider. The other one is a discrete parameter that takes two values only : 0 or 1. I need to put that parameter as a check box in the manipulate output, above the continuous slider. The check box should have a small line of text, which changes if the box is in its 0 state or its 1 state.

This is certainly very basic, but I can't find any clear example anywhere.

How can I implement what I described?

• it's not a checkbox but maybe works? Manipulate[Plot[Sin[a x], {x, 0, 10}], {a, 0, 1, 1}] – tsuresuregusa Feb 1 '16 at 2:14

I just want to point out the Edmund's fancy version of Manipulate is unnecessarily complicated. The same effects can be gotten with much simpler code.

Manipulate[
Plot[Sin[a + b x], {x, 0, 10}],
Row[{
Control[{{a, 0, ""}, {0, 1}}],
"  ",
Dynamic @ Switch[a, 0, "Zero", 1, "One"]}],
{{b, 1, ""}, 1, 5, Appearance -> "Labeled"}]


Manipulate allows a great deal of customization of controls, so it is seldom really necessary to use the kind of lower-level dynamic code that Edmund invoked (although such certainly works).

Also note that to insert a control specification such as {{a, 0, ""}, {0, 1}} into a formatting construct such as Row, one need only wrap it with Control.

### Update

In a comment below, the OP asks: How do you place the button to the left, and the slider to the right?

Manipulate[
Plot[Sin[a + b x], {x, 0, 10}],
Row[{
Control[{{a, 0, ""}, {0, 1}}],
"  ",
Dynamic @ Switch[a, 0, "Zero", 1, "One"],
Spacer[80],
Control[{{b, 1, ""}, 1, 5, Appearance -> "Labeled"}]}]]


The space between the two controls can be adjusted by changing the argument given to Spacer. For demonstration purposes, I have chosen a value that moves the slider to the far right.

• How do you place the button to the left, and the slider to the right ? – Cham Feb 1 '16 at 14:22
• So there isn't a command to push the slider to the right ? We need to adjust it by eye, is that right ? – Cham Feb 1 '16 at 18:54
• @Cham. I know of no simple command that will make the controls in a Row behave as there were a spring spacer between them. pushing them to space out over the full width of the panel they located in. – m_goldberg Feb 2 '16 at 2:24
• Ok, thanks a lot. So I'll use a spacing by eye-hand. It's all working great anyway. – Cham Feb 2 '16 at 15:48

A simple way is to define your Manipulate variable parameters so that they are recognised as the control type you want. Look in the first part of the Details and Options section of Manipulate documentation.

Manipulate[
Plot[Sin[a + b x], {x, 0, 10}], {{a, 0}, {0, 1}}, {b, 1, 5}]


You can get fancier by specifying the controls and layout directly.

Manipulate[Plot[Sin[a + b x], {x, 0, 10}],
Column@{
Row[{Checkbox[Dynamic@a, {0, 1}], Spacer[5],
Dynamic@Switch[a, 0, "Zero", 1, "One"]}],
Slider[Dynamic@b, {1, 5}, Appearance -> "Labeled"]}
]


Hope this helps.

• Thanks a lot. It's working. I think this is it. I'll wait a bit in case someone else find a simpler method, before checking this as "THE" answer. – Cham Feb 1 '16 at 2:34
• Why the Spacer[5] command ? Removing it doesn't change anything, apparently. – Cham Feb 1 '16 at 2:36
• Duh ! I see the spacing now ! – Cham Feb 1 '16 at 2:38