Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

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

2 Answers 2

Try

CurrentValue[{"CounterValue", "Section"}]
share|improve this answer
    
How did you find this? –  Mr.Wizard May 27 '12 at 19:44
3  
@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
1  
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
2  
@Mr.Wizard, c'est la vie –  Rojo Jun 8 '12 at 13:33
1  
@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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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