# Using With in combination with Graphics [duplicate]

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?

-

## marked as duplicate by Dror, Sjoerd C. de Vries, acl, Michael E2, Mr.Wizard♦Mar 15 '13 at 12:50

@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. –  Dror Mar 15 '13 at 10:05
@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? –  Dror Mar 15 '13 at 10:20
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. –  Rolf Mertig Mar 15 '13 at 10:20
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 –  gpap Mar 15 '13 at 10:38
@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. –  Michael E2 Mar 15 '13 at 12:30

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.

-