gpt4 book ai didi

wolfram-mathematica - 让 CellDingbat 在 Mathematica session 之间记住它的状态

转载 作者:行者123 更新时间:2023-12-04 08:45:45 26 4
gpt4 key购买 nike

我修改了我笔记本的样式表以包含 StyleData["Todo"]继承自 StyleData["Item"] .它将单元格 dingbat 更改为复选框。在样式表编辑器中:

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 session 之间保存。我以为 DynamicModule[]会做的伎俩。如何让复选框记住其状态?

编辑

Simon 的解决方案确实保存了复选框的状态,但是当用作 CellDingbat (MacOS X) 时,复选框会被裁剪。将 Simon 的代码放在 CellFrameLabels 选项中可以解决问题,并且还保留默认的“项目”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]

最佳答案

你的代码的问题(我认为)是一个新的 DynamicModule每次创建新的“待办事项”单元格时都不会创建。所以无处是每个 Checkbox 的状态可以得救。

我能想到的用于存储 Checkbox 状态的最简单的解决方案对于每个“待办事项”单元格是覆盖 CellDingbat第一次说Checkbox被激活。
(我玩过的其他选项是使用 TaggingRules
在“ToDo”和“ToDone”样式之间切换,等等...)

然而,即使是普通的CheckboxCellDingbat不存储其状态 - 尝试运行以下命令,然后通过 Show Expression 循环输出循环。

CellPrint[Cell["test", "Text", CellDingbat -> ToBoxes[Checkbox[]]]]

为了解决这个问题,我使用了 Checkbox有明确的论据 TrueFalse包裹在一个改变状态的按钮中。这是愚蠢且低效的,但它有效!

所以,我的单元格样式代码
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]]

Screenshota

我对这个解决方案不满意,但这是我想出的最好的解决方案。一个改进是将按钮功能代码移出单元格,这样就不会为每个选中的 ToDo 单元格重复它。还要让它在没有 ReplaceAll 的情况下运行这样就不需要内核,并且可以仅使用前端来运行该功能。

关于wolfram-mathematica - 让 CellDingbat 在 Mathematica session 之间记住它的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7932172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com