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!
3 Answers
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.
-
$\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 FangJul 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$ Jul 29, 2013 at 8:20 -
Not sure if this is what you're looking for, but the Cell
menu has an item that collapses open cells:
Here I highlighted the open input/output cells and then collapsed them to get the following:
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.
-
$\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 byAutoCollapse
, leaving the output cell unselected and then closing it bySelectionCloseUnselectedCells
, which is effectively the same as theClose Unselected Cells
button in your screenshot. I just wonder how do I rewriteAutoCollapse
to make it do the opposite way, because I didn't see any token providing the effect of SelectionClose"Selected"Cells. $\endgroup$– Leo FangJul 27, 2013 at 6:04 -
$\begingroup$ Aha! Mr. Wizard has provided another post which has the desired function. $\endgroup$– Leo FangJul 27, 2013 at 21:10
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.
Module
or something similar), which I already know (and I choose not to do for some reasons). $\endgroup$AutoCollapse
since it seems unlikely to have something fancier. $\endgroup$