Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
    
@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
1  
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
1  
@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
show 10 more comments

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

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.

1 Answer

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.

share|improve this answer
add comment

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