How to Autonumber Cell Tags in Mathematica Notebooks?

╄→尐↘猪︶ㄣ 提交于 2019-12-19 03:57:00

问题


I tried to follow the directions on autonumbering cells in a Mathematica-8 notebook, here

http://reference.wolfram.com/mathematica/tutorial/AutomaticNumbering.html

I created a tiny notebook with four text cells

foo
qux
blancmange
bar

Placing the cursor just before foo, I then used the Insert menu, Automatic numbering item, giving me the Create Automatic Numbering Object dialog box. I chose in the Counter dropdown then item Text, clicked This counter object radio button, and Huzzah! got the number 1 just before foo, presumably referring to the first cell of type Text in the notebook. I now see

1foo
qux
blancmange
bar

and I insert my own dot space after the 1 to get a nice-looking

1. foo
qux
blancmange
bar

I do likewise with bar and see the expected

1. foo
qux
blancmange
4. bar

because bar is the fourth cell of type Text. Great. Now I want to create an independent numbering system for cells qux and blancmange. I use Cell menu, Cell Tags..., Add/Remove Cell tags... item and put the tag test on cells qux and blancmange. I use Cell menu, Cell Tags..., Show cell tags item to make sure they're there, and, sure enough, they show up in the notebook (I won't attempt to mimic the display, here, but there is a link to my notebook in a Dropbox public folder at the bottom of this question if you want to have a look). I then position the cursor before qux and use Insert menu, Automatic numbering item, this time choosing Use the counter value corresponding to the location of:, then The first cell with the tag:, then test, my user-created tag, go OK, and i get a ZERO! ditto before blancmange. My final (bad) result, after putting a little lipstick on the pig with my own dot and space, looks like

1. foo
0. qux
0. blancmange
4. bar

If try referring to a non-existent tag, I get

1. foo
XXXqux
XXXblancmange
4. bar

so Mathematica recognized something, but not what I expected or needed.

I also need to say that I tried the Item Numbered item in the Format item, but I have two problems with it: (1) I don't know how to RESTART the numbering from 1, so every block of Item Numbered cells belongs to the same sequence of numbered items. I get 1, 2, 3, here, then 4, 5, 6, later, and 7, 8, 9, even later, etc. I really need independently numbered blocks of items: 1, 2, 3 here, 1, 2, 3 later, and 1, 2, 3 even later, etc. (2) I need to number various different kinds of cells, so I really need to create my own tags and control my tracking of them.

I will be very grateful for any advice.

Here's a link to my trivial little notebook.

CellTagsAutonumbering.nb


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/8903484/how-to-autonumber-cell-tags-in-mathematica-notebooks

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