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:


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

enter image description here

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:


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

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


CurrentValue[{"CounterValue", "Section"}]
  • $\begingroup$ How did you find this? $\endgroup$ – Mr.Wizard May 27 '12 at 19:44
  • 4
    $\begingroup$ @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 $\endgroup$ – Rojo May 27 '12 at 21:51
  • 1
    $\begingroup$ 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) $\endgroup$ – Rojo Jun 7 '12 at 20:14
  • 2
    $\begingroup$ @Mr.Wizard, c'est la vie $\endgroup$ – Rojo Jun 8 '12 at 13:33
  • 1
    $\begingroup$ @Mr.Wizard, I know you have v7, but I want my vote back ;D, works in v9 $\endgroup$ – 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.


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.