# Can evaluation show what was evaluated along with results? [duplicate]

After I write and evauate the following lines in Mathematica:

a = 1
b = 2
x = a + b


It will show only the result : 3. I want mathematica to show the following for my output:

x = 1 + 2 = 3

Is it capable of doing this?

There is no similarity between my post and the other. Can anyone prove that mathematica can do what I asked for?

• Do you want this to happen all the time or only when you ask for it? – Marius Ladegård Meyer Jun 12 '16 at 16:07
• All the time please. – Adi Jun 12 '16 at 16:11
• How about if you just type 13 + 14 without assigning 13 and 14 to variables? Still 13 + 14 = 27? – Marius Ladegård Meyer Jun 12 '16 at 16:24
• I need to use this for automation, I have a list of variables and then to copy them to word so I can present the results to my professor. – Adi Jun 12 '16 at 16:28
• So after a list of assigning variables and equations I need the program to calculate everything there is and to display them accordingly so I don't have to modify later in word. – Adi Jun 12 '16 at 16:32

You can use Trace with TraceDepth option set to 1 to get evaluation steps giving whole expression, and format result as you want it. Function performing this actions can be assigned to $Pre to be automatically used for all inputs. ClearAll[showSetSteps] SetAttributes[showSetSteps, HoldAllComplete] showSetSteps[Set[lhs_, rhs_]] := With[{trace = Replace[Trace[rhs, TraceDepth -> 1], {} -> {HoldForm@rhs}]}, lhs = trace[[-1, 1]]; Fold[HoldForm@Set[#2, #1] &, Append[Reverse@trace, HoldForm@lhs]] ] showSetSteps[expr_] := expr$Pre = showSetSteps;


Which will give:

a = 1
b = 2
x = a + b
(* a = 1 *)
(* b = 2 *)
(* x = 1 + 2 = 3 *)


Depending on what you actually expect to see in the result, you might want to use something else than Trace, for example something based on Inactivate/Activate.

ClearAll[inactivateActivate]
SetAttributes[inactivateActivate, HoldFirst]
inactivateActivate[expr_, patt_: _, opts : OptionsPattern[]] :=
Inactivate[expr, patt, opts] // Evaluate // HoldForm // Activate //
{#, # // ReleaseHold} &


Which gives:

a = 1; b = 2; c = 3; d = 4;
Trace[(a + b) c^d, TraceDepth -> 1]
inactivateActivate[(a + b) c^d]
(* {3*81, 243} *)
(* {(1 + 2) 3^4, 243} *)

f[x_] := x^2 + 10
Trace[(a + b) f[c] - c, TraceDepth -> 1]
inactivateActivate[(a + b) f[c] - c]
inactivateActivate[(a + b) f[c] - c, h_ /; Context[h] === "System"]
(* {57 - 3, 54} *)
(* {(1 + 2) f[3] - 3, 54} *)
(* {(1 + 2) 19 - 3, 54} *)
`