Wolfram mathematica 让CellDingbat记住Mathematica会话之间的状态
我已经修改了笔记本的样式表,以包含一个继承自Wolfram mathematica 让CellDingbat记住Mathematica会话之间的状态,wolfram-mathematica,stylesheet,Wolfram Mathematica,Stylesheet,我已经修改了笔记本的样式表,以包含一个继承自StyleData[“Todo”]的StyleData[“Item”]。它将单元格更改为复选框。在样式表编辑器中: Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]], CellDingbat->DynamicModuleBox[{$CellContext`color$$}, CheckboxBox[ Dynamic[$CellContext`c
StyleData[“Todo”]
的StyleData[“Item”]
。它将单元格更改为复选框。在样式表编辑器中:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat->DynamicModuleBox[{$CellContext`color$$},
CheckboxBox[
Dynamic[$CellContext`color$$], {RGBColor[1, 0.5, 0],RGBColor[0,Rational[2, 3], 0]},
Background -> Dynamic[$CellContext`color$$]],
DynamicModuleValues :> {}
],
]
问题是在笔记本中使用复选框时,复选框的状态在Mathematica会话之间不会保存。我认为DynamicModule[]
可以解决这个问题。如何让复选框记住其状态
编辑
Simon的解决方案确实保存了复选框的状态,但当用作CellDingbat(MacOS X)时,复选框会被剪裁。将Simon的代码放在CellFrameLabels选项中就可以做到这一点,并保留默认的“Item”CellDingbat。以下是我的观点:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellFrameLabels->{{
ButtonBox[
CheckboxBox[False], ButtonFunction :> (SelectionMove[
ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[
NotebookSelection[
ButtonNotebook[]], CellFrameLabels], CheckboxBox[
Pattern[$CellContext`x,
Alternatives[True, False]]] :> CheckboxBox[
Not[$CellContext`x]]]},
SetOptions[
NotebookSelection[
ButtonNotebook[]], $CellContext`new]]; SelectionMove[
ButtonNotebook[], After, CellContents]), Appearance -> None,
Method -> "Preemptive", Evaluator -> Automatic], None}, {
None, None}},
MenuSortingValue->1621]
您的代码(我认为)的问题在于,并非每次创建新的“ToDo”单元时都会创建一个新的
DynamicModule
。因此,没有任何地方可以保存每个复选框的状态
对于存储每个“ToDo”单元格的复选框的状态,我能想到的最简单的解决方案是在第一次激活复选框时覆盖单元格。
(我玩的其他选项是使用标记规则
,
在“待办事项”和“待办事项”样式之间切换等…)
但是,即使是CellDingbat
中的普通复选框
也不会存储其状态-请尝试运行以下命令,然后循环输出一个周期
为了解决这个问题,我使用了Checkbox
,并将确定参数True
或False
包装在一个按钮中,以更改状态。这是愚蠢和低效的,但它的工作
所以,我的细胞类型代码
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat -> ButtonBox[CheckboxBox[False],
ButtonFunction :> (SelectionMove[ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[NotebookSelection[ButtonNotebook[]], CellDingbat],
CheckboxBox[Pattern[$CellContext`x, Alternatives[True, False]]] :> CheckboxBox[Not[$CellContext`x]]]},
SetOptions[NotebookSelection[ButtonNotebook[]], $CellContext`new]];
SelectionMove[ButtonNotebook[], After, CellContents]),
Appearance -> None, Method -> "Preemptive", Evaluator -> Automatic]]
我对这个解决方案不满意,但这是我想到的最好的解决方案。一个改进是将按钮功能代码移出单元格,这样就不会对每个选中的ToDo单元格重复该代码。还可以使它在不使用ReplaceAll
的情况下运行,这样就不需要内核,只需使用前端即可运行函数。很难。顺便说一句,如果您设置了单元格
选项CellContext->Cell
,那么您可以将$CellContext`color$$$
替换为colors
。当我将代码粘贴到上面时,我收到了一条消息“遇到了一个带有头部符号的格式不正确的指令”。这是预期的吗?这已接近。复选框按钮夹在顶部,复选标记也夹在顶部。我对按钮、单元格和复选框尝试了大量的填充、边距和大小选项,但都没有效果。@JxB:对我来说很好-但不同的操作系统显示这些内容的方式不同。我已经在我的系统上发布了它的屏幕截图(Ubuntu 11.10,Mathematica 8.01)
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat -> ButtonBox[CheckboxBox[False],
ButtonFunction :> (SelectionMove[ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[NotebookSelection[ButtonNotebook[]], CellDingbat],
CheckboxBox[Pattern[$CellContext`x, Alternatives[True, False]]] :> CheckboxBox[Not[$CellContext`x]]]},
SetOptions[NotebookSelection[ButtonNotebook[]], $CellContext`new]];
SelectionMove[ButtonNotebook[], After, CellContents]),
Appearance -> None, Method -> "Preemptive", Evaluator -> Automatic]]