Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 671
Simon Avatar asked Oct 15 '10 01:10

Simon


2 Answers

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

like image 196
ragfield Avatar answered Oct 03 '22 10:10

ragfield


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.

like image 28
Simon Avatar answered Oct 03 '22 12:10

Simon