1
$\begingroup$

This question is related to another question of mine : How can I add a checkbox to a Manipulate expression?, but I'm still unable to make the checkbox to work properly. Here's a MWE code, from my much bigger version (I think that this code is pretty cool, since it also shows how to add an information display on top of a 3D graphics) :

Info[frequency_] := Inset[Grid[{
    {Row[{"The frequency :"}], Row[{NumberForm[frequency, {12, 3}], " unit"}]},
    {Row[{"Another data :"}], Row[{NumberForm[4.56323, {12, 3}], " unit"}]}
    },
    BaseStyle -> {FontSize -> 12, FontFamily -> "Courier"},
    Alignment -> Right, Frame -> All, FrameStyle -> {Thick, LightGray}
    ], {0.8, 0.1}]

Manipulate[Show[
    ParametricPlot3D[{Sin[3 frequency t], Cos[5 frequency t], Sin[2 t]}, {t, 0, 2 Pi}],
    PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
    Epilog -> Which[c == 0, {}, c == 1, Info[frequency]],
    SphericalRegion -> True,
    ImageSize -> {600, 600}],
    Row[{Checkbox[Dynamic@c, {0, 1}], Spacer[5], Dynamic@Switch[c, 0, "State OFF", 1, "State ON"]}],
    {{frequency, 1, "Test"}, 0, 4, 0.01, Appearance -> "Labeled"}]

The code above works partially, but in the case of my full code the output flashes with an error color (immediately after compilation), without giving any error message ! I suspect that's because the checkbox doesn't have an initial state value. So what is wrong with the code ? How can I define the initial value of the checkbox ?

If you compile the small code above, you shouldn't get any error message. But the checkbox may be checked ON or OFF, depending of what you do with with it, before each compilation. It is not behaving correctly.

The checkbox is used to turn ON/OFF an information display on top of the 3D graphics. By default, it should be OFF. The small text string ("State ON/OFF") should be displayed at the right of the checkbox, in the manipulate panel. Maybe there's a simpler way of doing this ?

$\endgroup$
4
  • $\begingroup$ Try putting a Dynamic@ on your Which in the Epilog. That seems to be causing me trouble when I try to play with this. $\endgroup$
    – N.J.Evans
    Commented Feb 22, 2016 at 15:17
  • $\begingroup$ @Evans, what trouble ? Also, adding Dynamic@ to Which doesn't do any difference on my code. The checkbox still doesn't have a default value at compilation. $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 15:25
  • $\begingroup$ Oops. I must have had some lingering definition screwing things up. Back to the question then, I don't really understand what help you're asking for. Are you going to use Compile[...], or do you just mean running the code through the MMA front end? You could try initializing c with a value before the Manipulate, etc. etc. Without a MWE that reproduces the problem you're having it'll be tough to help. $\endgroup$
    – N.J.Evans
    Commented Feb 22, 2016 at 17:38
  • $\begingroup$ @N.J.Evans, see the answer below. I think this is the solution to my problem. No need for Dynamic@. Oh, and why the Compile command ? I don't know that one. I'm running everything directly through the MMA front end. $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 21:08

1 Answer 1

1
$\begingroup$

You don't need Row if there is a single entry.

Info[frequency_] := Inset[
  Grid[{
    {"The frequency :",
     Row[{NumberForm[frequency, {12, 3}], " unit"}]},
    {"Another data :",
     Row[{NumberForm[4.56323, {12, 3}], " unit"}]}},
   BaseStyle -> {12, FontFamily -> "Courier"},
   Alignment -> Right,
   Frame -> All,
   FrameStyle -> {Thick, LightGray}],
  {0.8, 0.1}]

Using {True, False} rather than {0, 1} simplifies the code.

EDIT: Moved all controls to bottom.

Manipulate[
 ParametricPlot3D[
  {Sin[3 frequency t], Cos[5 frequency t], Sin[2 t]},
  {t, 0, 2 Pi},
  PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
  Epilog -> If[c, Info[frequency], {}],
  SphericalRegion -> True, ImageSize -> {600, 600}], 
 Row[{Spacer[550], 
   Control[{{c, False, "Show Info"}, {True, False}}]}], {{frequency, 
   1, "Test"}, 0, 4, 0.01, Appearance -> "Labeled"},
 ControlPlacement -> Bottom]

enter image description here

$\endgroup$
7
  • $\begingroup$ I need the checkbox to be on a single line, with its label text after it, since I have many sliders below it in my full code. The text label at left is taking too much space, and the sliders below the checkbox are then badly placed (ugly). Or how can I put the checkbox to the bottom-right position, while all the sliders are at the bottom-left ? $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 15:54
  • $\begingroup$ Ah ! The Spacer ! I haven't tought of that one ! Thanks. $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 19:41
  • $\begingroup$ Just a few simple questions : in the Spacer, what are the units of the spacing number ? How do you find the horizontal "size" of the label+checkbox, so you could put the checkbox at the proper place under the graphics ? Are you doing this "by eye" ? $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 20:06
  • 1
    $\begingroup$ @Cham - As stated in the documentation for Spacer the width is in printer points. And Spacer[{w, h}] will control the height as well as width. $\endgroup$
    – Bob Hanlon
    Commented Feb 22, 2016 at 20:16
  • $\begingroup$ Thanks. What about the position of the checkbox to the right ? Are you placing it "by eye" ? $\endgroup$
    – Cham
    Commented Feb 22, 2016 at 20:22

Your Answer

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

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