# First ReplaceAll, then evaluate an expression

Here is a simple example using ReplaceAll:

rvec[[2]] /. rvec -> {x, y}


I think Mathematica first tries to evaluate rvec[[2]], realising this is not a list, and only afterwards uses ReplaceAll. The warning output confirms this.

If instead of rvec[[2]] we have some complex expression (e.g. inversion of a matrix), Mathematica will to do it analytically first before making the substitution. This is not what I intend, I want to delay the evaluation of rvec[[2]] (or other more complex things in its place) until the ReplaceAll substitution has been done.

How can I tell Mathematica to substitute first, then evaluate?

• With[{rvec = {x,y}}, rvec[[2]]]? – AccidentalFourierTransform May 27 '19 at 13:22
• Thanks, this works. However, is it possible to keep my form (with a subsitution list) more or less intact? My rvec is really many expressions, and {x,y} a long substitution list. – Alexander Erlich May 27 '19 at 14:08
• I think it's best if you include a MWE where my trick above is inconvenient, and where a substitution really is required. Otherwise I'm not sure why you would want to do things your way (see What is the XY problem?). – AccidentalFourierTransform May 27 '19 at 15:00

You might be able to use Unevaluated:
Unevaluated[rvec[[2]]] /. rvec->{x,y}

(rvec /. rvec -> {x, y})[[2]]