One way to get a Notebook evaluated without side-effects like opening cell groups is do not insert the results of the evaluation into this Notebook:
NotebookEvaluate[nb, InsertResults -> False];
In this case all the cells in the Notebook nb
will be evaluated without changing the nb
in any way. You can apply this method even to the EvaluationNotebook[]
itself, but in this case a little trick is necessary to prevent infinite recursion:
SetOptions[EvaluationCell[], Evaluatable -> False];
NotebookEvaluate[EvaluationNotebook[], InsertResults -> False];
SetOptions[EvaluationCell[], Evaluatable -> True];
Another option I just discovered is to put at the end of the cell group a cell with Evaluatable -> False
(for example, it may be a usual "Text"
cell or a Horizontal Line Object). In this case after using the menu item Evaluation ► Evaluate Notebook
the results of evaluation will be inserted without opening the cell group where they will appear. I found this with Mathematica version 13.1 on Windows 10 x64.
It is also interesting that with Mathematica version 8.0.4 the cell groups won't be opened even without a trailing cell with Evaluatable -> False
. Hence this behavior is new in the recent versions, and may be unintentional (a consequence of a bug). I recommend reporting this to the official support with a reference to this answer.
Further discussion
On Windows 10 x64 I observe something very similar to what lericr observes on Mac: if a Notebook contains several closed cell groups containing evaluatable cells, only the last cell group opens after Evaluation ► Evaluate Notebook
, but only if this last cell group ends with an evaluatable cell and it's sole output. It is strange, but if the last cell group ends with an evaluatable cell without output or with two or more output cells, it doesn't open! This last observation finally ensures me that the described behavior is a regression bug in the recent Mathematica versions. Please report it to the support.
From the observation that the last cell group opens only if it has a trailing evaluatable cell with an output cell, comes a workaround: before evaluating the Notebook, just delete all the output using the Cell ► Delete All Output
menu item, and only then execute Evaluation ► Evaluate Notebook
. This method can easily be automatized:
SetOptions[EvaluationCell[], Evaluatable -> False];
FrontEndTokenExecute["DeleteGeneratedCells"];
FrontEndTokenExecute["EvaluateNotebook"];
SetOptions[EvaluationCell[], Evaluatable -> True];
A nice feature of the Cell ► Delete All Output
menu item is that it deletes the cells having GeneratedCell -> True
only immediately following a cell with Evaluatable -> True
, and doesn't delete standalone cells with GeneratedCell -> True
(which you possibly have manually created by copying). This method won't have an impact on the performance, because in any case after evaluation of a cell it's existing output is removed before inserting the newly generated output (even if the new ouput is Null
, i.e. no output cell will be created).
However, if a cell prints something using CellPrint
or NotebookWrite
(but not Print
!), the target cell group will be expanded in any case. This aspect of behavior is the same as in version 8.0.4.
Evaluatable -> True
and it's previous output. It is strange, but if the last cell group ends with an evaluatable cell without output, it doesn't open! This last observation (as well as different behavior across platforms) finally ensures me that the described behavior is a regression bug in the recent Mathematica versions. Added the "BUG" header and the tag. Please report it to the support. $\endgroup$Title
,Section
,Code
but on my real example (which unfortunately I don't have any means to show it here) it happens. I will file a bug report but it will be very difficult to explain the situation there. $\endgroup$