views:

90

answers:

1

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 Notebook Options > Evaluation Options > EvaluationCompletionAction can modify the CellLabels.

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

So, does anyone know where these CellLabels are generated?

+2  A: 

OK, the discussion on Physics Forums has lead to this quite hackish solution:

Unprotect[Out];
SetAttributes[Timeit, HoldAll]
Timeit[x_] := With[{t = Timing[x]}, If[t[[2]] === Null, Null,
   CellPrint[
    ExpressionCell[t[[2]], "Output", 
     CellLabel -> 
      StringJoin["(", ToString[t[[1]]], ")", "Out[", ToString[$Line], 
       "]:="]]];
   Out[$Line] = t[[2]];]]
$Pre = Timeit;

It doesn't correctly handle the CellLabel or storing of Out[] of expressions wrapped with one of the $OutputForms. But it's not too bad.

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.

Any better solutions are more than welcome.

Simon
+1 for not protecting "Out" at the end. No, seriously... do you believe this will survive a few releases? It's tempting ...
belisarius
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
@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.
belisarius
Simon