6
$\begingroup$

For example I have a block of code

Module[{}, a=1;b=2;...]

I want to have a function called gencode, so when I run gencode, it will generate an input cell which contains this block of code just like I type them by hand.

How to write this gencode?

$\endgroup$
3
  • 1
    $\begingroup$ CellPrint + ExpressionCell[Defer@code, "Input"]? $\endgroup$ – Michael E2 Apr 30 '17 at 12:25
  • $\begingroup$ Hi, @MichaelE2. This works great. You could post it as an answer. What is more, I search the doc before, wish to find some tutorial page to deal with these cell things. But I can not find a suitable one. Could you recommend a link? $\endgroup$ – matheorem Apr 30 '17 at 12:29
  • $\begingroup$ I don't really know a good reference. There's is this guide: reference.wolfram.com/language/guide/DocumentGeneration.html. I feel this is one my weaker areas of knowledge about Mathematica, even though I've generated homework assignments with such methods. Perhaps it's because there is not a good tutorial on it. $\endgroup$ – Michael E2 Apr 30 '17 at 13:23
7
$\begingroup$

CellPrint plus ExpressionCell are tools for generating cells. They do not hold their arguments, so some control of evaluation is necessary (Defer). [Update: In response to Szabolcs' comment, may as well let options be passed.]

ClearAll[gencode];
SetAttributes[gencode, HoldAll];
gencode[code_, opts : OptionsPattern[Cell]] := 
 CellPrint@ExpressionCell[Defer@code, "Input", opts];

Examples:

Mathematica graphics

Or following Szabolcs:

gencode[Module[{a, b}, a = 1; b = 2; a + b], GeneratedCell -> False]
$\endgroup$
7
  • 3
    $\begingroup$ +1, this is exactly what Defer is for. One note: the Cell will have GeneratedCell -> True . This means that Cell -> Delete All Output will delete it. It depends on the use case whether this is desired or not (I think usually it is not). One can always set GeneratedCell -> False in ExpressionCell. $\endgroup$ – Szabolcs Apr 30 '17 at 13:37
  • $\begingroup$ @Szabolcs Thanks for such valuable comment. +1 : ) $\endgroup$ – matheorem Apr 30 '17 at 14:20
  • $\begingroup$ I don't know whether this should be a separate question but how do I appropriately control evaluation in the case where for example I have set the value a=1 already and now want to generate b=1 by using something of the form gencode[b=a]? That is I do want to fully evaluate both b and a but I don't want to evaluate the final set. $\endgroup$ – Kvothe Mar 19 at 19:04
  • 1
    $\begingroup$ @Kvothe With[{a = a}, gencode[b = a]]? Or did I misunderstand what you wanted? $\endgroup$ – Michael E2 Mar 19 at 19:15
  • $\begingroup$ @MichaelE2, thanks! $\endgroup$ – Kvothe Mar 19 at 19:33
4
$\begingroup$

Here's another way:

NotebookWrite[
    EvaluationNotebook[]
  , Cell[ BoxData @ MakeBoxes @ Module[{a, b}, a = 1; b = 2; a + b], "Input"]
]

even this will do

NotebookWrite[
    EvaluationNotebook[]
  , MakeBoxes @ Module[{a, b}, a = 1; b = 2; a + b]
]

But only because "Input" is the default cell style.

$\endgroup$
4
  • 2
    $\begingroup$ Or equivalently using Paste: Paste@Cell[BoxData@MakeBoxes@Module[{a, b}, a = 1; b = 2; a + b], "Input"]. $\endgroup$ – Alexey Popkov Apr 30 '17 at 14:57
  • $\begingroup$ Hi, @Kuba. Thank you so much for introducing fundamental Boxes, learned a lot : ) +1 $\endgroup$ – matheorem Apr 30 '17 at 15:39
  • 1
    $\begingroup$ Hi, @AlexeyPopkov I just found this could be as simple as Paste@Defer@Module[{a, b}, a = 1; b = 2; a + b] $\endgroup$ – matheorem May 1 '17 at 1:45
  • 1
    $\begingroup$ @matheorem But this generates an "Output" cell, not an "Input" cell. $\endgroup$ – Alexey Popkov May 1 '17 at 5:14

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.