如何对 Mathematica 笔记本中的单元格标签进行自动编号?

发布于 2024-12-28 01:34:01 字数 2275 浏览 0 评论 0原文

我尝试按照 Mathematica-8 笔记本中的自动编号单元格的说明进行操作,此处

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

我创建了一个带有四个文本单元的小笔记本,

foo
qux
blancmange
bar

将光标放在 foo 之前,然后使用插入菜单,自动编号项,为我提供了创建自动编号对象对话框。我在 Counter 下拉列表中选择了 Text 项,单击This counter object 单选按钮,万岁!在 foo 之前得到数字 1,大概指的是笔记本中 Text 类型的第一个单元格。我现在看到了

1foo
qux
blancmange
bar

,我在 1 之后插入了自己的 dot space 以获得漂亮的效果,

1. foo
qux
blancmange
bar

我对 bar 也做了同样的操作,并看到了预期的效果

1. foo
qux
blancmange
4. bar

,因为 bar > 是 Text 类型的第四个单元格。伟大的。现在我想为单元格 quxblancmange 创建一个独立的编号系统。我使用 Cell 菜单、Cell Tags...添加/删除单元格标签... 项并放置标签 test< /code> 位于单元格 quxblancmange 上。我使用 Cell 菜单、Cell Tags...Show cell Tags 项来确保它们在那里,果然,它们显示在笔记本中(这里我不会尝试模仿显示,但如果您想查看的话,在本问题底部的 Dropbox 公共文件夹中有一个指向我的笔记本的链接)。然后,我将光标定位在 qux 之前,并使用 Insert 菜单的 Automatic numbering 项,这次选择 Use the counter value against the位置:,然后第一个带有标签的单元格:,然后test,我的用户创建的标签,OK,我得到一个零! blancmange 之前也是如此。我的最终(糟糕)结果,在用我自己的空格给猪涂上一点口红后,看起来像

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

如果尝试引用一个不存在的标签,我得到

1. foo
XXXqux
XXXblancmange
4. bar

所以 Mathematica 识别出了一些东西,但不是我期望或需要的。

我还需要说的是,我尝试了 Format 项中的 Item Numbered 项,但我有两个问题:(1)我不知道如何重新启动从 1 开始编号,因此每个 Item Numbered 单元格都属于同一编号项目序列。我在这里得到 1, 2, 3,然后是 4, 5, 6,之后是 7, 8, 9,甚至更晚,等等。我真的需要独立编号的项目块:这里是 1, 2, 3,1, 2 、 3 稍后,以及 1、2、3 甚至更晚,等等。 (2) 我需要对各种不同类型的单元格进行编号,因此我确实需要创建自己的标签并控制对它们的跟踪。

我将非常感谢任何建议。

这是我的小笔记本的链接。

CellTagsAutonumbering.nb

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2025-01-04 01:34:01

我认为您试图以错误的方式使用单元格标签
(我刚刚注意到你在上面的评论中说了这么多)!

计数器仅计算某种样式的单元格已经出现的数量(尽管计数器可以自动或手动重置/更改)。单元格标签是为了让您稍后可以引用特定的单元格,例如特定的方程或代码块等...

目前,您的单元格标记为“测试”,我们使用以下计数器 CounterBox["Code" ,“测试”]
该计数器将查找第一个标有“test”的代码样式单元格并打印其编号。由于您没有标记为“test”的代码块,因此它返回零。

如果您想要一个类似文本的单元格,它使用与普通文本单元格不同的计数器,
那么你需要创建一个新的文本样式。
转到格式>编辑样式表。然后单击default.nb 并将文本样式单元格复制到笔记本的样式表中。然后您可以将其修改为具有不同的名称(例如“Text2”)和不同的计数器。然后,您可以使用 CounterBox["Text2"] 引用这些单元格编号,就像处理普通文本单元格一样。

您还可以通过将 CounterBox 添加到 CellDingbatCellFrameLabels 来自动对它们进行编号。例如,请参阅:将单元格定义扩展到 CellFrameLabels 定义单个单元格的计数器增量

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

你的背包 2025-01-04 01:34:01

对于那些尝试做类似事情的人:
我建议执行以下操作:

提取笔记本中的所有单元格标签

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

通过查找标签循环遍历单元格:

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

希望有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文