Is there an easy way to select all initialization cells (and only those) in a Mathematica notebook?
3 Answers
My method is to define a special style in stylesheet for initialization cells. Here is my definition:
Cell[StyleData["Initial"],
CellFrame->{{1, 1}, {1, 1}},
CellMargins->{{78, 10}, {5, 10}},
Evaluatable->True,
CellGroupingRules->"InputGrouping",
TextClipboardType->"InputText",
StripStyleOnPaste->True,
PageBreakWithin->False,
GroupPageBreakWithin->False,
InitializationCell->True,
DefaultFormatType->DefaultInputFormatType,
ShowAutoStyles->True,
"TwoByteSyntaxCharacterAutoReplacement"->True,
HyphenationOptions->{"HyphenationCharacter"->"\[Continuation]"},
AutoItalicWords->{},
LanguageCategory->"Mathematica",
FormatType->InputForm,
NumberMarks->True,
LinebreakAdjustments->{0.85, 2, 10, 0, 1},
CounterIncrements->"Input",
MenuCommandKey->"i",
FontWeight->"Bold",
Background->RGBColor[1., 1., 0.8666666666666667]]
Then you can use alt+i to insert an initialization cell / convert an existing cell to Initial-style, and use alt+left-click to select all cells of this style in current notebook.
- 27,556
- 3
- 84
- 164
-
That's an interesting solution, but, as far as I can see, unfortunately not practical for my case because I often decide whether a cell should be an initialization cell after I created it. That is, I create the cell normally, and then later if I notice that I use the definition again farther away, I change it to an initialization cell. – celtschk Aug 08 '12 at 16:54
-
It's what I did. I write codes in normal
Input-style cells, and later select some of them and press alt+i to convert toInitial-style. This style definition is no more than just a wrap on aInput-style. – Silvia Aug 08 '12 at 16:57 -
Ah, OK, then I misunderstood you. In that case, it indeed looks like a solution. How exactly do I add it to my default style? – celtschk Aug 08 '12 at 17:05
-
You can edit the
Default.nbfile, but it would be more safe to create an individual style definition file, from theFormatmenu ->Edit stylesheet, create a new style with your preferred name, sayInitial, select the cell and press alt+shift+E, replace the content in the cell with the above code. Use "save as" to save the stylesheet file to hard disk. FromPalettesmenu ->install paletteto install it to your stylesheet menu. – Silvia Aug 08 '12 at 17:16 -
I don't see an option "create a new style". I get a line "Private style definitions for (current notebook name)", a dropdownlist "Choose a Style" with the only option "All", followed by "or Enter a style name" followed by an input box and an "Install Stylesheet" button. And below a large area with the text
Inheriting base definitions from "Default.nb"on the top. So where do I create the new style? – celtschk Aug 08 '12 at 17:22 -
Input your new style name in the input box followed by "Enter a style name" and press enter. – Silvia Aug 08 '12 at 17:32
-
-
Unfortunately the chat doesn't seem to work with this browser, but anyway, with your latest comment I managed to get it done, thank you very much. Works great! – celtschk Aug 08 '12 at 17:46
-
-
2Small note: new styles can inherit options from old styles. In this case, inheriting from the "Input" style may be desirable with
StyleData["Initial", StyleDefinitions->StyleData["Input"]]. This makes cleaner and clearer definitions most of the time. – dws Aug 08 '12 at 18:09 -
Unfortunately I just noticed a problem: As soon as I select the new stylesheet, all the standard styles (Title etc.) disappear. The new "Initial" style is the only one available. What might I have done wrong? – celtschk Aug 08 '12 at 18:27
-
@celtschk The first cell in your stylesheet should say something like "Inheriting base definitions from stylesheet "Default.nb" ". Perhaps you deleted that cell? – dws Aug 08 '12 at 18:32
-
@dws +1, Your way is the standard method. I copied details from the
core.nbstylesheet file for more customizable. – Silvia Aug 08 '12 at 18:34 -
@dws: No, that cell is still there. However I notice that it is slightly more indented than the new cell. I have no idea why, or if that could be the reason for the problem. – celtschk Aug 08 '12 at 18:35
-
@celtschk press ctrl + shift + E, see is the content
Cell[StyleData[StyleDefinitions -> "Default.nb"]]? – Silvia Aug 08 '12 at 18:42 -
1@celtschk If the first cell contains the contents given by Silvia, I don't know what happened. You can start over with the default stylesheet from the Format menu. Then edit the (private) stylesheet and just copy-paste Silvia's code there below the "Inheriting..." cell. Say ok when MMA asks whether to interpret this. This should add the new style. Finally, you can save and install the stylesheet as described above. – dws Aug 08 '12 at 18:59
-
I'm now at home, therefore I no longer can interact with the front end. However the saved style file has as first non-comment lines: `Notebook[{ Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Initial"],` so that's probably OK. Further tests will have to wait for tomorrow. Thank you for your help.
– celtschk Aug 08 '12 at 19:11 -
-
2There is already a
Codestyle that is an initialization cell. That has particular styling that may not appeal but in that caseStyleDefinitions->StyleData["Code"]probably makes more sense (?) – Mike Honeychurch Aug 08 '12 at 23:14 -
-
@MikeHoneychurch AHA! You're absolutely right! I never used
Codeand completely forgot it's initialization style! You should post it as an answer :) Always forget things orz – Silvia Aug 09 '12 at 01:47 -
@Rojo Thanks. I really want to learn deeply on the stylesheets things.. – Silvia Aug 09 '12 at 02:10
-
@Silvia I would have posted a stylesheet type answer based on the conversation thread -- as Rojo says stylesheets seem to be underutilized, however it seems to me the question is simply how to select all initialization cells and you can do that with keystrokes as per the last sentence of your answer. – Mike Honeychurch Aug 09 '12 at 03:23
Here is a method that can be run on existing notebooks, but has some drawbacks.
It leverages the Evaluate Initialization Cells option by redefining $Pre to set the CellTags then NotebookFind will select all the initialization cells.
First, we have a function that will set the CellTags of a cell when it is evaluated.
tagFun[input_] := (SelectionMove[InputNotebook[], All, EvaluationCell,
AutoScroll -> False];
SetOptions[NotebookSelection[], CellTags -> "init"];
SelectionMove[InputNotebook[], After, Cell, AutoScroll -> False];
input)
Then, change $Pre and evaluate only initialization cells.
oldpre = $Pre;
$Pre = tagFun;
FrontEndExecute[FrontEndToken["EvaluateInitialization"]];
Finally, change $Pre back and select the initialization cells.
$Pre = oldpre;
NotebookFind[InputNotebook[], "init", All, CellTags]
The drawbacks are:
- I could not package this into a function. In fact, the commands apparently need to be in separate cells (as I have them here).
- This evaluates the initialization cells when run.
- Overwrites
CellTagson initialization cells
- 801
- 8
- 9
-
An interesting solution, definitely worth having around, although for new notebooks Silvia's solution is superior. – celtschk Aug 08 '12 at 18:06
-
@celtschk I agree. Too bad
NotebookFinddoesn't let you search cell options. – dws Aug 08 '12 at 18:11 -
+1, for an existing nb you would like to scan your cells programmaticly. – Silvia Aug 08 '12 at 18:37
-
+1. A consolation prize would be to use
CurrentValue[EvaluationNotebook[], CellEvaluationFunction]instead of$Pre– Rojo Aug 09 '12 at 01:14
Here are a couple of new V9 approaches that use Cells. I think the Cells function makes some of these kinds of tasks simpler.
The first solution achieves the goal mentioned in a comment: How to copy all the initialization cells.
nb = EvaluationNotebook[]; (* change as desired *)
CopyToClipboard @
NotebookRead @ Select[Cells[nb], CurrentValue[#, InitializationCell] &]
The cells may be pasted in another notebook. The Cells approach allows one to do many things with cells, even if they are not actually selected in the notebook.
This creates a palette that allows you to mover a slider to select each initialization cell in turn. It won't select all of them as asked for, but one can locate them, which sometimes I want to do. (Thanks to @CarlWoll for suggesting an improvement.)
CreatePalette @ Manipulate[
initcells = Pick[#, CurrentValue[#, InitializationCell]] &@ Cells[SelectedNotebook[]];
whichCell = Clip[whichCell, {0, Length[initcells]}];
If[whichCell > 0, SelectionMove[#[[whichCell]], All, Cell] &@initcells];
whichCell,
{whichCell, 0, Length[initcells], 1},
{{initcells, {}}, None},
TrackedSymbols :> {whichCell}
]
- 235,386
- 17
- 334
- 747
-
1Faster is to use
Pick[Cells[], CurrentValue[Cells[], InitializationCell]]instead ofSelect. – Carl Woll Aug 26 '17 at 06:11 -
@CarlWoll Thanks, again. I usually use
Pick, but I probably didn't realizeCuurentValluewould be "listable" in this context and didn't think the performance difference would be significant. – Michael E2 Aug 26 '17 at 12:18 -
This doesn't show the located cell if it is within some section. But one can open all subgroups before. – BoLe Aug 07 '20 at 09:54
FrontEndExecute[FrontEndToken["EvaluateInitialization"]]. – dws Aug 08 '12 at 16:54