I am confused about when to use a simple list of style options, the Directive function or the Style function.

As shown below, for simple situations at least, they seem entirely equivalent which suggests personal preference.

This question regarding the requirement for the use of Directive indicates that aside from a some small edge cases, Directive is mostly preferred as easier to read and has the useful property that a single compound graphics Directive can be stored as a variable and referenced where needed.

That would have been enough for me except the Elementary Introduction Chapter 8 doesn't mention either of these and introduces Style. Style seems to be more widely applicable so perhaps it was chosen to minimise the number of functions introduced to a new user.

I am mostly interested in which method a beginner should start with as their default rather than trying to use all 3 at random - I'm leaning toward Directive since it translates well to Plot but again, all methods work so what am I missing?

       Red, Thick, Circle[],
       Blue, Rectangle[]
       Directive[Red, Thick], Circle[],
       Directive[Blue], Rectangle[]
       Style[Circle[], Red, Thick],
       Style[Rectangle[], Blue]
  • $\begingroup$ Related: (135062) $\endgroup$ – Mr.Wizard May 12 '17 at 6:33
  • $\begingroup$ "This question regarding the requirement for the use of Directive indicates that aside from a some small edge cases, it is mostly preferred as easier to read" -- You're saying what exactly is mostly preferred? Using Directive? Using a sequence of directives instead? Something else? $\endgroup$ – jjc385 May 12 '17 at 8:04
  • $\begingroup$ Works: Graphics[{GrayLevel[Log[2]], Disk[]}] Does not work: Graphics[{Style[Disk[], GrayLevel[Log[2]]]}] $\endgroup$ – Szabolcs May 12 '17 at 9:28
  • $\begingroup$ Works: style = Directive[Red, Thick]; Graphics[{style, Circle[]}]. Does not work: style = {Red, Thick}; Graphics[{style, Circle[]}]. Directive was introduced to address this programming problem, imo. -- Note your Style version is converted to Graphics[{{RGBColor[1, 0, 0], Thickness[Large], Circle[{0, 0}]}, {RGBColor[0, 0, 1], Rectangle[{0, 0}]}}], i.e., your first form, in the output in the front end. $\endgroup$ – Michael E2 May 14 '17 at 2:54
  • $\begingroup$ Something you can do with Style: Graphics[Style[{Circle[], Rectangle[], Point[{0.5, 0.5}]}, "foo"]], where "foo" is a style you have defined in your stylesheet. It can automatically style most graphics primitives in a consistent way (other than Circle[] for some reason -- an oversight???). $\endgroup$ – Michael E2 May 14 '17 at 3:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.