# Adding a label to an expression result

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= myVariable = 1 + 1

Out= 2


But I'd like to get an output that looks more like:

Out= 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?

• I think you should look up $Post and $Pre in the Mathematica documentation. – m_goldberg Dec 31 '12 at 2:13
• I couldn't get any joy out of $Post and $PrePrint (nuby here!). – CrustyNoodle Dec 31 '12 at 3:08
• Related: (1047), (11961) – Mr.Wizard Feb 22 '13 at 23:31

## 3 Answers

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

• +1 To get around the problems with % not working, you could do something like the CellPrint code in stackoverflow.com/a/3947046/421225 – Simon Dec 31 '12 at 23:11

I'll assume that you want to echo any literal Set operation that occurs in input, even if it is not on a line by itself.

## $Pre This may work for you: $Pre =
Function[
main,
Unevaluated[main] /. Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst],
HoldAll
];


Now:

{a = 2 + 2, b = 10/2, c = Sqrt};


a = 4

b = 5

c = 3

{a, b, c}

{4, 5, 3}


## echo function

Alternatively, since I cannot imagine the echo being practical for every Set you might do it like this:

$Pre =. (* clear the prior definition for$Pre *)

echo =
Function[
main,
Unevaluated[main] /. Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst],
HoldAll
];


Then:

{a = 2 + 2, b = 10/2, c = Sqrt} // echo


## CellEvaluationFunction

If you want to echo some but not all Sets, and you do not want to have // echo appear, you could perhaps use CellEvaluationFunction for individual Cells.

As an example, this code generates a new input Cell into which any Set that is typed will be echoed:

CellPrint[ExpressionCell[Placeholder[], "Input", CellEvaluationFunction ->
(ReleaseHold[
MakeExpression@# /.
Set -> Function[, Print@HoldForm[# = #2]; # = #2, HoldFirst]
] &)
]]


The cell that is created can be copied and pasted to make more, or you can use the Option Inspector to set the value of CellEvaluationFunction for existing cells.

• This works very well for my application - thankyou. – CrustyNoodle Jan 3 '13 at 22:28
• @Crusty I'm curious, if this works very well for your application why did you Accept another answer? It is entirely your prerogative to do so, but I'm wondering in what way you found that method superior. – Mr.Wizard Jan 4 '13 at 3:14

I think the simplest and most flexible method is to just add print statements before each operation that you want to label:

Print["myVariable ="];
myVariable=1+1


myVariable =

2

Print["{M, r, T} ="];
{m=10,r=6/2,t=1/2}


{M, r, T} =

{10,3,1/2}

This method allows you to label expressions without the limitations of variable naming in Mathematica. For example, the following uses an ASCII variable name for a stress tensor component but labels it with the Greek characters and subscripts:

Print["Subscript[\[Tau], \[Phi],\[Theta]] ="];
shearPhiTheta=m/(2 \[Pi] *r^2*t)


$\tau _{\phi ,\theta }\text{ =}$

$\frac{10}{9 \pi }$