1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ You'll probably need to redefine Set[] .Not a wise thing to do. $\endgroup$ Commented Oct 13, 2014 at 12:51
  • 2
    $\begingroup$ I'll never understand the need for this. Just look at your example. Looks pretty redundant to me. Anyway, $Post may be useful here. $\endgroup$ Commented Oct 13, 2014 at 20:56
  • $\begingroup$ 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.) $\endgroup$
    – Mr.Wizard
    Commented Oct 13, 2014 at 23:36
  • $\begingroup$ Related: (1047), (11961), (17121) $\endgroup$
    – Mr.Wizard
    Commented Oct 13, 2014 at 23:36
  • $\begingroup$ @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? $\endgroup$
    – TSGM
    Commented Oct 14, 2014 at 0:17

1 Answer 1

3
$\begingroup$

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!

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

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