Message Boards Message Boards

1
|
6039 Views
|
6 Replies
|
9 Total Likes
View groups...
Share
Share this post:

How would I make NotebookWrite behave the same way as CellPrint?

Posted 12 years ago
Here is an example of using CellPrint. Note that the Syntax highlighting is preserved:
CellPrint[ExpressionCell[Defer[Module[
    {x, y, z},
    z = x y;
    Sin[z]
    ]], "Input"]]

Here is an example of a something very similar using NotebookWrite:
newNB = CreateWindow[{}]

NotebookWrite[newNB,
ToBoxes[
  ExpressionCell[
   Defer@Module[{x, y, z}, z = x y; Sin[z]]
   , "Input"
   ]]]

Note that the syntax is not highlighted.  Can anyone see how to make these behave the same with respect to highlighting?
POSTED BY: W. Craig Carter
6 Replies
Thanks Brian and John---one question leads to another:

This small modification with a FontSize option for the Cell works:
newNB = CreateWindow[{}]

NotebookWrite[newNB,
Cell[BoxData[MakeBoxes[Module[{x, y, z}, z = x y; Sin[z]]]], "Input",
   Background -> GrayLevel[0.95],
  FontSize -> 24]]

But, I am curious why the FontSize option is not working in this:
CreateDialog[
Cell[BoxData[MakeBoxes[Module[{x, y, z}, z = x y; Sin[z]]]], "Input",
   Background -> Lighter@Pink, FontSize -> 24]]

CreateDialog returns a NotebookObject, but NotebookWrite doesn't.  However, I would have thought that CreateDialog creates a NotebookObject with the specified Cell and then returns the NotebookObject that it created.

I couldn't get this to work on John's version as well.

Addendum: After a bit of digging (i.e., saving the Dialog and looking inside the file.nb) , I see that the Dialog has the delayed rule option:   FontSize:>CurrentValue["PanelFontSize"]...
The documentation seems to imply that this would work:

CurrentValue["PanelFontSize"] = 24
But, CurrentValue is apparently Protected; so I tried this:
Unprotect[CurrentValue]
CurrentValue["PanelFontSize"] = 24


But, it didn't have any effect on the Dialog window.
POSTED BY: W. Craig Carter
Right.  CreateDialog does some automatic option settings when it receives a Cell or list of Cells.  If you use a Notebook expression though:

CreateDialog[
 Notebook[{Cell[
    BoxData[MakeBoxes[Module[{x, y, z}, z = x y; Sin[z]]]], "Input",
    Background -> Lighter@Pink, FontSize -> 24]}]]

then your options -- for Cell or Notebook -- should be respected.
Thanks for the boost up the learning curve!
POSTED BY: W. Craig Carter
This also keeps the highlighting:
NotebookWrite[newNB,
  Cell[BoxData[MakeBoxes[Module[{x, y, z}, z = x y; Sin[z]]]],
   "Input"]]
but it doesn't use the same ExpressionCell object. I'm not sure if that's a requirement, though.
Thanks John. That's exactly what I needed. Craig
POSTED BY: W. Craig Carter
newNB = CreateWindow[{}]
NotebookWrite[newNB,
Cell[BoxData@ToBoxes@Defer@Module[{x, y, z}, z = x y; Sin[z]],
  "Input"]]
POSTED BY: John Fultz
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract