Basically, I need to create a list of sections and subsections which goes in action menu button in order to make some sort of navigation pane for the notebook (similar to the one you can see in "Elementary Introduction to Wolfram Language" book except that the one in the Wolfram book is hard coded with NotebookLocate command referencing to the notebooks in book directory, on the other hand, I have one document and want to put a single ActionMenu button in docked cells in order to be able to navigate the document). Although, I have succeeded in this easily with next few lines of code (I'm putting here just the essential part of the code for clarity):
First@
FrontEndExecute@
FrontEnd`ExportPacket[#, "InputText"] & /@
NotebookRead@
Cells[CellStyle -> { "Section", "Subsection"}
](*for evaluation notebook*)
The problem occurs when I have counter boxes in sections of my notebook, i.e.:
- Section 1
1.1 SubSection 1
1.2 SubSection 2 - Section 2
... and so on, the code fails to read the counter box as text and returns every counter box starting repeatedly from 0.
Here is an reproducible example for normal sections, create notebook and run the code:
NormalNB = CreateDocument[{
Cell["Normal section heading 1", "Section"],
Cell["Normal subsection heading 1.1", "Subsection"],
Cell["Normal section heading 2", "Section"],
Cell["Normal subsection heading 2.1", "Subsection"],
Cell["Normal subsection heading 2.2", "Subsection"],
Cell["Normal section heading 3", "Section"]
}];
First@
FrontEndExecute@
FrontEnd`ExportPacket[#, "InputText"] & /@
NotebookRead@
Cells[NormalNB,
CellStyle -> { "Section", "Subsection"}
]
Here is an reproducible example for sections with counters, create notebook and run the code:
CountedNB = CreateDocument[{
Cell[TextData[{CounterBox["Section"]," Normal section heading 1"}], "Section"],
Cell[TextData[{CounterBox["Section"], ".",CounterBox["Subsection"], " Normal subsection heading 1.1"}],"Subsection"],
Cell[TextData[{CounterBox["Section"]," Normal section heading 2"}], "Section"],
Cell[TextData[{CounterBox["Section"], ".",CounterBox["Subsection"], " Normal subsection heading 2.1"}],
"Subsection"],
Cell[TextData[{CounterBox["Section"], ".",CounterBox["Subsection"], " Normal subsection heading 2.2"}],
"Subsection"],
Cell[TextData[{CounterBox["Section"]," Normal section heading 3"}], "Section"]
}
];
First@
FrontEndExecute@
FrontEnd`ExportPacket[#, "InputText"] & /@
NotebookRead@
Cells[CountedNB,
CellStyle -> { "Section", "Subsection"}
]
Any sort of help is more than welcome.