7

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.

Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
Simon
  • 14,631
  • 4
  • 41
  • 101
  • This question follows on from my attempt to answer a question at http://www.physicsforums.com/showthread.php?t=438079 – Simon Oct 15 '10 at 01:28
  • Do you want to customize the style or content of the labels? – Michael Pilat Oct 15 '10 at 04:17
  • I guess content. The original idea was to put the Timing into the label for an Output cell. – Simon Oct 15 '10 at 04:26
  • Apparently the default CellLabel is generated by the kernel, so it's not easy to change the default behaviour (without using hacks like the below). – Simon Nov 11 '10 at 21:54
  • There's an option to show timing in status bar -- howto/DisplayTheTimingOfAnEvaluationInANotebookWindow – Yaroslav Bulatov Dec 02 '10 at 11:18
  • @Yaroslav - Thanks. The problem with this is that it only stays there until something else prints to the status bar. (It is also mentioned in the [PF](http://www.physicsforums.com/showthread.php?t=438079) post and an answer below...) – Simon Dec 02 '10 at 15:34
  • `notebook` tag removed as part of the [2012 cleanup](http://meta.stackexchange.com/questions/128315/the-great-stack-overflow-tag-question-cleanup-of-2012). – Abhranil Das Apr 29 '12 at 19:45

3 Answers3

6

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.

Simon
  • 14,631
  • 4
  • 41
  • 101
  • +1 for not protecting "Out" at the end. No, seriously... do you believe this will survive a few releases? It's tempting ... – Dr. belisarius Oct 16 '10 at 04:57
  • I guess I was just trying to get it working - wasn't really thinking about robustness. I see no reason that this wouldn't work in future releases. The CellLabel mechanism etc has been there since V3. Still, there must be a nicer way. – Simon Oct 16 '10 at 05:58
  • @Simon I think I'll give it a try ... timing all seems a nice idea! other things like automatic //MatrixForm for plain lists could be implemented with care too. – Dr. belisarius Oct 17 '10 at 04:34
  • The MatrixForm one can be adapted from the example in the $PrePrint documentation. Try `$PrePrint = If[MatrixQ[#], Interpretation[MatrixForm[#], #], #] &` – Simon Oct 17 '10 at 06:21
  • @Simon Some expressions behave differently when using your code: try to evaluate `Unevaluated[2 + 2]` for example. – Alexey Popkov Feb 10 '11 at 03:30
  • @Alexey: My `Timeit[]` has exactly the same behaviour as `Timing[]` - and all I'm doing is a special type of `Timing[]`. If it's a problem for what you're doing, don't run it automatically with `$Pre`. It's said that `Unevaluated[]` is a [magic symbol](http://stackoverflow.com/questions/4856177/preventing-evaluation-of-mathematica-expressions/4856721#4856721) -- magic is fragile. – Simon Feb 10 '11 at 06:40
6

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

ragfield
  • 2,486
  • 13
  • 15
  • Thanks ragfield. Although I did mention that in the Physics Forums thread linked to above. The main problem with this solution is the Timing only stays there until something else prints to the status bar. – Simon Nov 12 '10 at 21:39
  • Cool trick! Surprised I've never encountered this option before. Makes me wonder what else EvaluationCompletionAction could be used for... – telefunkenvf14 Jan 13 '12 at 12:01
2

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.

nttrbttr
  • 21
  • 1