How to get (not display) section number

Notebook sections can be automatically numbered by inserting an automatic numbering object CounterBox["Section"]using the menu Insert > Automatic Numbering.... However, this object only controls the display of the section number, and I would like to get its numerical value for use in the program. Any idea how to do this?

Edit
The reason I want to use this is indicated here .

+3
source share
2 answers

Wrap CounterBox with TagBox and famous tag:

Cell[BoxData[TagBox[CounterBox["Section"], "tag"]], "Text"]

Then use FrontEnd`ObjectContents to convert all DynamicBox / CounterBox / ValueBox to literals and select the value of this TagBox:

x = First@Cases[FrontEnd`ObjectContents[nb, True], TagBox[x_, "tag"] :> x, \[Infinity]]

, , , :

x = FE`Evaluate[CurrentValue[{"MaxCounterValue", "Section"}]]
+5

, - , , .

:

nb = CreateDocument[{
    Cell["My Title", "Title"],
    Cell["My first section", "Section"],
    Cell["My second section", "Section"],
    Cell[TextData[{"Section ",
       CounterBox["Section"]}], "Section"]}];

, .

SelectionMove[nb, After, Notebook];
SelectionMove[nb, Previous, Cell];

.

cnt = sectionCnt = c = 0;
While[True, Print[c];
  c = NotebookRead[nb];
  If[c === {}, Break[]];
  If[c[[2]] == "Section", sectionCnt++];
  cnt++;
  SelectionMove[nb, Previous, Cell]];

sectionCnt . , :

Do[SelectionMove[nb, Next, Cell], {cnt}]
+1

All Articles