Let's create two notebooks external.nb and primary.nb. Then call the external.nb notebook from the primary.nb and investigate values of Out[ ]. The context of the external notebook is not important, however for the demonstration of the strange behavior it should contain number of cells. My external.nb notebook contains 4 cells:





The primary notebook contains the single cell which calls the external notebook from the primary. If we put both notebooks in the same directory, the cell is

NotebookEvaluate @ FileNameJoin[
  { NotebookDirectory[EvaluationNotebook[]], "external.nb"}

Open the primary.nb and evaluate the cell which calls the evaluation of the external.nb.

The output yields

Out[1] = d

So it returns output of the last cell of the called notebook.

Now refer to input %4 of primary.nb. I would expect an empty output, since at the moment there is no %4 out in the primary notebook. However the result is the same d.


?? Out[]

we see the primary.nb knows all Out[] values of the external.nb notebook. Bug or feature?

  • $\begingroup$ The kernel state (including In/Out values) is not local to notebooks. This is expected. $\endgroup$
    – Szabolcs
    Mar 19, 2019 at 12:05
  • $\begingroup$ That is clear, however why then not to continue the enumeration from the last Out[ ] value the kernel knows in the new notebook? I find this confusing. $\endgroup$
    – Acus
    Mar 19, 2019 at 12:17
  • $\begingroup$ Right, now I see what you mean. primary.nb keeps counting up from 1, yet higher input line values are already set in Out thanks to external.nb. It is indeed strange. $\endgroup$
    – Szabolcs
    Mar 19, 2019 at 12:22

1 Answer 1


I am guessing that this has to do with the following note in the NotebookEvaluate documentation:

The cells of the notebook are evaluated in a dialog subsession.

Here is an example session demonstrating similar behaviour:

enter image description here

See Dialog if you are not familiar with it.

Within the dialog, $Line keeps getting incremented as usual, and Out values are set. But once the dialog finishes, $Line is reset to its original value.

This is documented:

Dialog automatically localizes the values of $Line, $MessageList, and $Epilog.

Side note: A dialog is something you would normally encounter not when explicitly entering it, but when interrupting evaluation or when debugging (say, you set the debugger to break on a message/assert). Try e.g. Do[Pause[1], {i, 30}], then interrupt using Alt-, (Option-Command-. on Mac). Now you can evaluate i to check its value, or examine the kernel state in other ways. Exit the dialog using Return[]. It is natural that one would not want these evaluations to influence $Line once the dialog has finished.

  • $\begingroup$ Since the behavior is documented I conclude this is the intended design feature. I use NotebookEvaluate for package development, because it is much more convenient way to add new definitions and navigate in the many cell notebook than in plain .m file. I think I will try to add Clear[Out] as the last cell in the external.nb. $\endgroup$
    – Acus
    Mar 20, 2019 at 7:29
  • $\begingroup$ @user18792 You can edit an .m file as if it were a notebook. You can add section cells and text cells. You can close and open section cells. Try it. $\endgroup$
    – Szabolcs
    Mar 20, 2019 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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