To make my notebook easier to read for non-Mathematica literate colleagues (myself included), I'd like to have the output from an assignment expression look somewhat more obvious. Normally an expression assignment would look something like this:
In[1]= myVariable = 1 + 1
Out[1]= 2
But I'd like to get an output that looks more like:
Out[1]= myVariable = 2
I'm sure there are very convoluted ways to get this as an output but is there some really simple thing I can do so that the input is also still fairly easily readable without a heap of other commands wrapped around it?
Answer
Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre
and $Post
:
SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)
$Pre = saveSet;
$Post = (If[ValueQ@lastSet, Row[{lastSet, " = ", #}], #]) &;
After these definitions are evaluated, computations using Set
(=) show up as:
y = 42^2 + 1
y = 1765
but other expression evaluations will printout normally:
x == y
False
The downside of this is that, when these definitions are in effect, %
becomes unusable after a Set
evaluation. Condsider
y = 42^2 + 1
y = 1765
% - 1
-1+y = 1765
Comments
Post a Comment