5
$\begingroup$

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}];
temp

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

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

2 Answers 2

6
$\begingroup$

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]];
"Last"

Out[2]= "Last"

First

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.

$\endgroup$
0
$\begingroup$

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.

$\endgroup$

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.