7
$\begingroup$

In Mathematica the default behavior of output cells is to pop up after the kernel has processed the input cells and got a result. I know I could add ; in the end of input cells to disable output cells, which are often too messy in the middle of my calculations and usually I don't really care what they are, from appearing. However sometimes I need to go back and check some stuff, so it is still necessary to keep those intermediate results there. Then my questions is that is it possible to "fold" output cells? By "folding" I mean it would look as if I manually double click the cell, so the output cell will be hidden and an arrow appears next to the input cell. I tried to find some related settings described in this post, but nothing useful there. Thanks!

$\endgroup$
8
  • $\begingroup$ I'm sorry @Nasser, I don't get it either...It seems like you're talking about putting a series of steps in a single function (using Module or something similar), which I already know (and I choose not to do for some reasons). $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 4:02
  • $\begingroup$ By the way, the jumping cursor you're talking about could be achieved (though taking one more step) by pressing ↓ after evaluating a cell and then pressing shift+enter. Is it what you want? $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 4:04
  • $\begingroup$ yeah mathematica is inefficient in this indeed... $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 4:11
  • 1
    $\begingroup$ This is nearly a duplicate of (3235) but that doesn't address folding all output automatically without using the special function. $\endgroup$
    – Mr.Wizard
    Jul 27, 2013 at 6:27
  • $\begingroup$ @Mr.Wizard you are always our wizard! It would be great if it could be done automatically without any special function, but I'm almost satisfied with AutoCollapse since it seems unlikely to have something fancier. $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 21:19

3 Answers 3

8
$\begingroup$

I'm slow, but eventually I get there. ;-) You need CellEpilog to make the behavior automatic on evaluation of any cell. Importing the code from my autoFoldOutput function:

SetOptions[EvaluationNotebook[], 
 CellEpilog :> 
   (SelectionMove[EvaluationNotebook[], All, EvaluationCell];
    FrontEndTokenExecute["SelectionCloseUnselectedCells"])
]

Simply evaluate that in your Notebook and all output groups will be automatically closed when they are created. You may replace EvaluationNotebook[] with $FrontEndSession if you want to affect all Notebooks during the session or with $FrontEnd to persistently affect all Notebooks.

$\endgroup$
3
  • $\begingroup$ This is awesome Mr. Wizard! One quick question: Will the effect of using $FrontEnd continue even the kernel is closed and then reactivated? $\endgroup$
    – Leo Fang
    Jul 29, 2013 at 1:52
  • 2
    $\begingroup$ @Leo I'm glad you like it. :-) Yes, settings to $FrontEnd should persist between sessions; this is the same as setting them in the Global Preferences item of the Option Inspector (but don't try setting this complicated definition from there). They can be viewed and cleared from there as well (as text will show everything), so you don't have to worry about not being able to restore default behavior. $\endgroup$
    – Mr.Wizard
    Jul 29, 2013 at 8:20
  • $\begingroup$ Good to know! Thanks. :) $\endgroup$
    – Leo Fang
    Jul 29, 2013 at 17:41
5
$\begingroup$

Not sure if this is what you're looking for, but the Cell menu has an item that collapses open cells:

menu

Here I highlighted the open input/output cells and then collapsed them to get the following:

collapsed

If your question is about automatically and immediately collapsing every output cell, then that's a different story. It will most likely involve this answer.

$\endgroup$
2
  • $\begingroup$ Thanks Jens! I think Mr. Wizard's AutoCollapse could provide the effect I expect! It seems that when an output cell is generated, the selection is moved to the input cell by AutoCollapse, leaving the output cell unselected and then closing it by SelectionCloseUnselectedCells, which is effectively the same as the Close Unselected Cells button in your screenshot. I just wonder how do I rewrite AutoCollapse to make it do the opposite way, because I didn't see any token providing the effect of SelectionClose"Selected"Cells. $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 6:04
  • $\begingroup$ Aha! Mr. Wizard has provided another post which has the desired function. $\endgroup$
    – Leo Fang
    Jul 27, 2013 at 21:10
2
$\begingroup$

Here are two other alternatives:

SetOptions[EvaluationCell[], 
 "GeneratedCellStyles" -> {"Output" -> {"Output", CellOpen -> False}}]

And:

Dynamic[
 SetOptions[EvaluationCell[], CellOpen -> False];
 myHiddenContent
 ]

You could even make a one-shot, so that the cell initially displays unopened, but the user has the option to manually open it, without Dynamic getting in the way:

Evaluate[closeIt = True];
Dynamic[
 If[closeIt,
  SetOptions[EvaluationCell[], CellOpen -> False];
  closeIt = False
  ];
 myHiddenContent
 ]

(A more complete solution would use DynamicModule too)

The first solution prevents any "flashing" of an output cell that is temporarily shown before then being hidden. If you put the SetOptions piece of code as the first line in Dynamic in the second solution, in practice, I don't notice any flashing. Using other methods that don't close the cell until after it has been shown with content in it might result in flashing of temporary contents on the screen.

$\endgroup$

Your Answer

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

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