[Date Index] [Thread Index] [Author Index]
Re: creating input cell with unevaluated expression?
On 30 Sep 1999, Aaron Gross wrote: > Can anyone tell me how to do the following? I want to write an > expression that creates and inserts (at the current position in the > front-end) an input cell containing a given *unevaluated* expression, > just as if the user had typed it in himself. For instance, I'd like > to create an input cell containing the expression "1+1", so that if > the user then evaluates the cell, he gets an output cell containing > "2". > > I tried > > StylePrint[HoldForm[1+1],"Input"] > > but that doesn't work -- the input cell still has the "Hold" tag, so > when the user evaluates it by hand, he gets an output cell containing > "1+1" rather than "2". > > I also tried > > StylePrint[Unevaluated[1+1],"Input"] > > but that just gave me a "2" in the input cell. So what's the "right" > way to do this? Define a function named createInputCell like so: createInputCell[expr_] := ( SelectionMove[EvaluationNotebook, After, EvaluationCell]; NotebookWrite[ EvaluationNotebook, Cell[BoxData[ToBoxes[Unevaluated[expr], StandardForm]], "Input"] ] ) Endow it with the attribute HoldFirst. SetAttributes[createInputCell, HoldFirst] Now watch the magic! createInputCell[Integrate[Sin[x], x]] If you don't like two-dimensional typeset forms, you can replace the argument of Cell to ToString[Unevaluated[expr], InputForm] -- P.J. Hinton Mathematica Programming Group paulh at wolfram.com Wolfram Research, Inc. Disclaimer: Opinions expressed herein are those of the author alone.