2

Within Manipulate, I want to vanish certain controls if other controls satisfy a specific condition. The simplest case is to consider two checkboxes, x and y, where y vanishes if x is selected.

I have heard of the vanishing function ## &[] or Unevaluated[Sequence[]] (from this question), but it doesn't seem to do what I want. Consider the following code

Manipulate[x,
 Control[{{x, 0}, {1, 0}}],
 Dynamic@If[x == 0, Control[{{y, 0}, {1, 0}}], ## &[]]]

which yields

enter image description here

What I want is

enter image description here

but I'm getting

enter image description here

Surprisingly enough, using Grid in the following manner solves my problem

Manipulate[x, Dynamic@Grid[{
    {"", Control[{{x, 0}, {1, 0}}]},
    If[x == 0, {"", Control[{{y, 0}, {1, 0}}]}, ## &[]]}]]

Any idea why? How do I fix my code?

sam wolfe
  • 4,663
  • 7
  • 37

2 Answers2

2

This version will not produce any extra vertical space

Manipulate[x,
  {{x, 0}, None},
  Dynamic @ Column[If[x == 1, c1, c2]],
  Initialization :> (
    c1 = {Control[{{x, 1}, {1, 0}}]};
    c2 := {Control[{{x, 0}, {1, 0}}], Control[{{y, 0}, {1, 0}}]})]

Its two states look like

x0

and

x1

Update

Although the above solution using Manipulate to do everything works well enough, I think it better to localize the variables with DynamicModule. Like so:

DynamicModule[{x = 0, c1, c2},
  Manipulate[x,
    Dynamic @ Column[If[x == 1, c1, c2]]],
  Initialization :> (
    c1 = {Control[{{x, 1}, {1, 0}}]};
    c2 = {Control[{{x, 0}, {1, 0}}], Control[{{y, 0}, {1, 0}}]})]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • This look good, but I would prefer to keep the If in the code, making it slightly shorter. – sam wolfe Mar 18 '20 at 21:31
  • @samwolfe. That would be nice, but I don't see any way to do that and not have additional vertical space. – m_goldberg Mar 18 '20 at 21:40
  • I think when using ## &[] within the Grid you don't get the spacing. I wonder what's happening internally in Mathematica. – sam wolfe Mar 18 '20 at 21:41
  • @samwolfe. When I evaluate your Manipulate expression using Grid, I get exactly the same result as your first example without Grid. – m_goldberg Mar 18 '20 at 21:55
  • @samwolfe. I made a small change in the code so it now behaves exactly as you show in your post. – m_goldberg Mar 18 '20 at 22:08
  • I realised I was missing something. I changed the code of the Manipulate with Grid. Please check it now. – sam wolfe Mar 19 '20 at 00:15
1

How about

Manipulate[x, Control[{{x, 0}, {1, 0}}], 
 Dynamic@If[x == 0, Control[{{y, 0}, {1, 0}}], Invisible[# &]]]

or

Manipulate[x, Control[{{x, 0}, {1, 0}}], 
 Dynamic@If[x == 0, Control[{{y, 0}, {1, 0}}], ""]]
Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67
  • This is a good answer, but it seems I get an extra spacing which I would like to avoid. To see what I mean, comment , Dynamic@If[x == 0, Control[{{y, 0}, {1, 0}}], Invisible[# &]] and compare it. – sam wolfe Mar 18 '20 at 18:39