# What's wrong with naming a rule?

Please have a look at the code below. The second Manipulate uses the name rl of the replacement rule given in the first line, but unlike the first Manipulte, it does not plot anything, at least not on my computer. What goes wrong and how should I correct it? And why is % immune to the problem?

rl = {ρ -> 1 / 5 Sqrt[25 - 25 z^2 + 10 Sin[5 ϕ] + Sin[5 ϕ]^2]};
Manipulate[PolarPlot[ρ /. % // Evaluate, {ϕ, 0, 2 Pi}], {z, -1, 1}]
Manipulate[PolarPlot[ρ /. rl // Evaluate, {ϕ, 0, 2 Pi}], {z, -1, 1}]

• With[{rl = rl // Evaluate}, Manipulate[PolarPlot[\[Rho] /. rl, {\[Phi], 0, 2 Pi}], {z, -1, 1}]] - read the documentation, understand the scoping behavior of Manipulate...
– ciao
Apr 30, 2016 at 9:43
• @ciao that doesn't explain why % works. Apr 30, 2016 at 9:47
• Manipulate is a scoping construct so the second example which is not working is behaving correctly :-) z defined in rl is different than z from Manipulate's spec part, or will be as soon as Manipulate is evaluated. So at the end you should use With as ciao showed or define a function and pass z and ϕ. This of course doesn't explain why % works. Maybe there are some special rules about how FrontEnd reads % and Out[_], would be good to know.
– Kuba
Apr 30, 2016 at 10:06
• @QuantumDot, huh? Of course it does. That causes the result of the prior line to replace the % before anything else, exactly as if it had been there directly.
– ciao
Apr 30, 2016 at 10:08
• @ciao but why % is expected to be replaced asap?
– Kuba
Apr 30, 2016 at 10:11

This version works:

rl = {ρ[z_, ϕ_] :> 1/5 Sqrt[25 - 25 z^2 + 10 Sin[5 ϕ] + Sin[5 ϕ]^2]};
Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], %]], {ϕ,0, 2 Pi}], {z, -1, 1}]
Manipulate[PolarPlot[Evaluate[ReplaceAll[ρ[z, ϕ], rl]], {ϕ,0, 2 Pi}], {z, -1, 1}]


The main change is in the way the rule is defined as a $RuleDelayed$ instead of $Rule$.

• Thank you! I did try RuleDelayed but my attempt was in vain. Your code tells me why my code didn't work: I did not write rho as a function. Apr 30, 2016 at 13:56
• It's not a function per se, but a pattern. A function is similar to this but applies the rule whenever the pattern is encoutered. Happy to help Apr 30, 2016 at 13:59
• oh, I see. Thanks! Apr 30, 2016 at 14:03

Thanks to ciao's and Kuba's explanation, I have thought of some little code to exemplify the scoping behaviour of Manipulate; I hope it will be helpful to people who are still not very familiar with the concept:

a),

Manipulate[Hold@x, {x,0,1}]


b),

p=x;
Manipulate[2 p-x,{x,0,1}]


c),

rpl=q->x;
Manipulate[(2 q/.rpl)-x,{x,0,1}]


Others have explained why the version with rl does not work. But this does not explain the very weird phenomenon that the version with % does work. Why is % (which is just a notation for Out) special?

It seems that Manipulate singles out Out (i.e. %) for special treatment. Observe:

In[1]:= x
Out[1]= x

In[2]:= Manipulate[Hold[%1], {x, 0, 1}]
Out[2]= Manipulate[Hold[x], {x, 0, 1}]


Manipulate has taken the Out[1] (same as %1), and evaluated it, even though it was inside of Hold. This is not normal evaluation. It is Manipulate looking specifically for % and replacing it with its value.

This is no doubt done to make Mathematica less confusing and more accessible to beginners. But personally I don't like this special behaviour at all. For people who already understand how Mathematica works this (undocumented!) behaviour is very surprising and completely unexpected.

• Yes I find this very confusing. I think I fall somewhere between "beginner" and "experienced", and I find weird nuances in Mathematica like these only serve to confuse people like me. I really wish they implemented a global switch that turned off all "noob" features to make Mathematica behave more regularly. May 1, 2016 at 10:04
• Also, I think that Wolfram Research ought to clearly document the evaluation sequences inside Manipulate, Animate and other functions that are as complex and as, so to speak, mysterious. May 2, 2016 at 5:52