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.

I'm trying to embed a "Manipulate" applet I've created into a webpage by embedding a .cdf. I'm running into a problem with displaying outputs from functions I've written. Here is a simpler example that demonstrates the problem.

If I code something this way (which involves defining a new function):

Moof[n_] := Append[{Circle[{0, 0}, n]}, Circle[{1, 1}, 3]]
Manipulate[Graphics[Moof[a]], {a, 1, 4}]

then I get on the webpage the error "Moof is not a Graphics primitive or directive." This code works fine within Mathematica when the appropriate cell is evaluated.

On the other hand, if I code the same Manipulate applet without defining a new function:

Manipulate[Graphics[Append[{Circle[{0, 0}, a]}, Circle[{1, 1}, 3]]], {a, 1, 4}]

then everything works fine on the webpage.

Here is what the two different codes produce on a live webpage.

Unfortunately, the applet that I actually want to post is considerably more complicated and it would be difficult to write the Manipulate command all in native Mathematica functions. I assume that the difficulty has something to do with initialization of my user-defined functions. Is there any way around this besides rewriting my code from the ground up? Any advice on how to resolve this would be greatly appreciated.

share|improve this question
1  
A manipulate for a cdf must be completely self contained. Use the Initialization option to define your function there. –  halirutan Oct 21 '12 at 16:26
    
Possible duplicate: mathematica.stackexchange.com/q/11705/57 –  Sjoerd C. de Vries Oct 21 '12 at 18:23
add comment

2 Answers

up vote 5 down vote accepted

As stated in the comment, you cannot simply use functions defined outside your Manipulate because once the kernel is stopped (which happens when you save your cdf and use it anywhere else) those functions are not defined anymore.

Therefore, one possibility is to use the Initialization option

Manipulate[Graphics[Moof[a]], {a, 1, 4}, 
 Initialization :> (Moof[n_] := Append[{Circle[{0, 0}, n]}, Circle[{1, 1}, 3]])]

Once you created a Manipulate you should always test it by quitting the Kernel with (Evaluation -> Quit Kernel) and trying to move the sliders. If everything works it is likely your Manipulate contains everything required.

share|improve this answer
    
Thanks! I had seen "Initialization" in the documentation, but it didn't click until you explained and reassured. Here's how my applet turned out! Gothic Circle Patterns It could probably use some speeding up, but I'm pretty proud of getting it to its current operational state. Thanks again! –  Justin Lanier Oct 22 '12 at 3:05
add comment

SaveDefinitions should work too.

Manipulate[Graphics[Moof[a]], {a, 1, 4},SaveDefinitions->True]

After executing the Manipulate function you can now move the result to an empty notebook and save as CDF. The necssarry function definition are stored with it.

This is very similar to this question. The only difference is that there the OP wants to store data and you want to store a function definition. There is no difference in principle.

share|improve this answer
add comment

Your Answer

 
discard

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.