3
$\begingroup$

I know about the possibility to use Style in Print to influence the color of the next item, e.g.

Print[
 Style["this prints red ", Red], 
 "now back to default black ", 
 Style["this prints green ", Green]
]

I just want to print some lines in an extended output with some other color but I want to avoid to enclose every single item in e.g. Style[item, color].

What I am after:

Something to mix into a Print statement that applies for the rest of this particular Print statement up to the next style switching item in it but not beyond the end of this particular Print statement. Something like the well known escape sequences control printers which use character fonts rather than graphics raster printing.

$\endgroup$

2 Answers 2

4
$\begingroup$

A little bit unclear to me but maybe this:

foo = Print @@ (Style[Row[{##2}], #] & @@@ (Split[#, 
       Not@MatchQ[#2, _Directive] &])) &

foo @ {  Directive[Red], "this prints", "red ", Directive[{}], "now back", 
 "to default black ",  Directive[Green], "this prints green"
}

enter image description here

$\endgroup$
8
  • 1
    $\begingroup$ +1 In case OP is interested in giving colors only, the Split expression could be changed to Split[#, Not@ColorQ[#2] &]. $\endgroup$
    – user31159
    Commented Sep 23, 2016 at 10:48
  • $\begingroup$ @Xavier yep, wanted to keep that more flexible but good reminder. $\endgroup$
    – Kuba
    Commented Sep 23, 2016 at 10:50
  • $\begingroup$ Yes, Kuba, I was after something like what you provided. Your version even works for more than one feature like colour and bold: foo @ { Directive[Red], "this prints N[Pi] in red:", N[Pi], Directive[{}], " now back", "to default", Directive[Bold], " black ", Directive[Green], "this prints", Directive[{Green, Bold}], " green" }. It woks find, but how does it do that? I tried to decompose your fancy function foo, but I did not get the clue. Thanks for your help! $\endgroup$ Commented Sep 25, 2016 at 13:22
  • $\begingroup$ Would it be possible to reformulate the function foo to a function called PRINT such that it can be used like this: PRINT["this modifies Print to switch to ", Directive[Red], " red ",N[Pi,10], "...", Directive[{}]," that's it."]? Would it be possible to automatically apply List to provide Kuba's form of arguments, i.e. a list of arbitrary length instead of arbitrarily many arguments? $\endgroup$ Commented Sep 25, 2016 at 15:40
  • $\begingroup$ @AdalbertHanßen Hi, sorry for a delay, I'm encouraging you to try it step by step. First use Split... part on an example input, then @@@ with Style. It should become clearer, if not feel free to ask further :) $\endgroup$
    – Kuba
    Commented Sep 26, 2016 at 7:06
1
$\begingroup$

In Mathematica problems like this are often easily solve by writing simple functions that act as shortcuts for tedious typing. In your case, you could write

red[txt_] := Style[txt, Red]
grn[txt_] := Style[txt, Green]

Print[
  red @ "this prints red ",
  "now back to default black ",
  grn @ "this prints green "]
$\endgroup$

Your Answer

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

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