What generates the In/Out CellLabels in Mathematica and how can I add automatic Timing to them?

筅森魡賤 提交于 2019-12-05 23:29:30

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3938827/what-generates-the-in-out-celllabels-in-mathematica-and-how-can-i-add-automatic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!