4
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ 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? $\endgroup$
    – Kuba
    Sep 11, 2013 at 7:22
  • $\begingroup$ @Kuba: thanks for your comment. Anyone have any answer on this? Thanks! $\endgroup$
    – Gabriel
    Sep 11, 2013 at 13:25
  • $\begingroup$ @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. $\endgroup$ Sep 12, 2013 at 11:11
  • $\begingroup$ @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. $\endgroup$
    – Kuba
    Sep 12, 2013 at 11:16
  • $\begingroup$ @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. $\endgroup$ Jun 17, 2016 at 4:27

1 Answer 1

2
$\begingroup$

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

$\endgroup$
7
  • $\begingroup$ I missed this because when I grouped cells with Ctrl+Shift+G then still AbsoluteOptions[EvaluationNotebook[]] -> {CellGrouping -> Automatic} $\endgroup$
    – Kuba
    Sep 12, 2013 at 12:42
  • $\begingroup$ @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. $\endgroup$ Sep 12, 2013 at 12:47
  • $\begingroup$ You mean Automatic!=Automatic? because this explains why the following has failed: CreateDocument[NotebookRead /@ Cells[#], AbsoluteOptions[#]] &@ EvaluationNotebook[] $\endgroup$
    – Kuba
    Sep 12, 2013 at 12:54
  • 1
    $\begingroup$ @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. $\endgroup$ Sep 12, 2013 at 13:12
  • 1
    $\begingroup$ @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). $\endgroup$ Oct 26, 2019 at 8:06

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.