This seems to format numbers in an expression, but it probably could work better.
First, color rules:
colorrules = {
"0" -> "\*\n " <> ToString@ToBoxes@Style[0, Black],
"1" -> "\*\n " <> ToString@ToBoxes@Style[1, Red],
"2" -> "\*\n " <> ToString@ToBoxes@Style[2, Green],
"3" -> "\*\n " <> ToString@ToBoxes@Style[3, Blue],
"4" -> "\*\n " <> ToString@ToBoxes@Style[4, Cyan],
"5" -> "\*\n " <> ToString@ToBoxes@Style[5, Magenta],
"6" -> "\*\n " <> ToString@ToBoxes@Style[6, Yellow],
"7" -> "\*\n " <> ToString@ToBoxes@Style[7, Orange],
"8" -> "\*\n " <> ToString@ToBoxes@Style[8, Pink],
"9" -> "\*\n " <> ToString@ToBoxes@Style[9, Purple]}
Here is a cheap way, but it doesn't handle fractions gracefully. It should be sufficient for simple numbers or lists of real/complex numbers.
colorForm[expr_] := expr /. n_?NumberQ :> StringReplace[ToString@n, colorrules];
N[(-Pi)^(1/3)]
(N[(-Pi)^(1/3)] + 45 x)^2 // colorForm
2/3 // colorForm
Here is a somewhat more sophisticated approach that handles fractions, radicals, as well as the typeset E
and complex I
. The use of Interpretation
is perhaps over-ambitious, since copying the output and pasting it usually results in a very large expression. Another difference is that it shows all digits of machine real numbers.
ClearAll[colorForm];
colorForm[expr_, form_: StandardForm] := With[{colored = DisplayForm[
ToBoxes[expr, form] /.
s_String :>
With[{x = Quiet@ToExpression@s},
RowBox@List@StringReplace[ToBoxes@s, colorrules] /;
MatchQ[x, _Real | _Integer]]]},
Interpretation[colored, expr]
]
N[(-Pi)^(1/3)]
(N[(-Pi)^(1/3)] + 45 x)^Sqrt[2] // colorForm
2/3 // colorForm