这篇文章来自我的问题 extending cell definition to cellframelabels .我一直在玩 CounterIncrements
我没有得到我所期待的。
正如西蒙在他对我提到的帖子的回答中所做的那样,我们从制作一个计数器开始。
CellPrint[Cell["Setting the counter", "Text",
CounterAssignments -> {{"MyCounter", 0}}]]
现在我们打印这个计数器。
CellPrint[Cell[
TextData[RowBox[{"MyCounter ", CounterBox["MyCounter"]}]], "Text"]]
这样做的结果将是:
MyCounter 0
要增加计数器,我们可以使用选项
CounterIncrements
如下:CellPrint[Cell[TextData[RowBox[{"MyCounter ", CounterBox["MyCounter"]}]],
"Text", CounterIncrements -> "MyCounter"]]
这会给你:
MyCounter 1
您可以根据需要多次输入,您会看到计数器增加。
自
CounterIncrements
是一个单元格的选项,我对自己说:“好吧,如果我在一个单元格中创建一个单元格并在那里设置这个选项怎么办?”。由于我正在使用此选项制作单元格,因此我希望计数器增加。这会发生吗?CellPrint[
Cell[TextData[
RowBox[{"MyCounter ", CounterBox["MyCounter"],
Cell[TextData[RowBox[{"[InlineCell]"}]], "Text",
CounterIncrements -> "MyCounter"]}]], "Text"]]
输出是:
MyCounter 1[InlineCell]
我原以为输出是
MyCounter 2[InlineCell]
因为我告诉单元格内的单元格增加计数器但它没有这样做。文档说
CounterIncrements
“尚未完全集成到长期的 Mathematica 系统中,并且可能会发生变化”但我认为那里的信息有些误导。我想要这个的原因是我可以定义一个样式,每次使用它时都会增加一个计数器。但是这种样式将用于另一个单元格内的单元格。有人知道这里发生了什么吗?我在 Mac OS X 中使用 MMA8。
最佳答案
我的猜测是,只有在正确(非内联)单元格中才会对计数器进行计数。
这很好,因为内联单元格实际上仅用于格式化目的,而不是用于文档结构。
如果您将其移动到外部单元格,则计数器增加工作正常。修改上面的代码:
CellPrint[Cell["Setting the counter to 0", "Text",
CounterAssignments -> {{"MyCounter", 0}}]]
(* Prints a cell containing: Setting the counter to 0 *)
CellPrint[Cell[
TextData[RowBox[{"MyCounter ", CounterBox["MyCounter"],
Cell[TextData[RowBox[{"[InlineCell]"}]], "Text"]}]], "Text",
CounterIncrements -> "MyCounter"]]
(* Prints a cell containing: MyCounter 1[InlineCell] *)
这是为了诸如 your previous "Definition" style 之类的东西吗? ?如果是这样,那么为什么不将内联单元格作为从外部单元格继承其样式的普通(无样式)单元格。然后在“定义”样式中,即在样式表中使用计数器增量?
正如我上面所说,非内联单元格应该是样式化的单元格(如“定义”、“章节”、“节”等),因为它决定了文档结构。
编辑回应评论:
这是一个将创建新章节单元格和新定义单元格的调色板。
后者带有内置的、不可编辑的计数器。
请注意,大部分样式应移至样式表中。
CreatePalette[With[{nb = InputNotebook[]}, {
Button["New Chapter", SelectionMove[nb, After, Cell];
NotebookWrite[nb, Cell["New Chapter", "Chapter" (* Styling is in stylesheet*)]]],
Button["New Definition", SelectionMove[nb, After, Cell];
NotebookWrite[nb, Cell[TextData[RowBox[
{Cell[TextData[
StyleBox[#, FontWeight -> "Bold"] & /@ {
"Definition ", CounterBox["Chapter"], ".", CounterBox["Definition"], ": "}],
Editable -> False, Selectable -> False, Deletable -> False],
"New definition"}]], "Definition", CounterIncrements -> "Definition",
CellFrame -> {{1, 1}, {0, 2}}, CellMargins -> {{30, 24}, {6, 6}},
CellFrameColor -> RGBColor[0, 0, 1], Background -> RGBColor[0, 1, 1]]]
]}], WindowTitle -> "Document writing palette"];
关于wolfram-mathematica - 单个单元格的计数器增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251799/