1
$\begingroup$

Assume that we are given a symbol p={x,y} which should represent a 2D point. If x and y have numerical values then we can plot the point uisng

Graphics[Point[p]]

Next, I wanted to plot some setting for various x and y, so I thought I could use the With command and have something like:

With[{x = 1, y = 1},
 Graphics[Point[p]]
 ]

This doesn't work. I guess it has something to do with the order of evaluations, holding stuff etc. but I cannot figure out how to solve this.

So the question is how to use With together with Graphics?

$\endgroup$
15
  • $\begingroup$ @gpap: This is clear, but I want to use p as in my real example the definition of p depends on x and y in a somewhat more complicated manner. $\endgroup$
    – Dror
    Mar 15, 2013 at 10:05
  • $\begingroup$ @gpap: I have a point which depends on several variables. So you say that the only way is to define p as a function of the various symbols that define it? $\endgroup$
    – Dror
    Mar 15, 2013 at 10:20
  • $\begingroup$ Maybe the problem is the (understanding or wording) of the documentation of With ? So what is not clear about With? @gpap: just post your comment as an answer. $\endgroup$ Mar 15, 2013 at 10:20
  • 1
    $\begingroup$ No, that's not right - no recursion is taking place. I had a look around and your question can be answered by the answer I linked to and this $\endgroup$
    – gpap
    Mar 15, 2013 at 10:38
  • 1
    $\begingroup$ @Mr.Wizard Is the duplicate on SO a sufficient reason for closing on SE? I don't know how integrated or independent the sites are meant to be. To my mind, the answer on SE linked by gpap explains how to use scoping constructs but does not warn how not to use them for this case. I think the distinction is important for someone still getting familiar with Mathematica's order of evaluation and localization. $\endgroup$
    – Michael E2
    Mar 15, 2013 at 12:30

1 Answer 1

3
$\begingroup$

The trivial answer is: don't use With, use Block:

p := {x, y}

Block[{x = 1, y = 1},
 Graphics[Point[p]]
]

The question remains how one can use With. It is possible with Evaluate:

With[{x = 1, y = 1},
 Graphics[Point[p]] // Evaluate
]

This contravenes the expected behavior of With which normally replaces symbols before evaluation. It works in this case because nothing (bad) happens when you evaluate the incomplete Graphics expression; errors only appear on display. In other cases you may need to prevent evaluation of the full expression, yet you wish to make a substitution. This amounts to in-place evaluation, so you will need something like the Trott-Strzebonski method for In-Place Evaluation, or this method that WReach introduced me to:

Unevaluated[Print[Hold[p]]] /. HoldPattern[p] :> RuleCondition @ Block[{x = 1, y = 1}, p]

Hold[{1,1}]

Or, since p is a Symbol and not an arbitrary pattern we may use the "injector pattern":

Block[{x = 1, y = 1}, p] /. p_ :> Print[Hold[p]]

Hold[{1,1}]

I replaced Graphics with Print, and Point with Hold so that the holding and evaluation behavior would be apparent.

$\endgroup$

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