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 is a further question to my previous one. The solution for that question works well as long as the original input Manipulate code is still provided in the notebook. But in general, there are cases that only the output cell is included and the input code omitted. In such case, I think we could possily make the Manipulate remembers its own input code, and by request (e.g. a button) we can show the code.

A first attempt starts with either DynamicModule or Pattern. For instance, I tried

DynamicModule[{mc = HoldComplete@Manipulate[
 Column[{
   Plot[Sin[x - n], {x, 0, 12.4}],
   Button["show code",
    NotebookPut[Notebook[{Cell[BoxData[ToBoxes[mc]], "Intput"]}], 
     EvaluationNotebook[]]
    ]}], {n, .3, 11.8}]},
 ReleaseHold[mc]]

where mc is the short for 'manipulatecode'. But the code will not be shown if we click the button, only the output instead.

I also tried

mem : Manipulate[
Column[{
Plot[Sin[x - n], {x, 0, 12.4}],
Button["show code",
 NotebookPut[Notebook[{Cell[BoxData[ToBoxes[mc]], "Intput"]}], 
  EvaluationNotebook[]]
 ]}],
{n, .3, 11.8},
Initialization :> (mc := HoldComplete[mem];)]

But this will not work also. Is there a way to let the button creates a new cell and show the Manipulate code in this cell (better in expression format)?

share|improve this question
    
Do you want to store the code to re-evaluate Manipualteor to show it? Printable CellData is not readable –  Kuba Oct 7 '13 at 6:55

1 Answer 1

up vote 2 down vote accepted
manipulatecode = NotebookRead@EvaluationCell[];

Manipulate[
 Column[{
         Plot[Sin[x - n], {x, 0, 1}],
         Button["PrintCode", CellPrint[manipulatecode];]
       }]
 , {n, 0, 1}
 , SaveDefinitions -> True
 ]

And then, even if you delete the input code you can print it. Does it fit your needs?

share|improve this answer
    
Thank you Kuba, I can try to add some additional functions based on your solution now. Previously I was thinking on the direction of meta programming, but I was too busy to finish it. –  saturasl Oct 14 '13 at 17:57

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.