4
$\begingroup$

I am using Manipulate to plot a function, but I would also like the option of showing information about that function in the Manipulate control area, as text. I keep getting an error (see code / image below). The "Show b" textbox would switch the neighboring text on/off. I just can't get Mathematica to "see" the variable show variable or access the function f[x,b] in this If command in the Manipulate control area. The If statement does not seem to be recognized correctly, either. Suggestions would be most helpful!

Here is a MWE:

Clear[f]
f[x_, b_] := x^2 + b
Manipulate[
 Plot[f[x, b], {x, 0, 5}, ImageSize -> Large, PlotRange -> All, 
  AxesOrigin -> {0, 0}],
 {{b, 0.1}, 0, 2}, 
 Row[{Control[{{show, True, "Show b"}, {True, False}}], 
   If[show, "b = " <> ToString[f[0, b]], ""]}, Spacer[20]]]

Result

$\endgroup$

2 Answers 2

3
$\begingroup$

You're only missing a Dynamic wrapped around your If:

Clear[f]
f[x_, b_] := x^2 + b
Manipulate[
 Plot[f[x, b], {x, 0, 5}, ImageSize -> Large, PlotRange -> All, 
  AxesOrigin -> {0, 0}],
 {{b, 0.1}, 0, 2}, 
 Row[{Control[{{show, True, "Show b"}, {True, False}}], 
   If[show, "b = " <> ToString[f[0, b]], ""]}, Spacer[20]]]

enter image description here

The reason is that Manipulate doesn't really process or wrap custom controls too much, so you manually need to add stuff like Dynamic.

As for how to see what the problem might be: As you've noted, the If statement wasn't properly evaluated. In particular, you can see that the condition variable is FE`show$..., which indicates that it is a localized variable of a DynamyicModule (which is internally used by Manipulate). The fact that you see it without a value substituted indicates that the expression was evaluated in the wrong context, i.e. not as part of a dynamic evaluation (only those are properly scoped to enclosing DynamicModule constructs)

$\endgroup$
2
$\begingroup$

Try this:

Clear[f]
f[x_, b_] := x^2 + b
Manipulate[
 
 Plot[f[x, b], {x, 0, 5}, ImageSize -> Large, PlotRange -> All, 
  AxesOrigin -> {0, 0}, 
  PlotLabel -> 
   If[show == True, Panel[Row[{"b = ", b}], ImageSize -> {70, 40}]]],
 
 Control[{{b, 0.1}, 0, 2}], 
 Control[{{show, True, "Show b"}, {True, False}}],
 ControlType -> {Slider, Checkbox}]

with the following effect:

enter image description here

Have fun!

Edit: To address your question. I do not know how to do it dynamically in the form of the label of the Manipulate panel (as you asked). However, please find below the way of how to show it in the form of the slider label:

Clear[f]
f[x_, b_] := x^2 + b
Manipulate[
 Plot[f[x, b], {x, 0, 5}, ImageSize -> Large, PlotRange -> All, 
  AxesOrigin -> {0, 0}], 
 Control[{{b, 0.1, 
    Dynamic[Panel[If[show == True, Row[{"b=", b}], "b"], 
      ImageSize -> {80, 40}]]}, 0, 2}], 
 Control[{{show, True, "Show b"}, {True, False}}], 
 ControlType -> {Slider, Checkbox}]

Hope this helps.

enter image description here

$\endgroup$
2
  • $\begingroup$ Alexei: any way to get the text, "b = 1.538", to appear directly in the Manipulate control space, rather than in the plot itself? Thanks $\endgroup$ Nov 12, 2021 at 12:53
  • $\begingroup$ Please see the edit. $\endgroup$ Nov 12, 2021 at 14:24

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.