单个单元格的计数器增量 [英] CounterIncrements for individual cells

查看:25
本文介绍了单个单元格的计数器增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章源自我的问题将单元格定义扩展到 cellframelabels.我一直在玩 CounterIncrement,但没有得到我期望的结果.

正如西蒙在回答我提到的帖子时所做的那样,我们首先制作了一个计数器.

CellPrint[Cell["设置计数器", "Text",CounterAssignments ->{{"MyCounter", 0}}]]

现在我们打印这个计数器.

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

这样做的结果是:

MyCounter 0

要增加计数器,我们可以使用选项 CounterIncrements 如下:

CellPrint[Cell[TextData[RowBox[{"MyCounter", CounterBox["MyCounter"]}]],文本",反增量 ->我的计数器"]]

这会给你:

MyCounter 1

您可以根据需要多次输入,您会看到计数器增加.

由于 CounterIncrements 是单元格的一个选项,我对自己说:好吧,如果我在一个单元格中创建一个单元格并在那里设置这个选项呢?".由于我正在使用此选项制作单元格,因此我希望计数器增加.这种情况会发生吗?

CellPrint[单元格[文本数据[RowBox[{"MyCounter", CounterBox["MyCounter"],单元格[TextData[RowBox[{"[InlineCell]"}]], "Text",反增量 ->我的计数器"]}]],文本"]]

输出为:

MyCounter 1[InlineCell]

我期望输出为 MyCounter 2[InlineCell] 因为我告诉单元格内的单元格增加计数器但它没有这样做.

文档说CounterIncrements尚未完全集成到长期的 Mathematica 系统中,并且可能会发生变化",但我认为那里的信息有些误导.

我想要这个的原因是我可以定义一个样式,每次使用时都会增加一个计数器.但是这种样式将用于另一个单元格内的单元格.有人知道这里发生了什么吗?我在 Mac OS X 中使用 MMA8.

解决方案

我的猜测是,只有在正确(而非内联)单元格中才会对计数器进行计数.这很好,因为内联单元格实际上仅用于格式化目的,而不用于文档结构.

如果您将计数器移动到外部单元格,则计数器增加工作正常.修改上面的代码:

CellPrint[Cell["将计数器设置为 0", "Text",CounterAssignments ->{{"MyCounter", 0}}]](* 打印一个包含以下内容的单元格:将计数器设置为 0 *)单元打印[单元格[TextData[RowBox[{"MyCounter", CounterBox["MyCounter"],单元格[TextData[RowBox[{"[InlineCell]"}]], "Text"]}]], "Text",反增量 ->我的计数器"]](* 打印一个包含:MyCounter 1[InlineCell] *)

<小时>

这是为了您之前的定义"风格?如果是这样,那么为什么不将内联单元格作为从外部单元格继承其样式的普通(无样式)单元格.然后在定义"样式中,即在样式表中使用计数器增量?正如我上面所说,非内联单元格应该是样式化的单元格(如定义"、章节"、节"等),因为它决定了文档结构.<小时>

根据评论进行

这是一个将创建新章节单元格和新定义单元格的调色板.后者带有内置的、不可编辑的计数器.请注意,大部分样式应移至样式表中.

CreatePalette[With[{nb = InputNotebook[]}, {Button["New Chapter", SelectionMove[nb, After, Cell];NotebookWrite[nb, Cell["New Chapter", "Chapter" (* 样式在样式表中*)]]],按钮[新定义",SelectionMove[nb, After, Cell];NotebookWrite[nb, Cell[TextData[RowBox[{单元格[文本数据[StyleBox[#, FontWeight ->大胆"] &/@{"定义", CounterBox["Chapter"], ".", CounterBox["Definition"], ": "}],可编辑 ->错误,可选择 ->错误,可删除 ->错误的],"新定义"}]], "定义", CounterIncrements ->定义",CellFrame ->{{1, 1}, {0, 2}}, CellMargins ->{{30, 24}, {6, 6}},CellFrameColor ->RGBColor[0, 0, 1], 背景 ->RGBColor[0, 1, 1]]]]}],窗口标题 ->"文档书写调色板"];

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.

解决方案

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"];

这篇关于单个单元格的计数器增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆