This post derives from my question extending cell definition to cellframelabels. I\'ve been playing around with CounterIncrements
and I\'m not obtaining what I\
My guess is that counters are only counted if they are in a proper (not inline) cell. This is fine, since inline cells are only really meant for formatting purposes and not for document structure.
The counter increase works fine if you move it to the outer cell. Modifying your code above:
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] *)
Is this for something like your previous "Definition" style? If so, then why don't you have the inline cell as a plain (unstyled) cell which inherits its style from the outer cell. Then just have the counter increment in the "Definition" style, i.e. in the stylesheet? As I said above, the non-inline cell should be the one which is styled (as a "Definition", "Chapter", "Section", etc..), as that is the one that determines the document structure.
Here's a palette that will create new chapter cells and new definition cells. The latter with the built-in, non-editable counter. Note that most of the styling should be moved into the stylesheet.
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"];