3
$\begingroup$

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.:

  1. Section 1
    1.1 SubSection 1
    1.2 SubSection 2
  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.

$\endgroup$
2
$\begingroup$

CurrentValue knows "CounterValue": Obtaining the number in CounterBox

And can be used with CellObjects:

Composition[
  Map[First@FrontEndExecute@FrontEnd`ExportPacket[#, "InputText"] &],
  Map[(NotebookRead[#] /.  CounterBox[style_] :> 
       ToBoxes@CurrentValue[#, {"CounterValue", style}]) &
  ],
  Cells[#, CellStyle -> {"Section", "Subsection"}] &
] @ CountedNB

1 Normal section heading 1

1.1 Normal subsection heading 1.1

2 Normal section heading 2

2.1 Normal subsection heading 2.1

2.2 Normal subsection heading 2.2

3 Normal section heading 3

Further reading:

Outline of notebook sections

$\endgroup$
  • $\begingroup$ As easy as that. Thank you, just made my day. $\endgroup$ – Milos Cipovic Jun 1 '17 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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