Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CounterIncrements for individual cells

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'm expecting.

As Simon did in his answer to the post I mentioned we start by producing 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 option CounterIncrements as follows:

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

This will give you:

MyCounter 1

You can enter that 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 within a cell and I set this option there?". Since I'm making a cell with this option I would expect for the counter to increase. Does this happen?

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

The output is:

MyCounter 1[InlineCell]

I was expecting the output to be MyCounter 2[InlineCell] because I told the cell within the cell to increase the counter but it didn't do it.

The documentation says that CounterIncrements "has not been fully integrated into the long-term Mathematica system, and is subject to change" but I think the Information there is somewhat misleading.

The reason I want this is so that I can define a style that increases a counter every time it is used. But this style will be used to a cell that is within another cell. Does someone have an idea about what is happening here? I'm using MMA8 in Mac OS X.

like image 389
jmlopez Avatar asked Aug 31 '11 01:08

jmlopez


1 Answers

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.


Edit in response to comments:

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"];
like image 69
Simon Avatar answered Oct 17 '22 05:10

Simon