# styles and print and output

I know I can use styles to change how the result of Print looks. But I have a case where I use Print to display some informational output and also some result:

a=2;
b=3;
c=a+b;
Print["Variable a = ", a]; (* this I want with font size 12, or let's say Style1 *)
Print["Variable b = ", b]; (* this I want with font size 12, or let's say Style1 *)
Print["The result: a + b = ", c]; (* this I want with font size 16 with background color and in bold and probably different font, or let's say Style2 *)


The whole point of this is that I need the result printed after some text (i.e. in the same line). I know there are some solutions but all of them are enormously complicated. Print is simple and quick. But I need to distinguish the style of those prints. I can assign different styles manually to the results (Style2) but I have to redo it after each evaluation of the notebook.

## Update

To give exact code:

p0 = Quantity[10^5, "Pascals"];
rE = Quantity[6378*10^3, "Meters"];
gE = Quantity[9.81, ("Meters")/("Seconds")^2];
m = UnitConvert[(p0*4*π*rE^2)/gE, "SIBase"];
Print["\!$$\*SubscriptBox[\(p$$, $$0$$]\) = ", p0];
Print["R = ", rE];
Print["g = ", gE];
Print["Mass of the atmosphere is: ", m];


If I do what @MarcoB suggested in his comment, it looks:

styleRes =
Style[#, FontSize -> 18, FontWeight -> Bold,
Background -> LightRed] &;
p0 = Quantity[10^5, "Pascals"];
rE = Quantity[6378*10^3, "Meters"];
gE = Quantity[9.81, ("Meters")/("Seconds")^2];
m = UnitConvert[(p0*4*π*rE^2)/gE, "SIBase"];
Print["\!$$\*SubscriptBox[\(p$$, $$0$$]\) = ", p0];
Print["R = ", rE];
Print["g = ", gE];
Print[styleRes@StringJoin["Mass of the atmosphere is: ", ToString@m]];


And you can see the horror of caused by ToString. This is why I preferred to stick with Print["text",expression] because that gets the formating absolutely right, ToString messes everything up.

If there is a way to make a "bullet proof" ToString without spending hours to just write the code, I am all ears. I know, I could use ToString[expr,TraditionalForm] but I simply do not trust it to render results the same as Mathematica renders the output (e.g. TraditionalForm does not make space between thousands and I frankly have no idea what other caveats it has).

• Yes, but unsure how to apply it to whole Print. – leosenko Jan 25 '16 at 16:42

Maybe this will fit your needs:

myPrint[args__, {style__}] := Print[Row[{args}, BaseStyle -> {style}]]


myPrint["Mass of the atmosphere is: ", m,
{FontSize -> 18, FontWeight -> Bold, Background -> LightRed}]

myPrint["Mass of the atmosphere is: ", m, {"Section"}]


style1 = Style[#, 12] &;
style2 = Style[#, 24, Blue, Background -> Pink] &;


and then just use them quickly and easily,

Print["Variable a = ", style1@a];
Print["Variable b = ", style1@b];
Print["The result: a + b = ", style2@c];


• Thanks but I would like the whole line with that style, i.e. the whole printout of a single print command be formatted, not just the expression. – leosenko Jan 25 '16 at 16:43
• @leosenko ... then you may want to join the expressions in a single string, to which you can apply your style, within the Print command. For instance: Print[style2@StringJoin["The result: a + b = ", ToString@c]]; – MarcoB Jan 25 '16 at 17:39
• Is there any way to avoid ToString? See my updated question. – leosenko Jan 25 '16 at 18:15