Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Many of my notebooks have a similar repeating structure, which is very convenient and reliable for my workflow: a chunk of code defining a Manipulate for exploring some phenomenon, the output of the Manipulate, where the phenomenon can be explored, and then some notes or observations about the phenomenon. When I'm focused on coding, this is fine, but as my focus shifts to the phenomenon itself, the code is distracting and takes up a lot of space, so I'd like to be able to hide or collapse it.

Is there a way to hide or toggle the visibility of code, independently of the results it produces? In effect, what I'm seeking is there reverse of the default behavior, in which code and results that are grouped together can be collapsed to show just the code.

Note that I'm not seeking a way to move the code elsewhere: the point is the be able to easily move back and forth between having the code behind some data or visualization visible, and associated with the output, and having it hidden or collapsed.

share|improve this question
Come to think of it, I've never used the hide-output/keep-code behavior that's built it; but would always use a hide-code/keep-output feature. –  raxacoricofallapatorius Nov 12 '12 at 20:53
add comment

4 Answers

up vote 15 down vote accepted

Double click the output cell instead

Like so

EDIT: From murrays comment: tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group."

share|improve this answer
big +1, didn't know about this –  Szabolcs Nov 12 '12 at 20:59
Not only did I have no idea about this, but I tried it several times and got nothing to happen! What a great hidden (at least from some of us) trick. –  raxacoricofallapatorius Nov 12 '12 at 21:01
I find the comments interesting, I believe this has been in since v6, and was one of my favorite new features from that version. –  rcollyer Nov 12 '12 at 21:02
@Szabolcs I'm always amused by what is not known. It's hard to separate the arcane from the common in one's own mind I suppose. I wonder how many of the features you take for grated I have no knowledge of? It's plenty I imagine. –  Mr.Wizard Nov 12 '12 at 21:47
The double-click trick is also described in the Documentation Center page tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group." –  murray Nov 13 '12 at 16:29
show 4 more comments

You can create a palette to show/hide all the input cells in the selected notebook.

        Button["Hide code", {
            NotebookFind[SelectedNotebook[], "Output", All, CellStyle];
            FrontEndExecute[FrontEndToken[SelectedNotebook[], "SelectionCloseUnselectedCells"]]
        Button["Show code", NotebookFind[SelectedNotebook[], "Input", All, CellStyle]]
share|improve this answer
Nice! It seems it does not hide cells without output such as 1+1; –  chris Nov 13 '12 at 7:11
@chris Is there a way to hide it otherwise? I don't think you can fold a single cell. –  rm -rf Nov 13 '12 at 15:00
You could set Cell Open to False. –  Mr.Wizard Nov 13 '12 at 17:28
add comment

I have to do this all the time. I only have an annoyingly manual way to solve this. On a mac, Alt-click on the cell marker on the right for an InputCell containing the code, this should select ALL input cells. Then go to the Cell -> Cell Properties menu at the top and unselect the Open item. All the input cells should hide. On the right there should still be tiny little input cell markers. To open them follow the same procedure and select the Open again.

The answer by "rm -rf" is great! Thanks.

share|improve this answer
add comment

In version 9: suppose nb is your notebook object (this can be nb=EvaluationNotebook[], but if you're going to be closing input cells you probably want to have an auxilary notebook a choose the correct notebook from Notebooks[]). Then, to close all the Input cells, for example, do

cells = Cells[nb, CellStyle->"Input"]
SetOptions[#, CellOpen -> False] & /@ cells

To open them back up

SetOptions[#, CellOpen -> True] & /@ cells

Obviously, you can select any style cell to close and open

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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