6
$\begingroup$

The Q&A How to unset an notebook option programmatically? nearly asks the same question, but the focus there is on how to unset an option that has been given a value. I just want to undo a mistake I made by setting an option that does not exist:

SetOptions[EvaluationNotebook[], Foo -> Bar]

While the Foo -> Bar option does not seem to interfere with the working of the notebook or Mathematica, I'd like to clean up this mistake.

I'd like to keep the other notebook option settings, so copy-pasting the contents would not work exactly as I would like (a method mention in the linked Q&A). Of course I could copy the desired options, too. I'm hoping for a more straightforward solution.

(In fact, in the actual use case, the setting was some data to be stored in TaggingRules. Now the data appear twice in the notebook options, more or less; the TaggingRules version is the up-to-date one.)

$\endgroup$
  • 1
    $\begingroup$ I'm not entirely sure, but doesn't setting a option to Inherited more or less do this? $\endgroup$ – Sjoerd Smit Nov 22 '19 at 22:49
  • $\begingroup$ @SjoerdSmit You're suggesting that I set an option "MyData" -> Inherited and it will be deleted? I'll try it. -- Nope, it seems the option is kept, although the setting is now Inherited. $\endgroup$ – Michael E2 Nov 22 '19 at 22:50
  • $\begingroup$ Yes, the option is still there, but I believe that the value Inherited more or less acts as if the option was not there in the first place. $\endgroup$ – Sjoerd Smit Nov 22 '19 at 22:58
  • $\begingroup$ The whole option is inert as it is, so it makes no difference what it is set to. It's not an actual, valid notebook option. Unknown options to notebooks seem to be ignored. It's been there for months and I only just discovered it today. I'd like to get rid of it, if possible. $\endgroup$ – Michael E2 Nov 22 '19 at 23:37
10
$\begingroup$

You can use NotebookGet/NotebookPut. Here is a function that does this:

RemoveNotebookOption[nbobj_, rhs_]:=With[{nb = NotebookGet[nbobj]},
    NotebookPut[
        nb /. Notebook[c_, o___] :> Notebook[c, Sequence @@ FilterRules[{o}, Except[rhs]]],
        nbobj
    ]
]

Let's give the evaluation notebook the option "FOO"->"BAR":

SetOptions[EvaluationNotebook[], "FOO" -> "BAR"]

Check:

Lookup[Options[EvaluationNotebook[]], "FOO"]

"BAR"

Now use RemoveNotebookOption:

RemoveNotebookOption[EvaluationNotebook[], "FOO"]

Check:

Lookup[Options[EvaluationNotebook[]], "FOO"]

Missing["KeyAbsent", "FOO"]

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Great! That works. Thanks. (Still wonder why there's not a direct way, why more people don't need it from time to time. Maybe doofuses like me who need to fix such blunders are few and far between.) $\endgroup$ – Michael E2 Nov 22 '19 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.