13
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ 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. $\endgroup$ May 4, 2013 at 13:22
  • $\begingroup$ 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. $\endgroup$
    – Daniel W
    May 4, 2013 at 13:43
  • 1
    $\begingroup$ See also here: chat.stackexchange.com/transcript/message/7226732#7226732 $\endgroup$
    – Szabolcs
    May 4, 2013 at 16:40
  • 4
    $\begingroup$ 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. $\endgroup$
    – murray
    May 4, 2013 at 17:57

2 Answers 2

12
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ Thanks for your observation concerning my answer. I have deleted it because I can't find a example where Directive[] is unavoidable. $\endgroup$
    – andre314
    May 4, 2013 at 19:19
  • $\begingroup$ I suppose the gist is that one can certainly choose not to use Directive[], but one is left poorer and less expressive for it. $\endgroup$ May 5, 2013 at 4:50
3
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ That conjecture should be boldfaced and on top! $\endgroup$ Oct 8, 2013 at 10:47
  • 2
    $\begingroup$ Directive is still unnecessary for this case, VertexLabelStyle -> {{FontFamily -> "Helvetica", Bold, 14}} will work. $\endgroup$
    – xzczd
    Oct 8, 2013 at 11:14
  • $\begingroup$ @IstvánZachar. Why do you think it needs to be made so much more visible? Because it is valuable or because it is egregious? $\endgroup$
    – m_goldberg
    Oct 8, 2013 at 20:04
  • $\begingroup$ @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). $\endgroup$
    – m_goldberg
    Oct 8, 2013 at 20:15
  • $\begingroup$ @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. $\endgroup$ Oct 8, 2013 at 22:16

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.