49

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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
orome
  • 12,819
  • 3
  • 52
  • 100
  • 1
    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. – orome Nov 12 '12 at 20:53

7 Answers7

48

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."

You can only hide cells within groups, not individual cells. Here is a link to the docs for more info.

Allison B
  • 366
  • 1
  • 8
ssch
  • 16,590
  • 2
  • 53
  • 88
  • 3
    big +1, didn't know about this – Szabolcs Nov 12 '12 at 20:59
  • 2
    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. – orome Nov 12 '12 at 21:01
  • 6
    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
  • 3
    @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
  • @Mr.Wizard http://youtu.be/GiPe1OiKQuk – rm -rf Nov 12 '12 at 22:05
  • @rm-rf exactly :-) – Mr.Wizard Nov 12 '12 at 22:07
  • The double-click trick is shown in at least one, and I think several, of the screencasts in the WRI Learning Center pages at http://www.wolfram.com/broadcast/. And the Learning Center is linked directly on the Mathematica Welcome Screen ("Find Your Learning Path"). – murray Nov 13 '12 at 16:28
  • 7
    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
  • @murray oh that's even better! I'll add that to the answer – ssch Nov 13 '12 at 16:32
  • A special case of this, I'd like to make some buttons in a notebook which is used ans GUI for some package. After the buttons are made, the code producing them should be hidden. What's the proper way to achieve this? – user13253 Oct 24 '14 at 04:36
  • @qazwsx you may have a look on this AutoCollapse function – Phab Oct 05 '15 at 06:05
30

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

CreatePalette[
    Column[{
        Button["Hide code", {
            NotebookFind[SelectedNotebook[], "Output", All, CellStyle];
            FrontEndExecute[FrontEndToken[SelectedNotebook[], "SelectionCloseUnselectedCells"]]
        }],
        Button["Show code", NotebookFind[SelectedNotebook[], "Input", All, CellStyle]]
    }]
]
rm -rf
  • 88,781
  • 21
  • 293
  • 472
12

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

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Itai Seggev
  • 14,113
  • 60
  • 84
11

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.

ebergerson
  • 341
  • 1
  • 8
  • the way to quickly select all cells of the same type: Alt+Click a cell of certain type, then all cells of the same type in a notebook will be selected. – sunt05 Sep 02 '21 at 14:02
4

Alternatively, remove all inputs entirely: (CAUTION)

Copy-pasting the entire notebook to another, new notebook, then pasting the following in it will delete all input cells rather than minimizing them, such that you could send it to someone if you only wanted them to have the output:

Module[{nb},
 nb = EvaluationNotebook[];
 NotebookFind[EvaluationNotebook[], "Input", All, CellStyle];
 NotebookDelete[nb]]

Though, be careful with this method as it deletes all input cells (including itself) in the respective notebook.

Ghersic
  • 1,147
  • 7
  • 21
2

Following the solution given by Sjoerd de Vreis and Itai Seggev, I found useful define this pair of functions using their procedure as follows.

  • Function for closing all cells inputs:

    CloseAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> False] & /@ cells;
     ];
    
  • Function for recovering or made visible again:

     OpenAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> True] & /@ cells;
     ];
    

How to use it:

  1. Copy / Paste both functions above in a fresh / new notebook and run the notebook.
  2. Inside your target Notebook, at the end of your document, run CloseAllInputsCells[] . All input cells will be closed.
  3. For recovering all inputs again, run OpenAllInputsCells[]
neorvo
  • 21
  • 1
0

Compiling all the answers above into one executable code below:

CloseAllInputsCells[] := Module[{nb, cells}, nb = EvaluationNotebook[]; 
cells = Cells[nb, CellStyle -> "Input"]; SetOptions[#, CellOpen -> False]& /@ cells;];

OpenAllInputsCells[] := Module[{nb, cells}, nb = EvaluationNotebook[]; cells = Cells[nb, CellStyle -> "Input"]; SetOptions[#, CellOpen -> True]& /@ cells;];

Column[{Button["Hide Code", SelectionEvaluate[CloseAllInputsCells[]]], Button["Show Code", SelectionEvaluate[OpenAllInputsCells[]]]}]