Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for a way to create a variable that I can use to hold all of the graphics directives for, say, a plot command. For example, I have 8 plots with variations on the following:

Plot[{Im[eppar[400*10^12, t, 0.1]],
      Im[eppar[400*10^12, t, 0.2]], 
      Im[eppar[400*10^12, t, 0.5]], 
      Im[eppar[400*10^12, t, 0.8]]
     }, {t, tlow, thigh}, (**directives**)]

There's a fairly long list of directives I'm using for the plots, and I'd like to avoid having to modify 8 long, complicated expressions every time I want to change anything. Does anybody know of a solution to this? It would make my code much cleaner.

share|improve this question
You can use With like in here. – Kuba Jul 30 '13 at 17:33
Can you give us a clearer idea what you have in mind when you say "avoid having to modify 8 long, complicated expressions every time I want to change anything"? And I think we would like to see the "8 long, complicated expressions" and definition or eppar so we can work with your code. – m_goldberg Jul 30 '13 at 17:59
I think you may simply be looking for Directive. – C. E. Jul 30 '13 at 18:22
Closely related: (20718) (setSpec in my second answer is directly applicable.) – Mr.Wizard Jul 30 '13 at 20:12
@Mr.Wizard Guys, this is not a duplicate of that question (somehow my comment was deleted). That question was about how options are passed to HoldAll functions, but the question asked here is not about that technicality. It is much closer (may be a dupe) to this one. I hope I won't have to repeat this comment a third time (i.e this one won't get deleted). – Leonid Shifrin Jul 30 '13 at 21:35
up vote 6 down vote accepted

Here is another way using Evaluate:

ops1 = {Filling -> {1 -> {2}}, PlotStyle -> {Red, Green}, Frame -> True};

Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10}
      , Evaluate@ops1
      , PlotLabel -> Style["Using Evaluate", 20]

enter image description here

share|improve this answer

On Leonid's valued opinion that the Close was inappropriate I have reopened this question.

By my interpretation this does what is requested:

SetAttributes[setSpec, HoldAllComplete]

setSpec[s_Symbol, spec__] := s /: h_[pre__, s, post___] := h[pre, spec, post]

The usage is:

 Filling -> {1 -> {2}},
 PlotStyle -> {Red, Green},
 Frame -> True


Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10}, ops1]

enter image description here

Because Plot handles both Plot[. . ., option1, option2, . . .] and Plot[. . ., {option1, option2, . . .}] you can use either form in setSpec.

setSpec can also be configured to apply to only certain heads, e.g.:

SetAttributes[setSpecFor, HoldAllComplete]

setSpecFor[head_Symbol, s_Symbol, spec__] := s /: head[pre__, s, post___] := head[pre, spec, post]

And usage would be: setSpecFor[Plot, ops1, (* options *)]

This allows you to use the same name (Symbol) for separate options for different plot types.

share|improve this answer

Your Answer


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.