Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sometimes I see code samples in the document or in this site using Directive, but I haven't yet find a case that Directive is necessary. Usually it can be replaced just by List (except for Graphics and Graphics3D, in which Directive can be replaced by Sequence) and their visual appearance won't change at all, though their FullForm structures will be different:

(* These samples are all modified from the examples in the document. *)

p[1] = Plot[Sin[x], {x, 0, 10}, PlotStyle -> Directive[Orange, Thick, Dashed]];
p[2] = Plot[Sin[x], {x, 0, 10}, PlotStyle -> {Orange, Thick, Dashed}];
p[2] === p[1]
p[3] = 
  Plot[2 Sin[x], {x, 0, 10}, 
    Frame -> True, FrameLabel -> {x, 2 Sin[x]}, 
    LabelStyle -> Directive[Medium, Italic]];
p[4] = 
  Plot[2 Sin[x], {x, 0, 10}, 
    Frame -> True, FrameLabel -> {x, 2 Sin[x]} 
    LabelStyle -> {Medium, Italic}];
p[4] === p[3]
p[5] = 
  ParametricPlot3D[{Cos[φ] Sin[θ], Sin[φ] Sin[θ], Cos[θ]}, {φ, 0, 2 Pi}, {θ, 0, Pi}, 
    MeshShading -> {{Directive[Red, Specularity[White, 10]], 
      Directive[Green, Opacity[0.5]]}, {Blue, Yellow}}];
p[6] = 
  ParametricPlot3D[{Cos[φ] Sin[θ], Sin[φ] Sin[θ], Cos[θ]}, {φ, 0, 2 Pi}, {θ, 0, Pi}, 
    MeshShading -> {{{Red, Specularity[White, 10]}, 
      {Green, Opacity[0.5]}}, {Blue, Yellow}}];
p[6] === p[5]
p[7] = 
  Graphics[{Purple, Arrowheads[Large], Arrow[{{4, 3/2}, {0, 3/2}, {0, 0}}]}];
p[8] = 
  Graphics[{Directive[Purple, Arrowheads[Large]], Arrow[{{4, 3/2}, {0, 3/2}, {0, 0}}]}];
p[8] === p[7]
p[9] = 
  Graphics3D[
    Directive[Black, Thick, Dashed], 
     Line[{{-2, 0, 2}, {2, 0, 2}, {0, 0, 4}, {-2, 0, 2}}]}];
p[10] = 
  Graphics3D[{Black, Thick, Dashed, 
    Line[{{-2, 0, 2}, {2, 0, 2}, {0, 0, 4}, {-2, 0, 2}}]}];
p[10] === p[9]
Grid[Partition[p[#] & /@ Range@10, 2]]

False
False
False
False
False

enter image description here

Directive was added in version 6, so its existence can't be a issue left over by history, so what's the significance of Directive? Is there a case where Directive is required? Does Directive give any advantage over List or Sequence that I haven't noticed?

share|improve this question
2  
For Plot[], Plot[{Sin[x], Cos[x]}, {x, 0, 10}, PlotStyle -> Directive[Orange, Thick, Dashed]] and Plot[{Sin[x], Cos[x]}, {x, 0, 10}, PlotStyle -> {{Orange, Thick, Dashed}}] do yield the same effect, but Plot[{Sin[x], Cos[x]}, {x, 0, 10}, PlotStyle -> {Orange, Thick, Dashed}] does something a bit different. –  J. M. May 4 '13 at 13:22
    
I was just putting the same counterexample together when your comment appeared. The doc center says Directive is new in 6, but I seem to recall it in much older versions. –  Daniel W May 4 '13 at 13:43
1  
3  
Even in ordinary Plot where, say, you plot two functions, using something like PlotStyle -> {Directive[Red,Dashed],Directive[Green,Thick]} can be easier to read at a glance than the corresponding version PlotStyle -> {{Red,Dashed},{Green,Thick}}with nested lists. –  murray May 4 '13 at 17:57

2 Answers 2

up vote 10 down vote accepted

Directive denotes a single compound graphics directive, which idea cannot otherwise be expressed, although the same effect can often be attained through multiple paths. But then again, Mathematica offers multiple paths for many computations.

In a document one can define styles such as

myStyle = Directive[Thick, Blue, Opacity[0.5]]

and use them equally in Plot, Graphics etc. without having to apply Sequence or some other workaround. In other words, if you think of the directives as a single style, you can write what you mean.

Another thing is that Directive[..] does not have the head List, which can be an advantage in postprocessing graphics.

share|improve this answer
    
Thanks for your observation concerning my answer. I have deleted it because I can't find a example where Directive[] is unavoidable. –  andre May 4 '13 at 19:19
    
I suppose the gist is that one can certainly choose not to use Directive[], but one is left poorer and less expressive for it. –  J. M. May 5 '13 at 4:50

I have run into a situation where using Directive works, but using a list fails. The following code, using Directive, works as expected.

Graph[{DirectedEdge[a, b], DirectedEdge[b, c], DirectedEdge[a, c]},
  VertexLabels -> "Name",
  VertexLabelStyle -> Directive[FontFamily -> "Helvetica", Bold, 14],
  PlotRangePadding -> Scaled[.1],
  GraphLayout -> "SpringEmbedding"]

graph.png

However, when the directive is changed to a list, failure ensues.

Graph[{DirectedEdge[a, b], DirectedEdge[b, c], DirectedEdge[a, c]},
  VertexLabels -> "Name",
  VertexLabelStyle -> {FontFamily -> "Helvetica", Bold, 14},
  PlotRangePadding -> Scaled[.1],
  GraphLayout -> "SpringEmbedding"]

Graph[{a [DirectedEdge] b, b [DirectedEdge] c, a [DirectedEdge] c}, VertexLabels -> "Name", VertexLabelStyle -> {FontFamily -> "Helvetica", Bold, 14}, PlotRangePadding -> Scaled[0.1], GraphLayout -> "SpringEmbedding"]

On the other hand, replacing FontFamily -> "Helvetica" with "SR"restores success.

Graph[{DirectedEdge[a, b], DirectedEdge[b, c], DirectedEdge[a, c]},
  VertexLabels -> "Name",
  VertexLabelStyle -> {"SR", Bold, 14},
  PlotRangePadding -> Scaled[.1],
  GraphLayout -> "SpringEmbedding"]

Conjecture: A list can not be substituted for Directive when one or more of its elements is a rule.

share|improve this answer
    
That conjecture should be boldfaced and on top! –  István Zachar Oct 8 '13 at 10:47
    
Directive is still unnecessary for this case, VertexLabelStyle -> {{FontFamily -> "Helvetica", Bold, 14}} will work. –  xzczd Oct 8 '13 at 11:14
    
@IstvánZachar. Why do you think it needs to be made so much more visible? Because it is valuable or because it is egregious? –  m_goldberg Oct 8 '13 at 20:04
    
@xzczd. Good point, although I personally find the {{...}} form very strange and rather ugly (because I can think of no logical reason why double-listing should make a difference). –  m_goldberg Oct 8 '13 at 20:15
    
@m_gold Well, I found it really valuable, but then xzczd made the idol fall down. I really thought that you nailed it. Still, the conjecture is valid for one level of List. –  István Zachar Oct 8 '13 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.