如何获取(不显示)节号

发布于 2024-11-05 12:49:31 字数 336 浏览 0 评论 0原文

通过使用 Insert > 插入自动编号对象 CounterBox["Section"],可以对笔记本部分进行自动编号。自动编号... 菜单。但是,该对象仅控制节号的显示,我想获取其数值以在程序中使用。知道该怎么做吗?

编辑
我想使用它的原因在这里概述

Notebook sections can be automatically numbered by inserting the automatically numbering object CounterBox["Section"] using the Insert > Automatic Numbering... menu. However this object only controls the display of the section number and I would like to get its numerical value to use in a program. Any idea how to do that?

Edit
The reason I want to use this is outlined here.

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

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

发布评论

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

评论(2

云淡风轻 2024-11-12 12:49:31

用 TagBox 和已知标签包裹 CounterBox:

Cell[BoxData[TagBox[CounterBox["Section"], "tag"]], "Text"]

然后使用 FrontEnd`ObjectContents 将所有 DynamicBox/CounterBox/ValueBox 转换为文字并选出该 TagBox 的值:

x = First@Cases[FrontEnd`ObjectContents[nb, True], TagBox[x_, "tag"] :> x, \[Infinity]]

如果您只想知道某种类型的计数器有多少个有你可以做的:

x = FE`Evaluate[CurrentValue[{"MaxCounterValue", "Section"}]]

Wrap the CounterBox with a TagBox and a known tag:

Cell[BoxData[TagBox[CounterBox["Section"], "tag"]], "Text"]

Then use FrontEnd`ObjectContents to convert all DynamicBox/CounterBox/ValueBox to literals and pick out the value of that TagBox:

x = First@Cases[FrontEnd`ObjectContents[nb, True], TagBox[x_, "tag"] :> x, \[Infinity]]

If all you want to know is how many of a certain type of counters there are you can do:

x = FE`Evaluate[CurrentValue[{"MaxCounterValue", "Section"}]]
夕嗳→ 2024-11-12 12:49:31

必须有更好的方法来做到这一点,但如果我明白你想做什么,这里有一些可行的方法。

创建一个笔记本来使用:

nb = CreateDocument[{
    Cell["My Title", "Title"],
    Cell["My first section", "Section"],
    Cell["My second section", "Section"],
    Cell[TextData[{"Section ",
       CounterBox["Section"]}], "Section"]}];

选择最后一个单元格,该单元格恰好是“部分”单元格。

SelectionMove[nb, After, Notebook];
SelectionMove[nb, Previous, Cell];

倒数。

cnt = sectionCnt = c = 0;
While[True, Print[c];
  c = NotebookRead[nb];
  If[c === {}, Break[]];
  If[c[[2]] == "Section", sectionCnt++];
  cnt++;
  SelectionMove[nb, Previous, Cell]];

现在 sectionCnt 应该包含您想要的值。您可以轻松地回到原来的位置:

Do[SelectionMove[nb, Next, Cell], {cnt}]

There's got to be a better way to do this, but here's something that works, if I understand what you want to do.

Create a notebook to play with:

nb = CreateDocument[{
    Cell["My Title", "Title"],
    Cell["My first section", "Section"],
    Cell["My second section", "Section"],
    Cell[TextData[{"Section ",
       CounterBox["Section"]}], "Section"]}];

Select the last cell, which happens to be a Section cell.

SelectionMove[nb, After, Notebook];
SelectionMove[nb, Previous, Cell];

Count backwards.

cnt = sectionCnt = c = 0;
While[True, Print[c];
  c = NotebookRead[nb];
  If[c === {}, Break[]];
  If[c[[2]] == "Section", sectionCnt++];
  cnt++;
  SelectionMove[nb, Previous, Cell]];

Now sectionCnt should hold the value that you want. You can move back to where you were easily enough:

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