# Hide input with multiple output cells

I want to hide my input, but not with "close cell". i also want to have fast access to the input cell.

i found Mr.Wizard's AutoCollapse function, but it doesn't work with two or more Print-functions in the input.

Also double-clicking the output cell is not the solution, because i have more output cells. merging them is just a temporary solution because i get new output cells with the next calculation of the input.

here's a code example:

Print["Rechung:"]
l01 = -350;
Print[l01, "€"]
Print["---> bezahlt"]
Print["Trinkgeld:"]
l02 = -150;
Print[l02, "€"]
Print["---> bezahlt"]
Print["Total:"]
total = 0;
Print[total, "€"]


is there no easy way?

-
What is your objection to closed cells? – Mike Honeychurch Nov 29 '13 at 23:19
I want to have a nice clear output without the input cell. So i dont have to search for the interesting things on my notebook – Phab Dec 4 '13 at 16:08
So if the cells are closed you have to search for things? Doesn't make sense. Maybe you are thinking of cell groups being closed. I am talking about cells being closed. – Mike Honeychurch Dec 4 '13 at 21:40
Maybe i didn't get the difference between cells and cell groups!? hmm ... so what i want to do is close the cell with the input and just show the cells with the output. the output consists of more than one cell. i could merge this cells and double click on them. this does exactly what i want it to do BUT on the next evaluation of my input, i got another cell group of outputs. – Phab Dec 9 '13 at 20:25
"...BUT on the next evaluation of my input, i got another cell group of outputs" -- What else would you expect? And changing that means that you do not get new outputs(?) Makes no sense. sorry. – Mike Honeychurch Dec 9 '13 at 20:37

There are two methods in this answer, the last one is the best IMO

First method

You could use

flattenThenAutoCollapse[] :=
(
SelectionMove[EvaluationCell[], All, GeneratedCell];
FrontEndTokenExecute["ExpandSelection"];
FrontEndTokenExecute["CellGroup"];
SelectionMove[EvaluationCell[], All, GeneratedCell];
FrontEndTokenExecute["SelectionCloseUnselectedCells"]
)


(Version 7 users can replace EvaluationCell with EvaluationNotebook)

And then this works

Print["hello"]
Print["bye"]
flattenThenAutoCollapse[]


Whereas this does not, as you correctly point out

Print["hello"]
Print["bye"]
AutoCollapse[]


This method does nothing in the case in which there is no output, but at least it will do no damage either.

Things can go horribly wrong if you use CellPrint though. In this case the generated cells will not be grouped with the input cell. Expanding the selection will cause a cellgroup corresponding to SubSection or something similar to be selected, if you notebook is structured this way. Or simply all cells may be selected if you have no such structure. At that point FrontEndTokenExecute["CellGroup"] will do damage.

Second method

The following is then a bit more stabile. It does however rely on inelegantly setting and restoring CellTags.

flattenThenAutoCollapse[] :=
Block[{evCell, genCells, allCells, storedTags},
evCell = EvaluationCell[];
SelectionMove[evCell, All, GeneratedCell];
genCells = SelectedCells[];
allCells = Prepend[genCells, evCell];

storedTags = CurrentValue[#, CellTags] & /@ allCells;

SetOptions[#, CellTags -> "flattenThenAutoCollapse"] & /@ allCells;
NotebookLocate["flattenThenAutoCollapse"];
FrontEndTokenExecute["CellGroup"];
SelectionMove[evCell, All, GeneratedCell];
FrontEndTokenExecute["SelectionCloseUnselectedCells"];
SetOptions[#, CellTags -> #2] & @@@
Transpose[{allCells, storedTags}];
]


I suppose this cannot be made to work in version 7, because it works with CellObjects.

Example

Print[1]
Print[2]
CellPrint[Cell["hello"]]
Print[1]
flattenThenAutoCollapse[]


closes the input cell after grouping the generated cells with the input cell, despite the fact that printed cells are normally not grouped with the input cell.

-
Note: we can check if a cell generates other cells by checking SelectedCells[SelectionMove[EvaluationCell[], All, GeneratedCells]]. However, we still cannot assume that whenever a cell generates output it is part of a cellgroup together with its output, as per the CellPrint example. – Jacob Akkerboom Jan 19 '14 at 23:33
Thanks for notifying me of this Question; I had missed it. I'll take a closer look at your code later. :-) – Mr.Wizard Jan 20 '14 at 0:18
@Jacob, What is the difference in FrontEndTokenExecute["ExpandSelection"] and FrontEndTokenExecute["CellGroup"]? I have not been able to generate different results with these. – Ben Allgeier Aug 6 '14 at 5:03
@BenAllgeier "ExpandSelection" is more general, it works with expressions. For example, if the cursor is anywhere inside the expression f[], the entire expression is selected. For mac the associated hotkey is cmd + period (.). I'm not sure why I use both these tokens, as I agree there should be no difference if I use "ExpandSelection" correctly. – Jacob Akkerboom Aug 7 '14 at 16:52