6

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.

Community
  • 1
  • 1
jmlopez
  • 4,853
  • 4
  • 40
  • 74

1 Answers1

3

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"];
Community
  • 1
  • 1
Simon
  • 14,631
  • 4
  • 41
  • 101
  • I was afraid that the inline cell was just for style. Anyway, the idea was to just have style definitions, say like: Definition, Theorem, Lemma, Proposition, Example and so on. All I would be doing here is setting the style say to bold and it would increase their own counter. Then, by using what you showed me to create aliases I would go into any cell, for instance a text cell and I would use the alias to write say, a definition and it would display it as "Definition 1.1". This would avoid me having to change the style of the main cell to Definition and then writing the [esc]def[esc]. – jmlopez Aug 31 '11 at 11:23
  • On the other answer you posted you mentioned that maybe I should use a palette. Looking through the writing assistant palette I noticed the Math Cell drop down menu. I found it interesting that this one creates a "Text" cell and inserts contents in it. What I would really like is a button that inserts say a "definition" cell style and the text "Definition [#]" as we have previously been doing. Have you created some palettes before? If so, how can you make insert a cell of some style with some contents in it? The examples in the documentation only show how to create cells but not with content. – jmlopez Aug 31 '11 at 15:28
  • @jmlopez: I haven't created palettes before, but I won't be able to say that again! See the edit. – Simon Sep 01 '11 at 00:57
  • I figured that much. Thank you for posting it here. I find this a very nice way to make shortcuts like macros. I think this solves my problems for this question and the previous one. Now I'm facing problems exporting the math cells to html. I'll be posting something once I get an actual question. Once again, thank you for your help Simon. – jmlopez Sep 01 '11 at 02:45