2
$\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$ Commented Jan 12, 2023 at 13:22
  • $\begingroup$ Yes. Exactly. I just want to change Print and not change any other part of the code. $\endgroup$ Commented Jan 12, 2023 at 13:24
  • 2
    $\begingroup$ Have you tried using the Villegas-Gayley trick to override Print? $\endgroup$ Commented Jan 12, 2023 at 13:40

1 Answer 1

1
$\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$ Commented Jan 16, 2023 at 15:41
  • $\begingroup$ Of course you are right. Thanks. $\endgroup$ Commented Jan 16, 2023 at 15:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.