8

During rather long computation I have intermediate steps Printed with useful information which should be collected for possible use in future inside of the evaluation Notebook. When the computation is finished the final results are printed. But there are hundreds of intermediate Prints which fill the screen and contain supporting information which is interesting to see during computation but impede reading the final results. I wish to have an ability to collapse the intermediate prints in a CellGroup.

Here is an example from the Documentation page for EvaluationMonitor:

FindRoot[x^2 - 2, {x, 1}, 
 EvaluationMonitor :> Print["x = ", x " x^2 - 2 =", x^2 - 2]]

After evaluation of the above we get intermediate results in one collapsable CellGroup (note the collapsed cell group selected on the right):

screenshot

My problem is that my computation involves several cycles with intermediate evaluations and I am forced to Print the final results after a cycle is finished. So all the prints by default belong to one cell group. It can be simulated as follows:

Do[Print@FindRoot[x^a - 2, {x, 1}, 
   EvaluationMonitor :> Print["x = ", x , ", x^", a, " - 2 = ", x^a - 2]], {a, 1, 3}]

How is it possible to have intermediate prints grouped together and collapsible?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368

1 Answers1

4

All the prints belong to one cell group because they all have style "Print" which has option CellGroupingRules->"GraphicsGrouping" as seen from the "Core.nb" stylesheet. Apparently Cells with this option are grouped together. On the other hand style "Output" has CellGroupingRules->"OutputGrouping" and such Cells aren't grouped together and also aren't grouped with "GraphicsGrouping".

So for printing the final result of each computational cycle we can use CellPrint[ExpressionCell[..., "Output"]] instead of Print and get the desired behavior (intermediate cells are collapsed by hand):

Do[CellPrint@
  ExpressionCell[
   FindRoot[x^a - 2, {x, 1}, 
    EvaluationMonitor :> 
     Print["x = ", x, ", x^", a, " - 2 = ", x^a - 2]], "Output"], {a, 
  1, 3}]

screenshot

This method is especially handy with ShowGroupOpener -> True:

SetOptions[EvaluationNotebook[], ShowGroupOpener -> True];

Do[CellPrint@
  ExpressionCell[
   FindRoot[x^a - 2, {x, 1}, 
    EvaluationMonitor :> Print["x = ", x, ", x^", a, " - 2 = ", x^a - 2]], 
   "Output"], {a, 1, 3}]

screenshot

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 1
    This is good, but it would be better with EvaluationMonitor :> Print["x = ", x , ", x^", a, " - 2 = ", x^a - 2]. With this change, one can see what the actual exponent of x is. – m_goldberg May 03 '14 at 21:00
  • @m_goldberg Thanks, I have edited my answer accordingly. – Alexey Popkov May 03 '14 at 21:11
  • 1
    Also, your solution has the drawback that the three results are not put into the output history. It is not possible to retrieve them later with Out. Because of this I think you should consider another modification -- changing FindRoot[...] to rule[a] = FindRoot[...] -- allowing latter access to the results of FindRoot as rule[1], rule[2], rule[3]. – m_goldberg May 03 '14 at 21:56
  • @m_goldberg I see your point, but this question is about output formatting. You suggest some kind of memoization which is useful but not strictly related to the question. – Alexey Popkov May 03 '14 at 22:04