1
$\begingroup$

In the output of a script that calls Mathematica I would like to be able to differentiate lines printed by mathematica from lines printed by something else. I thought of prefacing lines printed by Mathematica with for example "m:". Is this possible?

$\endgroup$
3
  • $\begingroup$ Do you mean that you want to change how Print behaves without having to change the code that uses Print? $\endgroup$
    – Szabolcs
    Jan 12 at 13:22
  • $\begingroup$ Yes. Exactly. I just want to change Print and not change any other part of the code. $\endgroup$
    – Kvothe
    Jan 12 at 13:24
  • 2
    $\begingroup$ Have you tried using the Villegas-Gayley trick to override Print? $\endgroup$
    – Szabolcs
    Jan 12 at 13:40

1 Answer 1

0
$\begingroup$

As suggested by Szabolcs, I used the Villegas-Gayley method

Unprotect[Print];

Print[args___]:=Block[{$inMsg=True},
Print["|m:>",args,"<:m|"]
]/;!TrueQ[$inMsg]

Protect[Print];

This works well. Ideally I could do the same also for warning messages but I haven't quite figured out yet what the appropriate change would be to Message.

$\endgroup$
2
  • $\begingroup$ Just a small comment: you don't need result, just write Print["|m:>", args, "<:m|"];. $\endgroup$
    – Domen
    Jan 16 at 15:41
  • $\begingroup$ Of course you are right. Thanks. $\endgroup$
    – Kvothe
    Jan 16 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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