4

Bug fixed
CellGrouping -> Manual isn't needed anymore for copying cell groups. They are copied correctly by default.


I'm trying to use:

NotebookWrite[nb, NotebookRead[EvaluationNotebook[]];

to copy cells from the EvaluationNotebook[] to a new notebook nb. For this, I move around in the EvaluationNotebook[] using different applications of SelectionMove[nb, ...].

Now, I would like to copy the exact same cell groupings of the current document as well, and this doesn't seem to happen. In the newly created notebook nb, I just see consecutive cells, without any grouping. Why is that? How can copying of groups be achieved?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Gabriel
  • 1,877
  • 12
  • 22
  • I'm really curious. The information about CellGroupingRules is preserved. But even with CreateDocument[NotebookRead /@ Cells[#], AbsoluteOptions[#]] &@ EvaluationNotebook[] they are not grouped properly. What is what makes them grouped then? – Kuba Sep 11 '13 at 07:22
  • @Kuba: thanks for your comment. Anyone have any answer on this? Thanks! – Gabriel Sep 11 '13 at 13:25
  • @Kuba I was about to edit this, but I see you have already edited. Maybe you can edit Nobetook->Notebook? It is an interesting question. In a .nb file you can see that there is some information about grouping. You can also set cell grouping manually. I've played around with all this a little bit when looking at Gabriels previous question (I think). If you don't see another way maybe we have to look at editing the notebook file. – Jacob Akkerboom Sep 12 '13 at 11:11
  • @JacobAkkerboom done. It would be perfect to avoid modifying notebook file. I thought it should be available via standard notebook related functions. We will see, unfortunatelly now I can't focus on this more. – Kuba Sep 12 '13 at 11:16
  • @Kuba I just have checked your code CreateDocument[NotebookRead /@ Cells[#], AbsoluteOptions[#]] &@ EvaluationNotebook[] with version 10.4.1 and found that all cell groups (including manually created) are preserved with the default CellGrouping -> Automatic. Looks like it was a bug which is currently fixed. – Alexey Popkov Jun 17 '16 at 04:27
  • @AlexeyPopkov Thanks for feedback. I would like to read a good tutorial about cell grouping management. I'm always confused ;) – Kuba Jun 17 '16 at 05:41

1 Answers1

2

To make it work, you have to set the option CellGrouping -> Manual either for the target notebook, or globally in the Option Inspector (Ctrl+Shift+O). Then the following code works as expected:

nb = CreateDocument[{}, CellGrouping -> Manual];
SelectionMove[EvaluationNotebook[], All, Notebook];
NotebookWrite[nb, NotebookRead[EvaluationNotebook[]]];

enter image description here

István Zachar
  • 47,032
  • 20
  • 143
  • 291
  • I missed this because when I grouped cells with Ctrl+Shift+G then still AbsoluteOptions[EvaluationNotebook[]] -> {CellGrouping -> Automatic} – Kuba Sep 12 '13 at 12:42
  • @Kuba Grouping only applies to the EvaluationNotebook[]. When one copies cells from it the target notebook won't inherit the Cellgrouping option value, thus it will be the default Automatic. – István Zachar Sep 12 '13 at 12:47
  • You mean Automatic!=Automatic? because this explains why the following has failed: CreateDocument[NotebookRead /@ Cells[#], AbsoluteOptions[#]] &@ EvaluationNotebook[] – Kuba Sep 12 '13 at 12:54
  • 1
    @Kuba Yes, that is indeed strange. I don't know ATM what exactly stores the grouping preferences in the source notebook if not the $FrontEnd, $FrontEndSession or EvaluationNotebook[] (all return Automatic). The only thing different in the target is if you hit Ctrl+Shift+E on the non-grouped cells, you'll see something like CellGroupingRules->{$CellContext`GroupTogetherGrouping, 10000.} of which the $CellContext is missing in the source cell. – István Zachar Sep 12 '13 at 13:12
  • Yes I've seen this part too but I thought is shouldn't matter. This might be a key byt I don't know how to use it :) – Kuba Sep 12 '13 at 13:15
  • @Kuba: Thanks for the comment. I will check it out! – Gabriel Sep 12 '13 at 19:20
  • 1
    @IstvánZachar Indeed $CellContext was the source of the trouble: currently manually grouped cells get CellGroupingRules->{"GroupTogetherGrouping", 10000.} what works as expected, but changing it to CellGroupingRules->{$CellContext`GroupTogetherGrouping, 10000.} breaks manual grouping. Added the bugs tag to the OP (CellGrouping -> Manual currently isn't needed). – Alexey Popkov Oct 26 '19 at 08:06