2
$\begingroup$

Can you extract the Style attributes from a variable. For example:

var = Style["aString", Bold, 24]

You can get back "aString" by using ToString[var] but I am not able to get the attributes {Bold, 24}.

$\endgroup$

1 Answer 1

3
$\begingroup$

Examine its FullForm:

var = Style["aString", Bold, 24];
var // InputForm

(* Out: Style["aString", Bold, 24] *)

Exactly as you typed in. You can manipulate it just as you would any Mathematica expression. For example:

List @@ Rest[var]

(* Out: {Bold, 24} *)
$\endgroup$
6
  • $\begingroup$ Did not think of replacing the Head on var. Perfect, thank you so much. Peter $\endgroup$
    – pjc42
    Commented Dec 27, 2013 at 1:10
  • $\begingroup$ There ought to be a way to compare Style["aString", Bold, 24] and Style["aString", FontWeight -> Bold, FontSize -> 24]... $\endgroup$
    – rm -rf
    Commented Dec 27, 2013 at 1:25
  • $\begingroup$ @rm-rf Isn't there? var2 = Style["aString", Bold, 24]; var1 = Style["aString", FontWeight -> Bold, FontSize -> 24]; OrderedQ[{var2, var1}] returns a reasonable result. Do you want var1 and var2 to be the same? $\endgroup$ Commented Dec 27, 2013 at 1:33
  • $\begingroup$ @MarkMcClure Yes, the same. I understand why Mathematica sees them differently and why SameQ is False... I was just surprised that both forms didn't have the same underlying representation (I thought Style["aString", FontWeight -> Bold, FontSize -> 24] would auto-evaluate to Style["aString", Bold, 24] or something like that). $\endgroup$
    – rm -rf
    Commented Dec 27, 2013 at 1:36
  • $\begingroup$ @rm-rf So you'd like Equal[var2,var1] to evaluate to True in the same way that Equal[1,1.0] evaluates. I can see that. But, are there other situations where Equal[a,b] evaluates to True where a and b are non-identical, non-numeric expressions? I'm not sure off the top of my head, but I haven't been thinking about code so much lately. It's a little far afield of the OP's intention, perhaps. :) $\endgroup$ Commented Dec 27, 2013 at 1:53

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.