This question already has an answer here:

Is there a way to have Mathematica automatically print the name of the variable during an evaluation when the semi-colon is suppressed. So for instance, Matlab would do this:

>> a = 1

a =

     1

>> 1 + 1

ans =

     2

>> 

which makes it very easy to follow the computations. In many cases, we may have a list of Mathematica evaluations, but the output is only organized sequentially, which makes reading it difficult.

They do list Out[number] on the left of the Notebook, but this is entirely unhelpful. I've seen questions about a function that can print out the value of a variable and its name, but this is not quite the same question.

marked as duplicate by Mr.Wizard Oct 13 '14 at 23:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You'll probably need to redefine Set[] .Not a wise thing to do. – Dr. belisarius Oct 13 '14 at 12:51
  • 2
    I'll never understand the need for this. Just look at your example. Looks pretty redundant to me. Anyway, $Post may be useful here. – Sjoerd C. de Vries Oct 13 '14 at 20:56
  • I have marked this question as "already answered" -- please review the original and tell me if you feel your question is not answered satisfactorily there. (I will reopen this one if that is the case.) – Mr.Wizard Oct 13 '14 at 23:36
  • Related: (1047), (11961), (17121) – Mr.Wizard Oct 13 '14 at 23:36
  • @SjoerdC.deVries: the example was artificially simplified; as people have pointed out, I often have extended Mathematica sheets with many calculations in a single cell. It becomes difficult to differentiate the output without resorting to counting the number of lines manually. In order to make the output clear, you would either resort to fashioning a Print command or creating new cells, neither of which are always good solutions. I want to be able to see the output clearly. Why would that not be useful? – TSGM Oct 14 '14 at 0:17
up vote 3 down vote accepted

One can use $Pre and CellTags

SetAttributes[verbose, HoldAll];
verbose@expr_ := Module[{res = expr}, If[res =!= Null, 
    CellPrint@Cell[BoxData@ToBoxes@res, "Output", CellTags -> "ans =", 
    ShowCellTags -> True]]];
HoldPattern@verbose@Set[lhs_, rhs_] := CellPrint@Cell[BoxData@ToBoxes[lhs = rhs], "Output", 
    CellTags -> ToString@Unevaluated@lhs <> " =", ShowCellTags -> True];
$Pre = verbose;

enter image description here

P.S. I believe that this output formatting actually isn't necessary, but this example shows that Mathematica is very flexible!

  • +1, I suspect your In[184] is really the example that the OP is really looking for; namely, multiple assignments in a single cell. – bobthechemist Oct 13 '14 at 23:09
  • @bobthechemist: yes, exactly right! Multiple assignments in a single cell makes spotting the output difficult. – TSGM Oct 14 '14 at 0:13
  • @TSGM I suspected as much. I addressed that specific need here and gave several flavors of implementation. – Mr.Wizard Oct 14 '14 at 0:17
  • @Mr.Wizard: Yes, all very helpful. Thank you. – TSGM Oct 14 '14 at 0:25
  • Amazing work there. Much more helpful than conventional output, IMHO. – David Addison Aug 30 at 3:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.