This question already has an answer here:

The command With[{x = 1}, x] outputs 1.

The command t = x; With[{x = 1}, t] outputs x.

I expected this second input to give the same answer. How can I modify my code so it behaves in this way?


marked as duplicate by MarcoB, ybeltukov, Oleksandr R., user9660, Edmund Nov 11 '15 at 15:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ To give you an idea: try t=x; With[{x=1}, {x, t}]. $\endgroup$ – J. M. will be back soon Nov 11 '15 at 14:05

I think you are confusing the behaviour of With and Block

t = x; Block[{x = 1}, t]
(* 1 *)
  • $\begingroup$ Yes, this works better for my more complicated actual situation. Thanks :) $\endgroup$ – Tom Nov 11 '15 at 14:42
t = x; With[{x = 1}, Evaluate[t]]
  • $\begingroup$ Nice. Thank you $\endgroup$ – Tom Nov 11 '15 at 14:07

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