Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

enter image description here

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

 x = 2
 f1 = x^2 + 3

x^2 + 3 = 2^2 + 3 = 7
x + 5 = 2 + 5 = 7
share|improve this question
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. – bobthechemist Nov 27 '13 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? – Mr.Wizard Nov 28 '13 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:

share|improve this answer
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. – m_goldberg Nov 28 '13 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. – Mr.Wizard Nov 28 '13 at 2:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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