5
$\begingroup$

I like to work with initialization cells. During the different stages of development of my notebooks, I however often need to change those.

If they are scattered throughout the notebook, it is sometimes not easy to find all of them manually.

Hence I was wondering if there was a simple way to convert all initialization cells of a notebook to normal cells.

There seems to have been posted a workaround solution here, but I am looking for something simpler.

Thanks for suggestions!

$\endgroup$
  • 3
    $\begingroup$ You can select all input cells by Option-clicking (Alt-clicking on Windows?) on the cell bracket of one. Once selected, set, then unset the initialization cell flag on all of them. This is probably the simplest way. This won't affect initialization groups though. $\endgroup$ – Szabolcs Oct 14 '19 at 13:32
  • $\begingroup$ @Szabolcs Thanks a lot! I suppose it works. Strangely though, the "Evaluate→Evaluate initialization cells" option is not grayed out after I make all cells normal cells. But that may just be how it is. $\endgroup$ – Britzel Oct 14 '19 at 14:15
  • $\begingroup$ The option isn't greyed out even for an empty notebook, so it must be how it is. If anything was missed, it is the "initialization group" flag you may have set on some headings (i.e. cell groups). I usually have a single initialization group at the top of the notebook. $\endgroup$ – Szabolcs Oct 14 '19 at 14:35
  • $\begingroup$ Another way you can check is to quit the kernel and evaluate a cell that is not an intialization cell. If there are any initialization cells, Mathematica will ask you if you want to evaluate them first. $\endgroup$ – Szabolcs Oct 14 '19 at 14:36
  • $\begingroup$ Thanks again for the useful tips @Szabolcs ! Yes, having an initialization cell at the beginning is good coding practice for sure. I will try to improve! ; ) $\endgroup$ – Britzel Oct 15 '19 at 12:47
5
$\begingroup$

A simple and efficient programmatic approach:

CurrentValue[Pick[#, CurrentValue[#, InitializationCell]] &@Cells[], InitializationCell] = Inherited;

Just evaluate this statement in the Notebook where you wish to convert all InitializationCells into ordinary cells, and the InitializationCell option will be removed from the corresponding Cell[...] expressions.

Of course it can be simplified to

CurrentValue[Cells[], InitializationCell] = Inherited;

what also works, but for large Notebooks is much slower and may require a lot of system memory.

Similar effect can be achieved manually using the technique suggested in the comment by Szabolcs:

You can select all input cells by Option-clicking (Alt-clicking on Windows?) on the cell bracket of one. Once selected, set, then unset the initialization cell flag on all of them.

One drawback is that it affects only "Input" cells, while an initialization cell may have any style in the general case. An attempt to select the whole Notebook and apply this technique results in HUGE memory usage by the FrontEnd if the Notebook is large (due to the Undo/Redo system which memorizes every change), what can lead to freezing of the FrontEnd. So this technique should be used with care.

Note that initialization cells can be created also via setting the InitializationGroup option (menu item Cell ► Cell Properties ► Initialization Group). It is possible to work with it in the same way as with InitializationCell, but we must pass the name of this option as a String rather than Symbol (probably because this option is still "has not been fully integrated into the long-term Wolfram Language, and is subject to change"):

CurrentValue[Pick[#, CurrentValue[#, "InitializationGroup"]] &@Cells[], 
   "InitializationGroup"] = Inherited;
$\endgroup$
  • $\begingroup$ A very professional solution I could have never come up with. Thank you so much @Alexey Popkov ! $\endgroup$ – Britzel Oct 15 '19 at 12:50
  • $\begingroup$ @Britzel You are welcome. Thanks for the Accept. $\endgroup$ – Alexey Popkov Oct 15 '19 at 12:59

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.