# Can I mimic Mathematica's Messages functionality?

In a nutshell I'm trying to mimic Mathematica's Message window functionality in that I'm trying to Print to a notebook other than the invoking notebook. That is, as my process is running from Notebook1, process information is output to Notebook2 (which is currently displayed as well).

I've explored and am currently using a NotebookWrite implementation but display of matrices or algebraic expressions is problematic given the restrictions on the Cell data expected in that command. My current implementation is using,

NotebookWrite[outputNotebook, Cell[TextCell[outMsg], "Print"], After];


However, this is limited to text messages so trying to output a MatrixForm[...] of information appears as text of the command rather than in actual matrix format.

I've considered redirecting the \$Output and using Print, but it seems that approach expects "stream-level" writing to a file and doesn't accommodate an open notebook as the destination.

Ultimately I would like to use a simple wrapper on Print and make use of its robust support of multiple, varied-type, arguments to print, such as,

myPrint["Here's the information:", {x^2, y^2}, MatrixForm[{{a,b},{c,d}}] ];


and have it show as

$$\text{Here's the information:} \{x^2, y^2\}, \begin{bmatrix}a&b\\ c&d\end{bmatrix}$$

Any suggestions, or directions on where to investigate would be greatly appreciated.

• Cell + BoxData + ToBoxes + Row like in 32890, does it fit your needs? Or better, take a look at topics linked to that one in comments and tell us which fits the best. – Kuba Mar 9 '17 at 16:35
• @Kuba TextCell, ExpressionCell are a bit confusing to me and I never took the time to understand them. I just go directly to Cell and BoxData. Do I understand it correctly that these can only be used with CellPrint, which will them produce a proper Cell out of them? (Of course additionally they display as they would when converted to Cell, but that's something else.) – Szabolcs Mar 9 '17 at 16:42
• @Szabolcs I must say that I am a little confused too. But not only CellPrint has this property: CreateDocument@ExpressionCell[Defer[1 + 1], "Input"] – Kuba Mar 9 '17 at 16:45

Based on the suggestions above, I found the following works. Now I can wrap it with my own myPrint as needed.

nb = CreateDocument[];
msg = "Test ";
poly = {x^2, y^2};
matx = {{a,b},{c,d}};
NotebookWrite[nb, Cell[BoxData[ToBoxes[Row[{msg, poly, Matrixform[matx]}]]], "Output"]];


Why this over the other options?

• CellPrint went to the current notebook, not the 2nd one;
• The links in the referred to previous questions primarily dealt w/ external files and not necessarily open secondary notebooks;
• ExpressionCell worked w/ CreateDocument but I'm not sure how to write subsequent information to that newly created notebook.

Many thanks!