20
$\begingroup$

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.

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:

GetCounterNumber["Section"]

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

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

2 Answers 2

20
$\begingroup$

Try

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

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.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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