Mathematica 中的输入/输出单元标签是由什么生成的以及如何向它们添加自动计时?

发布于 2024-09-27 16:54:06 字数 974 浏览 1 评论 0原文

当 Mathematica 计算单元格时,它会为输入单元格和输出单元格提供 CellLabel In[$Line]:=Out[$Line]= 其中 $Line 是一个计数器,在每个计算的输入上递增。

如果您输入类似 TraditionalForm[expr]TeXForm[expr] (或 $OutputForms 中的任何其他 *Form)的名称,则表单也会添加到输出单元格的标签中。例如Out[1]//TraditionalForm=

我找不到任何自定义这些标签的方法。

  • 可以在“首选项”对话框中禁用它们。

  • 他们似乎不在 输入和的样式表选项 输出单元格 - 尽管选项 与 CellLabel 行为相关 在那里。

  • 也不在笔记本选项中 - 尽管在选项检查器中:笔记本选项> 评估选项> valuationCompleteAction可以修改 通过添加时间戳来显示 CellLabels。 它还可以在 StatusArea 中显示 Timing, 一旦有其他东西打印在那里,它就会被删除。

  • 也不是任何 init.m 类型 配置文件

那么,有人知道这些 CellLabels 是在哪里生成的吗?


特别是,我有兴趣将 Timing 添加到输出单元格的 CellLabel 中。

When Mathematica evaluates a cell, it gives the Input cell and Output cell the CellLabels In[$Line]:= and Out[$Line]= where $Line is a counter that gets incremented on each evaluated input.

If you input something like TraditionalForm[expr] or TeXForm[expr] (or any other *Form from $OutputForms) then the name of the form also gets added to the Output cell's label. eg Out[1]//TraditionalForm=.

I can't find any way of customising these labels.

  • They can be disabled in the Preferences dialog.

  • They don't seem to be in the
    StyleSheet options for Input and
    Output cells - although the options
    pertaining to the CellLabel behaviour
    are there.

  • Nor in the Notebook options -
    although in the Option Inspector: Notebook Options >
    Evaluation Options >
    EvaluationCompletionAction can modify
    the CellLabels by adding a TimeStamp.
    It can also show the Timing in the StatusArea,
    bit it gets removed as soon as something else prints there.

  • Nor any of the init.m type
    configuration files.

So, does anyone know where these CellLabels are generated?


In particular, I am interested in adding the Timing to the CellLabel for Output cells.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-10-04 16:54:06

另一种方法是设置EvaluationCompletionAction -> “ShowTiming”,每次评估后都会在笔记本窗口的状态栏中显示计时信息。

替代文本

Another way of doing this would be to set EvaluationCompletionAction -> "ShowTiming" which will display timing information in the status bar of the notebook window after each evaluation.

alt text

古镇旧梦 2024-10-04 16:54:06

好的,物理论坛上的讨论导致了这个相当黑客的解决方案(现已清理)向上一点):

SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, Module[{out, form},
  If[TrueQ[MemberQ[$OutputForms, Head[t[[2]]]]],
    out = First[t[[2]]]; form = "//" <> ToString[Head[t[[2]]]], 
    out = t[[2]]; form = ""];
  If[out === Null, Null,
    CellPrint[ExpressionCell[t[[2]], "Output", CellLabelAutoDelete -> False,
      CellLabel -> StringJoin["(", ToString[t[[1]]], ")",
        "Out[", ToString[$Line], "]", form, "="]]];
  Unprotect[Out]; Out[$Line] = out; Protect[Out]; out;]];]
$Pre = Timeit;

要使 CellLabels 持久,以便在保存和加载笔记本时不会丢失时间,您可以修改样式表,以便输出单元格具有选项 CellLabelAutoDelete ->;确实如此。 (编辑:现已添加到 CellPrint 命令中。)

我们非常欢迎任何更好的解决方案。

OK, the discussion on Physics Forums has lead to this quite hackish solution (now cleaned up a little):

SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, Module[{out, form},
  If[TrueQ[MemberQ[$OutputForms, Head[t[[2]]]]],
    out = First[t[[2]]]; form = "//" <> ToString[Head[t[[2]]]], 
    out = t[[2]]; form = ""];
  If[out === Null, Null,
    CellPrint[ExpressionCell[t[[2]], "Output", CellLabelAutoDelete -> False,
      CellLabel -> StringJoin["(", ToString[t[[1]]], ")",
        "Out[", ToString[$Line], "]", form, "="]]];
  Unprotect[Out]; Out[$Line] = out; Protect[Out]; out;]];]
$Pre = Timeit;

To make the CellLabels persistent so that you don't lose the timing when you Save and Load the notebook, you can modify the stylesheet so that the Output cells have the option CellLabelAutoDelete -> True. (Edit: Now added to the CellPrint command.)

Any better solutions are more than welcome.

自此以后,行同陌路 2024-10-04 16:54:06

通过在评估完成操作的选项检查器设置中选择“AddTimeStamp”,可以将评估时间戳添加到 Mathematica 单元格标签中。此外,可以通过编辑条目来给出选项列表,因此使用 {"ShowTiming","AddTimeStamp"} 我可以在状态栏中获得评估持续时间,并在单元格标签中获得输入和输出时间戳。

It's possible to add evaluation timestamps to Mathematica cell labels by selecting "AddTimeStamp" in the option inspector settings for EvaluationCompletionAction. Moreover, a list of options can be given by editing the entry, so using {"ShowTiming","AddTimeStamp"} I get both an evaluation duration in the status bar and input and output timestamps in the cell labels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文