8
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Lookup[Developer`CellInformation[#], "FirstCellInGroup"] can be useful for automatic collapsing the cell groups. $\endgroup$ Commented Nov 3, 2018 at 12:04

1 Answer 1

4
$\begingroup$

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

$\endgroup$
4
  • 1
    $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented May 3, 2014 at 21:00
  • $\begingroup$ @m_goldberg Thanks, I have edited my answer accordingly. $\endgroup$ Commented May 3, 2014 at 21:11
  • 1
    $\begingroup$ 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]. $\endgroup$
    – m_goldberg
    Commented May 3, 2014 at 21:56
  • $\begingroup$ @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. $\endgroup$ Commented May 3, 2014 at 22:04

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.