# How to assign options to a variable?

I want to redefine a function similar to Plot like this:

newPlot[func_, range_, prop___] := Plot[func, range, prop, GridLines -> something,
FrameStyle -> something]


To be more flexible, I'd like to write it more in the following way:

moreProp = GridLines -> something, FrameStyle -> something
newPlot[func_, range_, prop___] := Plot[func, range, prop, moreProp]


Unfortunately it's not working. What's the necessary syntax than for moreProp

I just edited the question for more clarification (I hope).

• SetOptions ... ? – BlacKow Apr 8 '16 at 19:36
• Not really what I was looking for – Kay Apr 8 '16 at 19:45
• Use moreProp = {GridLines -> something, FrameStyle -> something} – bbgodfrey Apr 8 '16 at 19:58
• I tried that, it's not working this way I guess... – Kay Apr 8 '16 at 20:03
• @Kay moreprops = {Frame -> True, GridLines -> Automatic}; newPlot[func_, range_, prop___] := With[{mp = moreprops}, Plot[func, range, prop, mp]]; newPlot[Sin[x], {x, 0, 10}, GridLinesStyle -> Dashed] still works – BlacKow Apr 8 '16 at 20:07

This is one aspect of a classic problem: Plot is HoldAll. So, moreProp is not being evaluated. I think the most effective way to work around this is to use With (cf. this question) to inject the values into Plot, e.g.

newPlot[func_, range_, prop___] :=
With[{mp = moreProp}, Plot[func, range, prop, mp]]


Then, it works:

Alternatively, you can use

newPlot2[func_, range_, prop___] := Plot[func, range, prop, #] & @ moreProp


which performs a similar injection.

As was pointed out in the comments, Plot accepts lists of options, so the above methods work. But, if Plot did not do this, you could use one of the following two methods:

newPlot3[func_, range_, prop__] :=
With[{mp = Sequence@@Flatten[{prop, moreProp}]},
Plot[func, range, mp]
]


or

newPlot4[func_, range_, prop__] :=
Plot[func, range, prop, ##]& @@ Flatten[{moreProp}]

• Seems is not working, when moreProp consists of more than one property like GridLines -> something, FrameStyle -> something, etc. – Kay Apr 8 '16 at 19:52
• @Kay newPlot[func_, range_, prop___] := With[{mp = {Frame -> True, GridLines -> Automatic}}, Plot[func, range, prop, mp]]; newPlot[Sin[x], {x, 0, 10}, GridLinesStyle -> Dashed] works for me. – BlacKow Apr 8 '16 at 19:55
• @BlacKow Indeed, you are right. I forgot to cramp the options... However, I'm not happy with the fact, that I have to assign dozens of Options to every redefiniton. Therefore I was hoping to do it like to assign something to a variable... I just edited my question to be a bit more clear hopefully – Kay Apr 8 '16 at 20:01
• @Kay you can reset what moreProp is set to. Try moreProp = {GridLines -> Automatic, FrameStyle -> Blue, Frame -> {{True, False}, {False, True}}} and then moreProp = {PlotStyle -> Red} and run newPlot[Sin[x], {x, 0, 10}] after each change. You'll see that it updates the values. – rcollyer Apr 8 '16 at 20:10
• Luckily Plot[] accepts a list of options; for some other functions, using Sequence[] might be necessary. – J. M.'s discontentment Apr 8 '16 at 20:28