Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Formatting Input Cells within a NotebookWrite[]
Replies: 3   Last Post: Mar 2, 2013 2:34 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Park

Posts: 1,557
Registered: 5/19/07
Re: Formatting Input Cells within a NotebookWrite[]
Posted: Feb 27, 2013 11:42 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Craig,

If you wanted to get things exactly as you typed them you might type them,
expose the underlying expression and them copy that box structure into your
NotebookWrite. For example:

NotebookWrite[EvaluationNotebook[],
RowBox[{"Integrate", "[",
RowBox[{RowBox[{"x", " ", RowBox[{"y", "^", "2"}]}], ",",
RowBox[{"{", RowBox[{"y", ",", "1", ",", "2"}], "}"}]}], "]"}]]

This is a bit of work if you want to do much of it.

But how are you using this that it is so important that it be exactly as
typed?


David Park
djmpark@comcast.net
http://home.comcast.net/~djmpark/index.html






From: W Craig Carter [mailto:ccarter@MIT.EDU]


Hello Mathgroup,

I'd like to have NotebookWrite[] create an input cell *exactly* as I have
typed that input cell. I'm trying to create set of tutorials with exercises
and worked solutions.

Here are two *almost working* method, however they don't reproduce the code
as it is written---which is what I'm trying to do. I'm stuck.

(*--------------------------Method 1--------------------------------*)
SetAttributes[expressionCell, HoldAll];

expressionCell[expr_] :=
NotebookWrite[EvaluationNotebook[],
Cell[Block[{BoxForm`$UseTextFormattingWhenEvaluating = True},
RawBoxes[MakeBoxes[expr]]], "Input",
LanguageCategory -> "Mathematica"]]

(*examples---these "almost" work, but the FrontEnd converts to StandardForm
2d form and removes indentation. I'm trying to make it look more like
InputForm*)

expressionCell[Integrate[x y^2, {y, 1, 2}]]

expressionCell[
Manipulate[x y^2,
{y, 1, 2}]
]

expressionCell[
Module[{x, y, z},
x y z
]
]

(*--------------------------Method 2, wrap expr in
InputForm--------------------------------*)

Clear[expressionCell]
SetAttributes[expressionCell, HoldAll];

expressionCell[expr_] :=
NotebookWrite[EvaluationNotebook[],
Cell[Block[{BoxForm`$UseTextFormattingWhenEvaluating = True},
RawBoxes[MakeBoxes[InputForm[expr]]]], "Input",
LanguageCategory -> "Mathematica"]]

(*examples---these work, but I am trying to indicate to students what I am
typing, and so wish to eliminate the explicit *-multiplication, and I'd like
to keep my line returns and indentation on the Module example
*)

expressionCell[Integrate[x y^2, {y, 1, 2}]]

expressionCell[Manipulate[x y^2, {y, 1, 2}]]

expressionCell[
Module[{x, y, z},
x y z
]
]


This is a follow on question to "Re: Using Defer on Dynamic Expressions?"
which was answered by John Fultz and by Mark Maclure on the Wolfram
community site.

W Craig Carter
Professor of Materials Science, MIT





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.