# How can I exhibit the evaluation of formulas in a Mathcad-like way?

How can I create a function F that works like this:

 x = 2
f1 = x^2 + 3
f2=x+5

F[f1]

x^2 + 3 = 2^2 + 3 = 7

 F[f2]

x + 5 = 2 + 5 = 7

• Unless @Mr.Wizard has answered your question adequately, can you clarify? I'm confused by the lack of a verb in the question title and the relationship between the green text and code. Commented Nov 27, 2013 at 16:50
• I don't think this question needs to be closed. As I see it the user wants to show the evaluation chain, including at least the step where symbol is "filled in" by a definition, such as x in x^2 + 3. User: is this correct? Commented Nov 28, 2013 at 2:29

First you will need to make your definitions with SetDelayed (:=) rather than Set (=):

x  := 2
f1 := x^2 + 3
f2 := x + 5


You can get a list of all evaluation steps using Trace or TracePrint. To get only the steps that transform the entire expression use the Option TraceDepth -> 1, and you can format with Row

Row[Trace[f1, TraceDepth -> 1], "="]

f1 = x^2+3 = 4+3 = 7


This is not exactly what you asked for but I hope it is close enough to help. The additional steps are accessible with Trace but I could not think of a simple and robust way to use them and I can't spend more time on this right now. For reference:

Trace[f1]

{f1,x^2+3,{{x,2},2^2,4},4+3,7}

• I think your answer might be closer to what the OP is asking for if you added a definition for his F along the lines of: SetAttributes[F, HoldFirst]; F[var_Symbol] := Row[Trace[var, TraceDepth -> 1], "="] -- providing, of course, that you think this would be robust enough to meet the OP needs. Commented Nov 28, 2013 at 2:15
• @m_goldberg Good point. user10833: that is indeed the way to package an expression such as this as a function. I did not do that as I did not feel this was complete, e.g. there is no 2^2+3 step shown. Commented Nov 28, 2013 at 2:24