Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
    
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

1 Answer 1

up vote 2 down vote accepted

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

share|improve this answer
    
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->{$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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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