如何获取(不显示)部分编号

时间:2011-05-06 01:27:57

标签: wolfram-mathematica

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

修改
我想要使​​用它的原因是here

2 个答案:

答案 0 :(得分:5)

使用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"}]]

答案 1 :(得分:1)

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

创建一个可以玩的笔记本:

nb = CreateDocument[{
    Cell["My Title", "Title"],
    Cell["My first section", "Section"],
    Cell["My second section", "Section"],
    Cell[TextData[{"Section ",
       CounterBox["Section"]}], "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}]