Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm confused with Mathematica's way of parsing expressions. I've been struggling with this for a while and never found an exhaustive answer, sometimes things don't parse the way I think they would and I don't really understand why.

As an example, with Mathematica 8:

(* A works *)
  Plot[(y/a) /. {y -> (x - b)}, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* B doesn't work *)
  Plot[(x/a) /. {x -> (x - b)}, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* C works *)
(x/a) /. {x -> (x - b)}
  Plot[%, {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

(* D doesn't work *)
test = (x/a) /. {x -> (x - b)}
 Plot[test, {x, 0, 2}, PlotRange -> {0, 1}]
, {a, 1, 2}, {b, 0, 1}]

(* E works *)
test2[x_, a_, b_] = (x/a) /. {x -> (x - b)}
  Plot[test2[x, a, b], {x, 0, 2}, PlotRange -> {0, 1}],
  {a, 1, 2},
  {b, 0, 1}]

Case A works, so the substitution is performed fine inside Plot and Manipulate.

But B doesn't, which I could understand as an issue trying to substitute a variable with an expression containing itself, but then, if you evaluate it beforehand, as in C, everything works again, so it has to be a problem with x being part of Plot, I guess.

Then if you assign the result of the substitution to a variable, you can't directly plot it, so it seems that variables are not evaluated if they are not functions (as in E, with pattern matching) inside Plot. But % is, so % is "special" as it gets evaluated inside plot while a standard symbol assigned to a value does not.

Can someone explain me all this? I guess it's related to the Hold attributes a function can have?

share|improve this question
You might also want to read this: – R. M. Feb 12 '13 at 2:09

Plot has Attributes HoldAll, so one possibility to get what you expect is to do just

SetOptions[Plot, Evaluated -> True];

at the beginning.

Another possibility (better documented) would be to use Evaluate inside Plot:

Plot[Evaluate[...], {x, 0, 2}]
share|improve this answer
I see, but why A works? I guess it's because when Plot comes to Evaluate the expression it first replaces x with a numeric value, and that replacement goes "inside" my /.{x->x-b} which then starts making very little sense... Right? – user61865 Feb 12 '13 at 1:14
Also, why % behaves in that "special" way, i.e. see C versus D – user61865 Feb 12 '13 at 1:15
@user61865. % is a shortcut for a function call. Evaluate FullForm@Hold@%; you will get Hold[Out[]]. – m_goldberg Feb 12 '13 at 2:00
"Evaluate inside Plot" better documented?! Now that's an understatement if I ever read one. The use of Evaluated ->True is essentially undocumented. – m_goldberg Feb 12 '13 at 2:05
@user61865. Your last comment is really another question. Don't ask a new question in a comment. Post it in a new question. – m_goldberg Feb 12 '13 at 15:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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