1
$\begingroup$

So basically I have some code like this:

Manipulate[
With[{
a = h + 5,
b = a/2
}], {h, -10, 10}]

It works fine for a but doesn't calculate the value of b. the output for b is simply a/2, whatever I change h to has no effect on it.

$\endgroup$
2
$\begingroup$

Beside that the body of your With is missing, you are making a basic mistake: You assume that you can use a inside the definition braces, when you just defined a there. This does not work.

You need to nest two With constructs to do this

Manipulate[With[{a = h + 5},
  With[{b = a/2},
   {a, b}]
  ], {h, -10, 10}]

or since version 10 or so, you can use this form (which is shown in red but that is only due to the missing support of the highlighting):

Manipulate[
 With[{a = h + 5}, {b = a/2}, {a, b}],
 {h, -10, 10}
]
$\endgroup$
  • $\begingroup$ I tried it and indeed it works, but where is that form of With documented? Has Wolfram committed to this? $\endgroup$ – Alan Oct 22 '16 at 3:51
  • 1
    $\begingroup$ As far as I know this is an undocumented feature. Although it is really handy, the highlighter makes it impossible to use since I cannot stand when some 20 lines of correct code are marked red :) But I thought, I let you know. $\endgroup$ – halirutan Oct 22 '16 at 4:05
  • $\begingroup$ @Alan Undocumented. See: mathematica.stackexchange.com/a/121173/121 $\endgroup$ – Mr.Wizard Oct 22 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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