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.

How to let output and message be print to two separate external files? Also, I'd like the content print out to have similar look as when it's print inside the notebook, i.e. it should be in OutputForm or whatever proper form.

share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

Sounds like you are looking for $Messages and $Output, and maybe also $PrePrint or $Post. $Messages and $Output are a list of streams to which corresponding output is written (note that $Output is only getting output from Prints, not from return values of shift-return-evaluations). To cover return values of shift-return-evaluations you might want to manipulate $PrePrint or $Post so it will also write to the desired output file.

You can open a file for each with OpenWrite and append the resulting streams to the two lists and the messages and output will be written to those files. You can use the option FormatType of OpenWrite to write to those files in OutputForm. But since these are text files the outcome is not very useful in my opinion: For lengthy expressions the multiline ASCII art is usually harder to read than input form and these files will not be readable for any program. So I'd rather stick with the default InputForm.

For return values of shift-return-evaluations you could use something like:

$PrePrint = Function[Write[outstream,#];#]

where outstream is the open stream to your output file.

share|improve this answer
    
After setting FormatType -> OutputForm, I get errors Intersection::normal: Nonatomic expression expected at position 2 in Intersection[{StandardForm, TraditionalForm}, FormatType]. in the file storing messages. Why? –  Problemaniac May 7 '12 at 21:17
    
What exactly do you do? The following works for me: fh = OpenWrite[fname, FormatType -> OutputForm];Write[fh, "test"];Close[fh]. Do you get the same error for this? If yes, which version of Mathematica are you using on which platform? –  Albert Retey May 8 '12 at 7:39
    
No. I don't know exactly all the details of the program I'm running. But I'm guessing, it's trying to print something whose up value isn't defined for OutputForm. –  Problemaniac May 8 '12 at 20:02
    
@MaThEmAtika: I see. It's hard to say what's going on without seeing the code. It's probably not even clear whether the problem is with my suggestion, the program you are running or Mathematica itself. If you find out what's causing the problem I'd be interested to see what it is, although I probably can't provide a solution.... –  Albert Retey May 8 '12 at 23:28
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.