3
$\begingroup$

Is there a way to Defer only parts of the expression? For example, I have

a=1;

and then I want the expression to be shown asa = 1;by usingDefer[a = Evaluate[a]],But the actual output is just

a = Evaluate[a]]

Is there away to only Defer part of an expression?

$\endgroup$
3
  • 1
    $\begingroup$ Maybe Defer[a = x] /. x -> a or Defer[a = #] &[a]? $\endgroup$
    – kglr
    Commented May 15, 2014 at 19:15
  • 1
    $\begingroup$ or With[{x = a}, Defer[a = x]]? $\endgroup$
    – kglr
    Commented May 15, 2014 at 19:20
  • $\begingroup$ Both solution works, thank you! $\endgroup$
    – Yituo
    Commented May 15, 2014 at 19:24

1 Answer 1

5
$\begingroup$

You can insert into held expressions in a number of ways:

Using ReplaceAll (/.): Defer[a=x]/.x->a

Using function evaluation: Defer[a=#]&[a]

Using With : With[{x=a}, Defer[a=x]

Example:

a = 2; z = 4; w = 5;
{Defer[a + w = x + z] /. x->a, Defer[a + w = # + z]&[a], With[{x = a},Defer[a + w = x + z]]}
(* {a + w = 2 + z, a + w = 2 + z, a + w = 2 + z} *)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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