For example, I have a number with style like so:
Style[1, Red, 20]
How do I remove its style, so it returns to its inner value 1
? Is there a function that can allow me to remove the head "Style"?
Without greater context I recommend a simple ReplaceAll
operation, to work on all appearances of Style
at any level.
{1, Style[2, Red], 3, Style[4, Red]} /. Style[x_, ___] :> x
{1, 2, 3, 4}
If you want to strip Style
from only numbers then you could use something like:
/. Style[x_?NumberQ, ___] :> x
This should work (untested):
removeStyle[expr_Style] := removeStyle@First[expr]
removeStyle[expr_] := expr
The best solution would be to avoid storing styled numbers in the first place, if you intend to compute with them. Only style them at the last minute before displaying them, but do not store the styled version if you can avoid it.
Perhaps something like (not extensively tested)?
stripStyle = ToExpression@*StripBoxes@*ToBoxes
which strips general non-semantic elements from an expression
{1, Style[2, Red], 3, Style[4, Red], Framed[6]} // stripStyle
(* {1, 2, 3, 4, 6} *)
You can use the Format | Clear Formatting
menu item for this. Highlight the offending symbols, and use the menu item. There should also be a keyboard shortcut, on MacOS it is Shift + Command + Delete.
First
. $\endgroup$