CounterIncrements for individual cells

This post comes from my question of extending cell definition to cellframelabels . I played with CounterIncrements and I do not get what I expect.

As Simon said in his reply to the message I mentioned, we start by creating a counter.

 CellPrint[Cell["Setting the counter", "Text", CounterAssignments -> {{"MyCounter", 0}}]] 

Now we print this counter.

 CellPrint[Cell[ TextData[RowBox[{"MyCounter ", CounterBox["MyCounter"]}]], "Text"]] 

The result of this will be:

 MyCounter 0 

To increase the counter, we can use the CounterIncrements option as follows:

 CellPrint[Cell[TextData[RowBox[{"MyCounter ", CounterBox["MyCounter"]}]], "Text", CounterIncrements -> "MyCounter"]] 

This will give you:

 MyCounter 1 

You can enter as many times as you want, and you will see that the counter increases.

Since CounterIncrements is an option for a cell, I said to myself: "Well, what if I make a cell inside the cell and I set this parameter there?". Since I am making a cell with this option, I would expect the counter to increase. It happens?

 CellPrint[ Cell[TextData[ RowBox[{"MyCounter ", CounterBox["MyCounter"], Cell[TextData[RowBox[{"[InlineCell]"}]], "Text", CounterIncrements -> "MyCounter"]}]], "Text"]] 

Conclusion:

 MyCounter 1[InlineCell] 

I expected the output to be MyCounter 2[InlineCell] , because I told the cell inside the cell to increase the counter, but it did not.

The documentation says that CounterIncrements "was not fully integrated into the long-term Mathematica system and can be changed," but I think the information there is somewhat misleading.

I want this to be so that I can define a style that increments the counter every time it is used. But this style will be used for a cell inside another cell. Does anyone have an idea of ​​what's going on here? I am using MMA8 on Mac OS X.

+6
source share
1 answer

I assume that counters are only counted if they are in the correct (not built-in) cell. This is great because inline cells are for formatting purposes only and not for document structure.

Increasing a counter works great if you move it to an external cell. Code change 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 something like your previous Definition style ? If so, why don’t you have a built-in cell as a simple (untrue) cell that inherits its style from an external cell. Then simply enter the counter in the Definition style, i.e. In the stylesheet? As I said above, a cell other than the built-in one should be the one that is written (like "definition", "chapter", "section", etc.), since this determines the structure of the document.


Edit in response to comments:

Here is a palette that will create new chapter cells and new definition cells. The latter with a built-in, non-editable counter. Please note: most of the style should be moved to 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"]; 
+3
source

Source: https://habr.com/ru/post/896157/


All Articles