# Obtaining the number in CounterBox

Is there a way of obtaining the current number of a counterbox? Say for instance that in a new notebook you create 3 section cells. Then by evaluating the command:

CellPrint[Cell[TextData[CounterBox["Section"]]]]


You can see now many cells with the style "Section" you have created up to the point at which you evaluated the command.

Is there a way of obtaining the the number of "Sections" created without having to print a cell displaying a CounterBox? I would like to do something like:

GetCounterNumber["Section"]


and it should return the total number of Sections created. It should behave similarly for subsections and other cells that have counters.

-
see my edit. You should probably unaccept so people give it a shot –  Rojo Jun 9 '12 at 14:28

Try

CurrentValue[{"CounterValue", "Section"}]

-
How did you find this? –  Mr.Wizard May 27 '12 at 19:44
@Mr.Wizard. The sequence took about 2-3 minutes: 1st minute) Have the impulse of giving a useless answer in a comment, of the lines of TextRecognize[ Rasterize[ StyleBox[RowBox[{"Number ", CounterBox["Section"]}], FontSize -> 40]]] 2') Realizing that made no sense, didn't work, 0 became "O", etc. 3') Decided to try for real. Smelled CurrentValue had a chance. Went to the CurrentValue command list SE question. Ctrl+F, Counter. Found 2. Tried the most obvious, didn't work. Tried the second one. EUREKA Unfortunately, no magic –  Rojo May 27 '12 at 21:51
To @MrWizard and all those of you who believed in me. In a recent answer to an old MrWizard's question, I realised that this code doesn't do what I implied. It gives you the TOTAL number of Section cells, not the number up to the point in question. This answer as well as that old MrWizard's one are still waiting for someone to, perhaps, understand how to get CurrentValue[{"CounterValue", "Section"}] to return something other than 1. My sincerest apologies. Rojo... (someone call John Fultz) –  Rojo Jun 7 '12 at 20:14
@Mr.Wizard, c'est la vie –  Rojo Jun 8 '12 at 13:33
@Mr.Wizard, I know you have v7, but I want my vote back ;D, works in v9 –  Rojo Jan 22 '13 at 22:48

This now works in version 9:

CurrentValue[{"CounterValue", counterName}]


It determines the position from the cell which runs the evaluation. So a Shift+Enter evaluation will look at the position of the Input cell, a button evaluation will look at the position of the button, a Dynamic will look at the position of the Dynamic, etc.

-