将单元格定义扩展到 CellFrameLabels 定义 [英] Extending cell definition to CellFrameLabels definition

查看:22
本文介绍了将单元格定义扩展到 CellFrameLabels 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含用于编写文档的样式的笔记本.我希望 Mathematica 的行为类似于 LaTeX,因为当我编写 "Definition" 单元格时,它将编写 "Definition [Chapter#].[Definition#]".

要了解我的意思,请执行以下操作.在一个空的 notebook 中创建一个单元格并将样式修改为 "Chapter".您可以通过选择单元格并转到 Format->Style->Other,输入 "Chapter" 来完成此操作.

现在转到 Format->Edit StyleSheet....在输入框中输入Chapter.这将生成一个标记为章节的单元格.选择那个单元格,然后点击Cell->Show Expression.此时选择您在那里看到的所有文本并将其替换为以下内容:

Cell[StyleData["Chapter"],CellFrame->{{0, 0}, {0, 0}},ShowCellBracket->自动,CellMargins->{{42, 27}, {10, 30}},CounterIncrements->"章节",CounterAssignments->{{"Section", 0}, {"Definition", 0}},FontFamily->"Verdana",字体大小-> 24,FontWeight->"粗体",CellFrameLabels->{{细胞[文本数据[{章节 ",CounterBox["章节"]}], "ChapterLabel", CellBaseline ->基线],继承},{继承,继承}},FontColor->RGBColor[0.641154, 0.223011, 0.0623026]]

这将改变章节单元格的显示方式.我改变了颜色和字体.对我来说最重要的是 CellFrameLabels.请注意,我已将其设置为每次创建章节单元格时都会显示:Chapter [Chapter Number].

在上图中,我创建了几个章节单元格并添加了文本:": Title of Chapter #".

这很简单,我们可以创建任何单元格,应用定义并利用计数器来标记单元格.

我注意到有些书的盒子里有定义.所以在这种情况下,我想创建一个包含 Definition 的框.这是我对单元格 "Definition" 定义的蹩脚尝试.

Cell[StyleData["Definition"],CellFrame->{{0, 0}, {0, 2}},ShowCellBracket->自动,CellMargins->{{27, 27}, {0, 8}},PageBreakWithin->False,CellFrameMargins->16,CellFrameColor->RGBColor[0.641154, 0.223011, 0.0623026],背景-> RGBColor[0.963821, 0.927581, 0.844465],FontFamily->"Verdana",CounterIncrements->"定义",字体大小->12,CellFrameLabels->{{细胞[文本数据[{定义 ",CounterBox["Chapter"], ".",CounterBox["定义"]}], "DefinitionLabel", CellBaseline ->基线],继承},{继承,继承}},]

这是它在笔记本中的样子:

问题是:有没有办法让 CellFrameLabels 成为单元格的一部分?我希望标签具有相同的背景并与其他文本内联.这是我希望它看起来如何的屏幕截图:

我将标签"设为粗体和蓝色.这是用户不应该能够修改的东西.

解决方案

我认为不可能按照您想要的方式进行.CellLabels 只能是文本,而 CellDingbatCellFrameLabels 都可以是任意的单元格表达式.

两个 CellDingbat ->...CellFrameLabels ->{{...,None},{None,None}} 如果单元格只有一行长,则有效.但不要自动调整多行单元格的大小(至少据我所知).例如:

Cell["Abcdefg", "Text",CellFrame->{{0, 1}, {0, 2}},CellMargins->{{30, 24}, {6, 6}},CellFrameMargins->0,CellFrameColor->RGBColor[0, 0, 1],CellFrameLabels->{{Cell["Definition 1.1", "Text",CellFrame ->{{2, 0}, {0, 2}}, CellFrameMargins ->0],无},{无,无}},CellFrameLabelMargins->0,背景-> RGBColor[0, 1, 1]]

在上面放一个CellFrameLabel就没有这个问题,但是不知道怎么左对齐...

Cell["Abcde", "Text",CellFrame->{{1, 1}, {0, 2}},CellMargins->{{30, 24}, {6, 6}},CellFrameMargins->0,CellFrameColor->RGBColor[0, 0, 1],CellFrameLabels->{{None, None}, {None,单元格[定义 1.1",文本",CellFrame ->{{2, 2}, {0, 2}}, CellFrameMargins ->0]}},CellFrameLabelMargins->0,背景-> RGBColor[0, 1, 1]]

我认为最好的解决方案是在单元格内容中包含定义 ch.def:".

Cell[TextData[{单元格[定义1.1:",可编辑->假​​,可选择->假,可删除->假],"Abcdefg"}], "文本",CellFrame->{{1, 1}, {0, 2}},CellMargins->{{30, 24}, {6, 6}},CellFrameColor->RGBColor[0, 0, 1],背景-> RGBColor[0, 1, 1]]

让它不会被普通用户删除,它可能几乎和单元格(框架)标签一样好.它可以包含计数器,以便自动显示正确的编号.唯一的问题是它不会自动出现,但如果你只是复制一个预先存在的单元格,那么问题不大.

<小时>

添加一个 来搭配您的样式表.这会很有用,因为只有 数量有限MenuCommandKey 可用于新样式的值(nb 覆盖默认值只会使人们感到困惑).有关此类调色板的示例,请参阅此答案.

I'm in the process of creating a notebook that contains a style to write documents. I would like Mathematica to behave similar to LaTeX in the sense that when I write a "Definition" cell then it will write "Definition [Chapter#].[Definition#]".

To see what I mean do the following. In an empty notebook create a cell and modify the style to "Chapter". You can do this by selecting the cell and the going to Format->Style->Other, enter "Chapter".

Now go to Format->Edit StyleSheet.... Enter Chapter in the input box. This will generate a cell labeled Chapter. Select that cell, and click on Cell->Show Expression. At this point select all that text that you see there and replace it with the following:

Cell[StyleData["Chapter"],
 CellFrame->{{0, 0}, {0, 0}},
 ShowCellBracket->Automatic,
 CellMargins->{{42, 27}, {10, 30}},
 CounterIncrements->"Chapter",
 CounterAssignments->{{"Section", 0}, {"Definition", 0}},
 FontFamily->"Verdana",
 FontSize->24,
 FontWeight->"Bold",
 CellFrameLabels->{{
    Cell[
     TextData[{
       "Chapter ",
       CounterBox["Chapter"]
       }], "ChapterLabel", CellBaseline -> Baseline], Inherited}, {
   Inherited, Inherited}},
 FontColor->RGBColor[0.641154, 0.223011, 0.0623026]]

This will change the style of how a chapter cell is displayed. I changed the color and font. The most important thing to me is the CellFrameLabels. Noticed that I have made it so that every time you create a chapter cell it will display: Chapter [Chapter Number].

In the picture above I have created several chapter cells and I have added the text: ": Title of Chapter #".

This is simple enough, we can create any cell, apply a definition and take advantange of counters to label the cells.

I have noticed how some books have definitions enclosed in box. So in this case I would like to create a box that contains Definition. Here is my lame attempt with the definition of the cell "Definition".

Cell[StyleData["Definition"],
 CellFrame->{{0, 0}, {0, 2}},
 ShowCellBracket->Automatic,
 CellMargins->{{27, 27}, {0, 8}},
 PageBreakWithin->False,
 CellFrameMargins->16,
 CellFrameColor->RGBColor[0.641154, 0.223011, 0.0623026],
 Background->RGBColor[0.963821, 0.927581, 0.844465],
 FontFamily->"Verdana",
 CounterIncrements->"Definition",
 FontSize->12,
 CellFrameLabels->{{
    Cell[
     TextData[{
       "Definition ",
       CounterBox["Chapter"], ".", 
       CounterBox["Definition"]
       }], "DefinitionLabel", CellBaseline -> Baseline], Inherited}, {
   Inherited, Inherited}},
 ]

Here is how it looks in the notebook:

Here is the question: Is there a way to make the CellFrameLabels part of the cell? I want the label to have the same background and to be inline with the other text. Here is a screen shot of how I want it to look:

I have made the "label" bold font and blue. This is something that the user should not be able to modify.

解决方案

I don't think that it's possible to do in the way you want. CellLabels can only be text, while both CellDingbat and CellFrameLabels can be arbitrary cell expressions.

Both CellDingbat -> ... and CellFrameLabels -> {{...,None},{None,None}} work if the cell is only a single line long. But do not automatically resize for multiple line cells (at least as far as I could tell). For example:

Cell["Abcdefg", "Text",
 CellFrame->{{0, 1}, {0, 2}},
 CellMargins->{{30, 24}, {6, 6}},
 CellFrameMargins->0,
 CellFrameColor->RGBColor[0, 0, 1],
 CellFrameLabels->{{Cell[" Definition 1.1  ", "Text", 
   CellFrame -> {{2, 0}, {0, 2}}, CellFrameMargins -> 0], None}, {None, None}},
 CellFrameLabelMargins->0,
 Background->RGBColor[0, 1, 1]]

Putting a CellFrameLabel on the top does not have this problem, but I don't know how to align it to the left...

Cell["Abcde", "Text",
 CellFrame->{{1, 1}, {0, 2}},
 CellMargins->{{30, 24}, {6, 6}},
 CellFrameMargins->0,
 CellFrameColor->RGBColor[0, 0, 1],
 CellFrameLabels->{{None, None}, {None, 
    Cell[" Definition 1.1 ", "Text", 
     CellFrame -> {{2, 2}, {0, 2}}, CellFrameMargins -> 0]}},
 CellFrameLabelMargins->0,
 Background->RGBColor[0, 1, 1]]

I think that maybe the best looking solution would be to include the "Definition ch.def:" in the cell contents.

Cell[TextData[{
 Cell["Definition 1.1:   ", Editable->False, Selectable->False, Deletable->False],
 "Abcdefg"}], "Text",
 CellFrame->{{1, 1}, {0, 2}},
 CellMargins->{{30, 24}, {6, 6}},
 CellFrameColor->RGBColor[0, 0, 1],
 Background->RGBColor[0, 1, 1]]

Make it so that it's not deletable by the average user and it is probably almost as good as a cell(frame)label. It can include counters so that it automatically shows the correct numbering. The only problem is that it does not appear automatically, but if you just copy a pre-existing cell, then that's not too much of a problem.


Edit: Adding an input alias that creates the non-deletable counter

First we get the current input aliases,

oldAliases = InputAliases /. Options[EvaluationNotebook[], InputAliases];

then replace any existing alias EscdefEsc with our new one:

newAliases = 
  Append[DeleteCases[oldAliases, "def" -> _], 
   "def" -> Cell[TextData[
     RowBox[StyleBox[#, FontWeight->"Bold", FontColor->Blue]&/@{"Definition ", 
      CounterBox["Chapter"], ".", CounterBox["Definition"], ":   "}]],(*"Text",*)
     Editable -> False, Selectable -> False, Deletable -> False]];  
SetOptions[EvaluationNotebook[], InputAliases -> newAliases]

Since I don't have your style sheet, I need to set a couple of counters:

CellPrint[Cell["Setting the counters", "Text", 
  CounterAssignments -> {{"Chapter", 2}, {"Definition", 3}}]]

Now I can use the alias in an existing cell - it inherits the styling of the parent cell (unless otherwise specified):


Another option is to make a palette to go with your stylesheet. This would be useful since there's only a limited number of MenuCommandKey values that you can use for your new styles (n.b. overwriting the default ones will just confuse people). See this answer for an example of such a palette.

这篇关于将单元格定义扩展到 CellFrameLabels 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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