# How to copy groups of cells with NotebookWrite?

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?

All help very much appreciated!

-
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 7: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 at 4:27

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];

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
@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->{$CellContextGroupTogetherGrouping, 10000.} of which the $CellContext` is missing in the source cell. – István Zachar Sep 12 '13 at 13:12