How to Autonumber Cell Tags in Mathematica Notebooks?

大城市里の小女人 提交于 2019-11-30 22:02:34
Simon

I think you are trying to use cell tags in the wrong way
(and I just noticed that you said as much in a comment above)!

The counters only count how many cells of a certain style have already occurred (although the counters can be automatically or manually reset/changed). The cell tags are so that you can refer to a specific cell later on, such as a specific equation or code block etc...

At the moment, your cells tagged "test" us the following counter CounterBox["Code", "test"]. This counter will look for the first code style cell tagged with "test" and print its number. Since you have no code blocks tagged "test", it returns zero.

If you want a text-like cell that uses a different counter from the normal text cells, then you need to make up a new text style. Go to Format > Edit Stylesheet. Then click on the default.nb and copy the Text style cell into your notebook's stylesheet. Then you can modify it to have a different name (e.g., "Text2") and different counter. Then you can reference these cells numbers using CounterBox["Text2"], just like you did with the normal text cells.

You can also have them automatically numbered by adding the CounterBox to the CellDingbat or CellFrameLabels. See, for example: Extending cell definition to CellFrameLabels definition and CounterIncrements for individual cells

For those of you trying to do something similar: I would recommend doing the following:

Extract all cell tags in the notebook

tags= Union[Cases[NotebookGet[nb], (CellTags -> t_) :> t, Infinity]]

Loop through the cells by finding their tags:

Do[
NotebookLocate[tags[[i]]];
(*Some stuff you wanted to do to each cell with a certain tag*)
,{i,1,Length[tags]}
]

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!