Suppose you want to access a variable defined in a "surrounding" context (scoped via Begin
and End
) from within Manipulate
. The following naive attempt does not work.
Begin["MyContext`"];
x = 123;
Manipulate[x + y, {y, 0, 1}]
End[];
x = 456; (* changes the value of Manipulate output *)
One way to fix this is by using Inactive
and Activate
as follows.
Begin["MyContext`"];
x = 123;
Activate@Manipulate[Inactive[x] + y, {y, 0, 1}]
End[];
x = 456; (* does not change the value of Manipulate output *)
MyContext`x = 456; (* changes the value of Manipulate output *)
Is this a robust way to achieve the desired goal or do you see any problems using this pattern in more involved examples? If so which alternative solutions do you suggest?
Begin["MyContext`"]; Manipulate[Module[{x = 123}, x + y], {y, 0, 1}] End[]; x = 456;
do what you want? $\endgroup$Manipulate
in a specific context likeMyContext:x
(colon -> backtick). $\endgroup$"`"
in code by surrounding the code in double backticks"``"
:"``MyContext`x``"
$\endgroup$