The command Do[Print[i], {i, 1, 3}] does what you expect: creates 3 output lines of 1, 2 and 3, in that order.

If one merges these output cells, then evaluates the original Do command, the output is now 3, then 2, then 1. When one deletes the output cells, the regular behavior returns.

It isn't that the Do loop is running backwards. When one does "the same thing" with

temp = {}
Do[AppendTo[temp, i];
Print[i], {i, 1, 3}];

the order of the Print command is reversed, but the contents of temp are still {1, 2, 3}, not {3, 2, 1}.

Why does this happen? Does Print write to a queue that is sometimes dumped in reverse order?

I am running Mathematica 11.0.0 on Windows 10

  • $\begingroup$ You can see how the printing order is affected by trying with Do[Pause[1]; Print[i], {i, 1, 3}]. $\endgroup$
    – J. M.'s torpor
    Sep 25 '18 at 10:40
  • $\begingroup$ Forgot to add: running Mathematica 11.0.0 on Windows 10. $\endgroup$
    – GregH
    Sep 25 '18 at 11:31
  • $\begingroup$ I can reproduce it in M11.3 on macOS. $\endgroup$
    – Szabolcs
    Sep 25 '18 at 12:28
  • $\begingroup$ Me, too (after Szabolcs explained to me what to do...) $\endgroup$ Sep 25 '18 at 12:34
  • $\begingroup$ Related: "Is it possible to Print expressions in reverse order?" $\endgroup$ Oct 16 '19 at 12:24

The reason this happens is described in ref/CellPrint, in the possible issues section:

Setting CellAutoOverwrite and GeneratedCell can cause unexpected results:

In[1]:= CellPrint[Cell["First", "Print", CellAutoOverwrite -> False, 
   GeneratedCell -> False]];

Out[2]= "Last"


Merging the cells sets these two options:

  • it is no longer a generated cell,
  • and, theoretically if the user merged the cells, the user doesn't want them overwritten.

Tricky behavior, but I think it is within the scope of what's documented. A solution to improve the behavior would probably be appreciated, but for now it seems documented and expected.


Important update

The system doesn't auto-overwrite cells having Evaluatable->True even if they have the options CellAutoOverwrite->True and GeneratedCell->True set (checked with versions 8.0.4, 11.3 and 12.0). AFAIK it isn't documented.

Original post

I would like to add additional references and a discussion to the insightful answer by user6014.

From the Documentation page for OutputAutoOverwrite (emphasis is mine):

  • With the default setting OutputAutoOverwrite->True, the output from a command overwrites any previously existing output. Only those output cells that have CellAutoOverwrite set to True can be overwritten.

  • With the setting OutputAutoOverwrite->False, output from each evaluation of a command is printed in the notebook. The output is printed directly after any previously generated output.

  • CellAutoOverwrite is typically set for cells in "Output" style.

From the Documentation page for GeneratedCell (emphasis is mine):

GeneratedCell is an option for Cell that indicates whether the cell was generated from the kernel.

  • The setting for GeneratedCell is used to determine which cells should be considered as Wolfram Language output.

From the Documentation page for CellAutoOverwrite (emphasis is mine):

CellAutoOverwrite is an option for Cell which specifies whether an output cell should be overwritten by new output when the preceding input cell is evaluated.

  • Any sequence of cells with GeneratedCell->True is assumed to correspond to output.

  • With CellAutoOverwrite->False, new output is placed directly after the preceding input, pushing down pre-existing output.

So we seemingly have the following vocabulary assumed in the Documentation:

  • "output cell" – a Cell with option GeneratedCell -> True set

  • "output" – any sequence of such Cells

  • "new output" – output generated during evaluation of the current EvaluationCell[]

  • "pre-existing output" – output (directly under the EvaluationCell[]) generated before beginning of the current evaluation

  • "Output" – is a name of built-in style which typically sets CellAutoOverwrite -> True and GeneratedCell -> True (we can change it via custom stylesheet)

And the rules which one can deduce from the above observations and the experience:

  • If after input cell isn't present a Cell with CellAutoOverwrite -> True, then new output will be placed directly after the input cell pushing down all subjacent pre-existing cells

  • If generated new Cell has GeneratedCell -> False, it won't be considered as a part of new output, as well as a part of output at all

From these two rules directly follows that when an input cell generates Cells with both CellAutoOverwrite -> False and GeneratedCell -> False, they will be printed immediately after it in the reverse order.


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.