# How to generate an input cell?

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?

• CellPrint + ExpressionCell[Defer@code, "Input"]? Commented Apr 30, 2017 at 12:25
• 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? Commented Apr 30, 2017 at 12:29
• 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. Commented Apr 30, 2017 at 13:23
• Strongly related: "Output of code generated by CellPrint goes to the end of notebook" Commented Jul 20, 2022 at 22:36

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 about a default option value for CellPrint is GeneratedCell -> True (see also this comment by John Fultz), we should let options be passed.]

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


Examples:

Or following Szabolcs:

gencode[Module[{a, b}, a = 1; b = 2; a + b], GeneratedCell -> False]

• +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. Commented Apr 30, 2017 at 13:37
• @Szabolcs Thanks for such valuable comment. +1 : ) Commented Apr 30, 2017 at 14:20
• 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. Commented Mar 19, 2021 at 19:04
• @Kvothe With[{a = a}, gencode[b = a]]? Or did I misunderstand what you wanted? Commented Mar 19, 2021 at 19:15
• @MichaelE2, thanks! Commented Mar 19, 2021 at 19:33

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.

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