Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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


and I select the expression:


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.

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

4 Answers 4

up vote 4 down vote accepted

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
share|improve this answer
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? –  Tyilo Jan 10 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? –  Tyilo Jan 10 at 13:37
a = b = c = 1; hold[(a + b)/c] –  Kuba Jan 10 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. –  István Zachar Jan 10 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)   *)
share|improve this answer
Just a note: Defer @@ Hold[(a + b)/c] ... etc. works as well. –  Yves Klett Jan 10 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? –  Tyilo Jan 10 at 13:24
Also, this doesn't work when the variables has already been defined. –  Tyilo Jan 10 at 13:28
Defer instead of Hold without replacement should work. It already holds –  Rojo Jan 10 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} –  Alexei Boulbitch Jan 13 at 8:25

Edit: answer rewrite

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.

You asked:

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 Replace 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]


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

fill[(a + b)/c]

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

share|improve this answer
I just realized that I read the comment I quoted completely backward. Time for a break I guess! I'll fix this later. –  Mr.Wizard Jan 10 at 15:52

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

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


Hold[(1 + 2)/3]


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

will give

share|improve this answer
This is almost exactly the same as Alexei's answer. I wouldn't consider it a different one. –  István Zachar Jan 10 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. –  Ymareth Jan 10 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. –  Alexei Boulbitch Jan 13 at 8:45
@AlexeiBoulbitch If you're referring to the downvote, that wasn't me. –  Tyilo Aug 24 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. –  Alexei Boulbitch Aug 24 at 19:28

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.