Set Options on a system function has effect on all notebooks

I have a Notebook A with default context: “Unique to this Notebook”.
In there I set an option:

SetOptions[Interpolation, InterpolationOrder -> 1]

This has however an effect on my other notebook which has a default context: “Global”.

How can I set that option for Interpolation, just affecting the function inside the notebook’s A context?

Second question :

In the notebook A I can type:

$Context

And get somthing like:

>>>”Notebook$$18$462796`”

From notebook B I can access a variable varA of A, by typing:

Notebook$$18$462796`varA

Is there a way in which I can store the A Notebook’s context in another variable to call it from different parts of my code, something like:

AContxt=Notebook$$18$462796
varAnb=AContxt`varA

This last thing doesn’t work, I would like to understand why.

Thanks

=================

3

 

Try SetOptions on EvaluationNotebook. Remark: if you want to ask two rather disconnected questions it’s better to use two different posts.
– Sjoerd C. de Vries
Nov 20 ’14 at 10:02

=================

2 Answers
2

=================

Interpolation is a built-in function located in the System` context, which is the same for all Notebooks independently of $Context, hence any changes of Options of this function will be global. But you can define your own function in the local context and set local options for it (see here for explanations):

Clear[interpolation];
Options[interpolation] = Options[Interpolation];
interpolation[data_, opts : OptionsPattern[]] := Interpolation[data, opts, Sequence @@ Options[interpolation]]

Now

SetOptions[interpolation, InterpolationOrder -> 1];

will have only local effect.

The definition of local interpolation can be added into an initialization Cell of the particular Notebook.

Answering second part of the question, yes you can. You need to specify the context of a variable explicitly to make it easily accessible from any Notebook. For example, define in a context my` a variable nb1 which value will be the context of your first Notebook:

my`nb1 = $Context

“Notebook$$18$462796`”

This code should be evaluated in your first Notebook. In the second Notebook you can access this variable in the same way:

my`nb1

“Notebook$$18$462796`”

  

 

Thanks! I think this solves my first question.
– Santi
Nov 21 ’14 at 2:14

1

 

I updated the answer.
– Alexey Popkov
Nov 21 ’14 at 8:13

  

 

Thanks Alexey! The method you mention is fine, but I can’t access the variable varA of nb1 by doing: my`nb1varA. I need to join them as @Albert mentioned. So I maybe ask a second separate question in order to accept both
– Santi
Nov 21 ’14 at 10:31

For your second question this would define a global aContxt so that it can be used in other notebooks (to be evaluated in the notebook with the private context):

Global`aContext = $Context;

Then you can define a new variable like this:

SetDelayed @@ Join[Hold[varAnb], ToExpression[aContext <> “varA”, InputForm, Hold]]

the reason why this looks so complicated is that you have to avoid early evaluation of the symbol you are creating. The other complication is that you have to create the full symbol name which includes the context as a string and then convert that full symbol name to an actual symbol, which is done here with ToExpression. By using SetDelayed varAnb in the second notebook now will always reflect the current value of varA in the first. The above did assume that the second notebook was set to have the default context “Global`”, if that also is supposed to have a private context, then you’d have to use the long name “Global`aContext” there as well.

It might also be worth to just introduce an extra context meant for exchange purposes, e.g. define Exchange`varA in any notebook and use it as Exchange`varA in others. You are completely free to choose the context name (but better avoid the ones already in use like “System`”). So to save some typing you could e.g. use something like e`varA in every notebook for those variables you want to share between notebooks.

  

 

Thanks these really solves my second question. Maybe I can split the question in two, to be able to accept the other answer as well?
– Santi
Nov 21 ’14 at 10:31

2

 

I think that problem is one reason why it is recommended to have one post per question. Another reason is that it will be easier to find the questions and answers and that probably is the main purpose of the site. Never mind about accepting my answer in this case, though. I’m happy if you accept the answer which solves the first of your questions.
– Albert Retey
Nov 22 ’14 at 10:44