Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Using the settings under Edit-> Preferences->Evaluation I can redirect all the Print statments to the console.

Settings dialog

What I'd really like to do is redirect just some specific Print statements to the console. I know I can print to another notebook with something like:

PrintToNotebook[nb_, expr_] := ( 
   SelectionMove[nb, After, Cell]; 
   NotebookWrite[nb, Cell[BoxData[ToBoxes[expr]], "Print"]]) 

So perhaps this boils down to finding the "Console Handle"?

Question: How can I create a function (eg PrintToConsole) that behaves like Print but outputs to the console?


Turns out the trick is in MessagesNotebook[]. This is what I'm currently using

$OldLine = -1;
       expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
        Cell[BoxData[ToBoxes[expr]], "Print", 
         CellLabel ->  "During evaluation of In[" <> ToString@$Line <> "]:=", 
     ShowCellLabel -> ($OldLine =!= $Line)]];
   $OldLine = $Line;);
   expr__] := (SelectionMove[MessagesNotebook[], After, Cell];
    Cell[BoxData[ToBoxes[Row@{expr}]], "Print", 
     CellLabel -> "During evaluation of In[" <> ToString@$Line <> "]:=",
         ShowCellLabel -> ($OldLine =!= $Line)]];
       $OldLine = $Line;);
share|improve this question
up vote 15 down vote accepted

The "Messages" window or the console is just another notebook, referenced by MessagesNotebook[]. So to print directly to that, use:

PrintToConsole[expr_] := (SetSelectedNotebook[MessagesNotebook[]]; 
    NotebookWrite[SelectedNotebook[], Cell[BoxData[ToBoxes[expr]], "Print"]])

This will print to the console and also brings it to focus. To make the notebook visible if it already isn't, add SetOptions[MessagesNotebook[], Visible -> True] in the definition above. However, I don't think that either of them are good UX, because it might get annoying if you're printing something continuously, and want to work in a different notebook in the mean time.

To get the same behaviour as Print with the During evaluation of... lines printed, use:

PrintToConsole[expr_] := (SelectionMove[MessagesNotebook[], After, Cell];
        Cell[BoxData["During evaluation of In[" <> ToString@$Line <> "]"], "CellLabel"]];
    NotebookWrite[MessagesNotebook[], Cell[BoxData[ToBoxes[expr]], "Print"]]) 

enter image description here

share|improve this answer
Thanks! I didn't find anything useful in the help under "Console". And under "Message window" there was this unhelpful entry. – Ajasja May 18 '12 at 18:24
This prints to the Messages window, but it does not cause the window to become visible. Can you add that functionality? – Mr.Wizard May 19 '12 at 7:29
@Mr.Wizard See edit. – R. M. May 19 '12 at 17:45
On version 7 this now prints to the evaluation Notebook if the Messages window is not already open. – Mr.Wizard May 19 '12 at 22:45
Well, you just need to change Visible -> True, but I'm not sure if that's a good thing. Even the focus grabbing isn't good UX. The user can always choose to turn it visible or not (from the menu bar), but popping it up every time makes it annoying. – R. M. May 19 '12 at 22:57

Your Answer


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.