MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: creating input cell with unevaluated expression?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20133] Re: creating input cell with unevaluated expression?
  • From: "P.J. Hinton" <paulh at wolfram.com>
  • Date: Sat, 2 Oct 1999 03:04:58 -0400
  • Organization: "Wolfram Research, Inc."
  • References: <7suv3g$21l@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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.



  • Prev by Date: Re: Mathematica programming language
  • Next by Date: Re: Mathematica programming language
  • Previous by thread: RE: creating input cell with unevaluated expression?
  • Next by thread: Re: creating input cell with unevaluated expression?