# Using dynamic values in controls

The following code displays a plot and tries to display the x=1 value of the same plot in the control area:

DynamicModule[{lbl},
Manipulate[lbl = ToString[Sin[x (1 + a x)] /. x -> 1];
Labeled[Plot[Sin[x (1 + a x)], {x, 0, 6}], lbl],
Column[{Style[lbl, Bold, Large], Control[{a, 0, 2}]}]]]


Is there a way to scope the lbl variable so that it is updated properly in the display?

• you can simply wrap the Style with Dynamic. Note that you will always get 0 anyway, because that is the correct value following the substitution x -> 0
– glS
Oct 18, 2016 at 10:28
• if $x=0$ you have $\sin(0\times( 1+a \times 0)) = \sin(0) =0$. Anyway, doesn't Dynamic solve your problem?
– glS
Oct 18, 2016 at 13:15
• @dionys Maybe there's a typo? I get 0. under the plot: i.stack.imgur.com/b5Jhw.png -- but it does seem to be a fairly irrelevant side-issue compared to getting lbl to display. Oct 18, 2016 at 13:26
• @glS Apologies. Yes, with a fresh kernel the original label would always be zero. I was just trying to throw an example together quickly and didn't notice that Globalx was leaking in. Oct 18, 2016 at 15:27

Here's a variation that has the label above (in the control area) and below (in the plotlabel area). The trick is to have the Dynamic wrap around each of the instances of the lbl variable.

Manipulate[lbl = ToString[Sin[x (1 + a x)] /. x -> 1];
Labeled[Plot[Sin[x (1 + a x)], {x, 0, 6}], Dynamic[lbl]],
Column[{Style[Dynamic[lbl], Bold, Large], Control[{a, 0, 2}]}]]


Why do you not like

Manipulate[
Plot[Sin[x (1 + a x)], {x, 0, 6},
PlotLabel -> Style[Sin[x (1 + a x)], Bold, FontSize -> 18]], {a, 0,
2}]

• You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this meta Q&A helpful Oct 18, 2016 at 12:17
• The example is a simplified demonstration. I don't care specifically about this plot. What I would like to do is dynamically update the label (or include more complicated updating) in the controls. Oct 18, 2016 at 12:57
• I must be missing something. The code above updates the label as you move the slider. What more do you seek? Oct 18, 2016 at 14:06
• @NicholasG The key here is "in the control area" ... it entails a different evaluation track because of how this part is executed inside of Manipulate`. Oct 18, 2016 at 15:04