我的猜测是,只有在正确的(非内联)单元格中的计数器才会被计数。
这很好,因为内联单元格实际上仅用于格式化目的,而不用于文档结构。
如果将其移至外部单元格,则计数器增加效果很好。修改上面的代码:
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] *)
这是为了类似的事情吗你以前的“定义”风格?如果是这样,那么为什么不将内联单元格作为普通(无样式)单元格,它从外部单元格继承其样式。然后只需在“定义”样式中(即在样式表中)增加计数器增量?
正如我上面所说,非内联单元格应该是有样式的单元格(如“定义”、“章节”、“部分”等),因为它决定了文档结构。
编辑回应评论:
这是一个调色板,将创建新的章节单元和新的定义单元。
后者带有内置的、不可编辑的计数器。
请注意,大部分样式应移至样式表中。
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"];