Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'd like to NotebookWrite a Cell in StandardForm with a Grid to a new document. What I'd like to copy is

toCopy = TraditionalForm@Grid[{{a^2, b^2, c^2}, {d^2, e^2, f^2}}]

If I now use the following code

NotebookWrite[
 CreateDocument[]
 , toCopy]

I get a red rectangle in the new Notebookbook. I've tried to change all letters a to f to "a" ... "f", but nothing changed.

I am somewhat puzzled... What I missing here? Thanks for all help, as always!

Edit: With the answer of Alexey, it works good to copy the Grid to the new Notebook. But the TraditionalForm is lost: I get InputForm. Even using

NotebookWrite[
 CreateDocument[],
  ToBoxes[toCopy, TraditionalForm]]

doesn't seem to work. Any more help, as always, very much appreciated!

share|improve this question
add comment

2 Answers 2

You should pass boxes as the second argument to NotebookWrite:

toCopy = Grid[{{a^2, b^2}, {c^2, d^2}}];
NotebookWrite[CreateDocument[], ToBoxes@toCopy]

Answering your second question, it is sufficient to wrap the expression by TraditionalForm to get it in TraditionalForm:

NotebookWrite[CreateDocument[], ToBoxes@TraditionalForm@toCopy]

But it still gives you an "Input" Cell. If you wish to generate an "Output" Cell, you should specify it explicitly:

NotebookWrite[CreateDocument[], 
 Cell[BoxData@ToBoxes@TraditionalForm@toCopy, "Output"]]

P.S. Also, see relevant discussion in this thread: "Programmatically writing to a notebook in TraditionalForm."

share|improve this answer
    
Thanks, this works good for the copying, but changes back to InputForm (I think), and not TraditionalForm. I appreciate any help for this problem! –  Gabriel Nov 9 '13 at 10:22
    
@Gabriel Please see updated answer. –  Alexey Popkov Nov 9 '13 at 15:57
    
Thank you for the update! –  Gabriel Nov 10 '13 at 10:34
add comment

I found the solution analysing the output of Mathematica with "Show Expression".
To display the contents of a Cell in TraditionalForm, one has to use a
FormBox[ ..., TraditionalForm] around the boxes given to Cell (for example, using ToBoxes,
as follows:

NotebookWrite[ 
    CreateDocument[],
    Cell[ FormBox[ ToBoxes @ Grid[{{a^2, b^2}, {c^2, d^2}}], TraditionalForm] ]
             ]

Thanks for informing if I misunderstood somewhere along the lines... Hope this helps to other Mathematica users.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.