# How can I convert a CounterBox into a number?

There must be a possibility to convert a CounterBox into a number. For example

CounterBox["Page", {FileNameTake[bookUrl], "someTag"}]


The suggestion

book = NotebookOpen[bookUrl];
CurrentValue[NotebookSelection[book], {"CounterValue", "Chapter"}]


does not work (if the cell is selected programmatically).

I don't know how to referene a cell with a special tag with CurrentValue, but I presume that wouldn't work either.

I don't know why I have the hint here that this question is "subjective"??

I would appreciate any help.

• You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful – Michael E2 Mar 8 '16 at 12:39
• "... the hint here ..." -- where is "here"? – m_goldberg Mar 8 '16 at 12:47
• CurrentValue[SelectedCells[], {"CounterValue", "Section"}] works fine, yet I don't know if that's the answer. The question is a little bit chaotic. – Kuba Mar 8 '16 at 12:49
• Thank you for the comments, I will follow the instruction in future posts. The "hint" came up in a yellow window shortly after I typed the title of my question. Sorry, I am rather new in this forum. – Darwin1871 Mar 8 '16 at 22:40

Your basic idea seems to work for me. I used Cells instead of NotebookSelection. I tagged a cell with the tag "C2", so that the cell looked like this:

Cell["Stuff", "Text",
CellChangeTimes->{{3.666429490958235*^9, 3.666429492522786*^9}},
CellTags->"C2"]


Then CurrentValue returns a number:

It even works in another notebook. Let nb be the other notebook object.

foo = CurrentValue[
First@Cells[nb, CellTags -> "C2"], {"CounterValue", "Chapter"}]
(*  2  *)


Note book selection works, too. Below I had selected the "Hi there!" cell.

foo = CurrentValue[
NotebookSelection[nb], {"CounterValue", "Chapter"}]
(*  1  *)

• Thank you Michael for your very well documented answer. Finally i tried this on my raspberry pi with version 10. And it works! But not on my version 9!! with Windows. What can be the reason? A lot of things with CurrentValue do work, but nothing with counters like Section, Page, etc. What might be the reason? Is there a bug? Or might it be some fancy Option somewhere? Or the fact that I have also an older version of mathematica installed on my system? I read somewhere that this might cause some trouble (but I never encountered any). – Darwin1871 Mar 8 '16 at 23:18
• Hallo, maybe I shouldn't have marked this as answered. I still need a workaround because CurrentValue[] command doesn't work in version 9. And also i wonder if there is a possibility to get this NUMBER, which is generete by the CounterBox[] command and which is to be seeeen in the output. I tried a lot of things, even CopyToClipboard[%] an similar things. When I try to copy the selected number I always get the underlying CounterBox[] command. Has someone an idea? Don't want to post a question again. – Darwin1871 Mar 10 '16 at 17:32
• @Darwin1871 Thanks for letting me know. Feel free to unaccept, if it doesn't solve your problem. I can't explore V9 as I have V10. You may edit the question to clarify your needs. Maybe someone with V9 will be able to help. My docs say that CurrentValue hasn't been updated since V9, so I'm surprised it doesn't work. Or maybe the docs are wrong. Who knows. – Michael E2 Mar 10 '16 at 17:40