# Accommodating local variables within Dynamic

I would like to use a local variable within Dynamic, but that may not work, and the front end will show the local variable in red to give a warning. However, if I define a global variable to equal the local one, then I can use the global one inside Dynamic. Will this always work, or is it just a way to hide a problem that may occur? For example, this works:

Manipulate[
Block[{data = RandomReal[{0, 1}, {2, 2}]}, dataG = data;
Dynamic@ArrayPlot[dataG^p]], {p, 0.1, 10},
SynchronousUpdating -> False]


(In the present example, since the only effect on the plot of changing p is to change the color, one could also use ColorFunction.)

I stripped away many details from the application I have in mind. Rather than a simple data, I have a complicated calculation to do that depends on other control variables of the Manipulate. And that is the other reason for using Dynamic and why I want the local variable to be defined within Manipulate.

If this will not always work, is there a convenient way to introduce global variables so that they do not conflict with other global variables in the same session and without having to remember or check what has been used already?

• Does the further scoping have to be inside Manipulate? Otherwise you could wrap the Manipulate[ArrayPlot[...], ...] in a scoping construct, eg Module, DynamicModule, or With (but not Block): Module[{data = RandomReal[{0, 1}, {2, 2}]}, Manipulate[ArrayPlot[data^p], {p, 0.1, 10}, SynchronousUpdating -> False]]. You won’t need Dynamic inside this way. Jan 9, 2021 at 19:44
• @MarcoB Yes, it does have to be inside. I stripped away many details from the application I have in mind. Rather than a simple data, I have a complicated calculation to do that depends on other control variables of the Manipulate. And that is the other reason for using Dynamic. Jan 9, 2021 at 19:54
• I see. Please add that constraint and context to the question. Jan 9, 2021 at 19:57
• I'd use With on the example: Manipulate[With[{data = RandomReal[{0, 1}, {4, 4}]}, Dynamic@ArrayPlot[data^p]], {p, 0.1, 10}, SynchronousUpdating -> False] Jan 9, 2021 at 21:06
• @MichaelE2 I don't think I can use With because I need to to some calculations before I obtain the value of data, and these depend on other control variables. Jan 9, 2021 at 21:19

Two alternatives:

Using With (use multiple ones if necessary):

Manipulate[
With[
{data = RandomReal[{0, 1}, {4, 4}]},
Dynamic@ArrayPlot[data^p]],
{p, 0.1, 10},
SynchronousUpdating -> False]


Use a local variable with no control:

Manipulate[
data = RandomReal[{0, 1}, {4, 4}];
Dynamic@ArrayPlot[data^p],
{p, 0.1, 10},
{data, None},
SynchronousUpdating -> False]


One advantage of With is that it does not create a tracked symbol, which sometimes simplifies the Dynamic behavior of the Manipulate. Two disadvantages of With are that the variable is constant, and many nested With are inelegant.

• The first does not have a footprint on the notebook size out of the box, the second one requires UnsavedVariables.
– Kuba
Jan 10, 2021 at 6:26
• @Kuba Thanks. I thought I was going to say that, except about UnsavedVariables. That option had eluded my attention, maybe because I stopped contributing to the demonstrations project around the time V8 came out. Jan 10, 2021 at 14:02