2
$\begingroup$

Consider the following code

Manipulate[a,
 Dynamic@Grid[{
    {"Check 1", Control[{{a, 0, ""}, {1, 0}}]},
    If[a == 1, {"Check 1.1", Control[{{aa, 0, ""}, {1, 0}}]}, 
     Unevaluated[Sequence[]]],
    If[a == 1, {"Check 1.2", Control[{{ab, 0, ""}, {1, 0}}]}, 
     Unevaluated[Sequence[]]]
    },
   Alignment -> {{Right, Left}, Automatic}],
 ControlPlacement -> Left]

enter image description here

Checking Check 1 leads to

enter image description here

Is it possible to use only one If for both controls Check 1.1 and Check 1.2?

Using Flatten, for example,

Manipulate[a,
 Dynamic@Grid[{
    {"Check 1", Control[{{a, 0, ""}, {1, 0}}]},
    If[a == 1, Flatten[{
       {"Check 1.1", Control[{{aa, 0, ""}, {1, 0}}]},
       {"Check 1.2", Control[{{ab, 0, ""}, {1, 0}}]}
       }],
     Unevaluated[Sequence[]]]
    },
   Alignment -> {{Right, Left}, Automatic}],
 ControlPlacement -> Left]

I get

enter image description here

which is not what I want. I tried playing around with Column or Row but no success. Any ideas?

Also, is there a better or faster way of doing something like this? I'm using Dynamic to be able to do it, but I wonder if Mathematica already has something to do this kind of things.

$\endgroup$

1 Answer 1

6
$\begingroup$

Use Apply[Sequence] instead of Flatten:

Manipulate[a, 
 Dynamic@Grid[{{"Check 1", Control[{{a, 0, ""}, {1, 0}}]}, 
    If[a == 1, Sequence @@ 
      {{"Check 1.1", Control[{{aa, 0, ""}, {1, 0}}]}, 
       {"Check 1.2", Control[{{ab, 0, ""}, {1, 0}}]}},
     Unevaluated[Sequence[]]]}, 
   Alignment -> {{Right, Left}, Automatic}], ControlPlacement -> Left]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.