4
$\begingroup$

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?

$\endgroup$
13
  • 2
    $\begingroup$ What is your objection to closed cells? $\endgroup$ Commented Nov 29, 2013 at 23:19
  • $\begingroup$ 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 $\endgroup$
    – Phab
    Commented Dec 4, 2013 at 16:08
  • $\begingroup$ 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. $\endgroup$ Commented Dec 4, 2013 at 21:40
  • $\begingroup$ 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. $\endgroup$
    – Phab
    Commented Dec 9, 2013 at 20:25
  • $\begingroup$ "...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. $\endgroup$ Commented Dec 9, 2013 at 20:37

1 Answer 1

4
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ Commented Jan 19, 2014 at 23:33
  • $\begingroup$ Thanks for notifying me of this Question; I had missed it. I'll take a closer look at your code later. :-) $\endgroup$
    – Mr.Wizard
    Commented Jan 20, 2014 at 0:18
  • $\begingroup$ @Jacob, What is the difference in FrontEndTokenExecute["ExpandSelection"] and FrontEndTokenExecute["CellGroup"]? I have not been able to generate different results with these. $\endgroup$ Commented Aug 6, 2014 at 5:03
  • $\begingroup$ @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. $\endgroup$ Commented Aug 7, 2014 at 16:52

Your Answer

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

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