# Replace symbols with values without evaluation

If I have assigned values to a, b and c:

a=7;
b=5;
c=6;


and I select the expression:

(a+b)/c


and I press cmd + enter in the Mathematica frontend, the expression will be replaced by 2.

I would like to do something similar which would fill in the values of the symbols without evaluating the result. So (a+b)/c would be replaced by (7+5)/6.

Is there a way of doing this? If so, can you also assign this to a keyboard shortcut that would replace the selected expression?

Note: Assume you don't know the values or the names of the variables. Inputting (a+b)/c to a function, should be able to produce something like (7+5)/6 without having to input a, b and c or 7, 5 and 6.

• What should happen when b = -5?
– Kuba
Jan 10, 2014 at 13:00
• @Kuba either (7-5)/6 or (7+(-5))/6. Jan 10, 2014 at 13:12
• I swear this question is a duplicate. People, please help me find it. Jan 10, 2014 at 13:39
• @Mr.Wizard I've only found this, only partially duplicate, so I took the liberty to answer. Jan 10, 2014 at 13:44
• @István That's not the one, but thank you for looking first. Jan 10, 2014 at 13:45

Using OwnValues and HoldForm:

{a = 7, b = 5, c = 6};

HoldForm[(a + b)/c] /. OwnValues@a /. OwnValues@b /. OwnValues@c

(7 + 5)/6

With[{a = a, b = b, c = c}, HoldForm[(a + b)/c]]

(7 + 5)/6


Assuming that variables are already defined and you don't want to bother with listing the symbols, and you want to have a function that does it in one go:

Attributes[hold] = {HoldAll};
hold[x_] := HoldForm@x /. Cases[Hold@x, s_Symbol :> (HoldPattern@s -> s), Infinity];

hold[(a + b)/c]

(7 + 5)/6

• Given that I don't want to type the names of the symbols again, is there a way of just inputting (a+b)/c and the function would figure out to do /. OwnValues@symbol for each symbol? Jan 10, 2014 at 13:26
• I know this a probably even harder, but would it be possible to output (7+5)/(-6) instead of -(1/6)*(7+5) when c=-6? Jan 10, 2014 at 13:37
• a = b = c = 1; hold[(a + b)/c]
– Kuba
Jan 10, 2014 at 13:38
• @Tyilo If you examine FullForm[(a + b)/c] (without values for a, b, c), you will realize that it is already represented as Times[Plus[a, b], Power[c, -1]]. So that is again an entirely different question. Jan 10, 2014 at 13:43
Hold[(a + b)/c] /. {a -> 1, b -> 3, c -> 4} /. Hold -> Defer

(*   (1 + 3)/4   *)

Hold[(a + b)/c] /. {a -> 1, b -> -3, c -> 4} /. Hold -> Defer

(*   (1 - 3)/4   *)

Hold[(a + b)/c] /. {a -> 1, b -> -3, c -> -4} /. Hold -> Defer

(*   -(1/4) (1 - 3)   *)

• Just a note: Defer @@ Hold[(a + b)/c] ... etc. works as well. Jan 10, 2014 at 13:22
• Given that I don't want to type the names of the symbols and the values again, is there a way of just inputting (a+b)/c and the function would figure out the values itself? Jan 10, 2014 at 13:24
• Also, this doesn't work when the variables has already been defined. Jan 10, 2014 at 13:28
• Defer instead of Hold without replacement should work. It already holds
– Rojo
Jan 10, 2014 at 18:28
• @Rojo That was my first idea also, but it did not work. This (a + b)/c // Defer /. {a -> 1, b -> 3, c -> 4} returns (a + b)/c; this (a + b)/c /. {a -> 1, b -> 3, c -> 4} // Defer and this Defer[(a + b)/c /. {a -> 1, b -> 3, c -> 4}] return (a + b)/c /. {a -> 1, b -> 3, c -> 4} Jan 13, 2014 at 8:25

This question in its base form is a duplicate, but since I cannot find the original, and since you extended the question to something more unique than what I recall, I shall provide a short answer.

I know this a probably even harder, but would it be possible to output (7+5)/(-6) instead of -(1/6)*(7+5) when c=-6?

For that kind of control see for example: Returning an unevaluated expression with values substituted in

Combining that with RuleCondition, described in Replacement inside held expression, we can use:

SetAttributes[{defer, fill}, HoldAll]

MakeBoxes[defer[args__], fmt_] := Block[{Times}, MakeBoxes[Defer[args], fmt]]

fill[expr_] := defer[expr] /. x_Symbol :> RuleCondition[x]


Now:

{a, b, c} = {7, 5, -6};

fill[(a + b)/c]

(7+5)*-(1/6)


Because this uses Defer you can use the output of fill as input and it will fully evaluate.

• I just realized that I read the comment I quoted completely backward. Time for a break I guess! I'll fix this later. Jan 10, 2014 at 15:52

In the same search that found your post, I also found: https://mathematica.stackexchange.com/a/100152/76328

I tailored Edmund's approach just slightly to fit my needs. I hadn't heard of Inactivate before, but it performs substitutions while not evaluating the main expression. It sounds like a simple solution to this post?

That said, I do like István Zachar's general-case approach as well.

Tailored version noted above:

InactiveAndActive = Function[expr, Column@Through@{Inactivate, Identity}@Unevaluated@expr, HoldAll];


EDIT: I just noticed that the above doesn't format things like 1/x in StandardForm. A better solution is:

HoldAndEvaluate = Function[expr, Column@Through@{Inactivate, Identity}@Unevaluated@expr /. Inactive[x_] -> HoldForm@*x, HoldAll];


Mathematica has the function Hold which prevents evaluation, until you Release that Hold.

Hold[(a+b)/c] /.{a->1,b->2,c->3}


gives

Hold[(1 + 2)/3]


Then

Release[Hold[(1 + 2)/3]]


will give

1

• This is almost exactly the same as Alexei's answer. I wouldn't consider it a different one. Jan 10, 2014 at 14:55
• I posted it on an un-refreshed page, didn't see Alexi's answer until afterwards. Gotta be quick round here clearly. Jan 10, 2014 at 18:23
• Not really. I tried to get rid of Hold in the output. The advantage of the present approach is the ease of its later evaluation it, if needed, as it is shown above. Its penalty is to have Hold visible, which (it seems) was not desired by Tyilo. The penalty of my approach is that the way to evaluate the result is less straightforward. It may be done however by /.Defer->Evaluate. Jan 13, 2014 at 8:45
• @AlexeiBoulbitch If you're referring to the downvote, that wasn't me. Aug 24, 2014 at 2:52
• @Tyilo No, the up- and downvotes are no matter for me. I just discuss the essence as I understand it. Have fun. Aug 24, 2014 at 19:28