I've read this page but I'm still kind of confused about something. Here's a simple example function, where it simply plots a function. If you give it the ImageSize option, it should use that, but otherwise it should use the default one of ImageSize->100:
Options[PlotWrapperFn] = {ImageSize -> 100};
PlotWrapperFn[pTitle_, opts : OptionsPattern[]] := (
Print@Style[pTitle, 24];
Return@Plot[x^2, {x, 0, 5},
Evaluate[FilterRules[{opts}, Options[Plot]]]];
)
As I understand it, the line with FilterRules[{opts}, Options[Plot]]
is essentially taking the options out of opts
that are valid options for Plot[]
and using them with Plot[]
.
However, it became clear to me that this doesn't work, because if you call the function without the ImageSize
option, opts
is empty and thus it doesn't add any options to Plot
.
If I instead make it filter the options for PlotWrapperFn
by doing Evaluate[FilterRules[{Options[PlotWrapperFn]}, Options[Plot]]]
, it will obviously always choose the default option of ImageSize->100
, defeating the purpose of making it an option.
I realized I can do it by using Evaluate[FilterRules[{opts,Options[PlotWrapperFn]}, Options[Plot]]]
, because that searches both opts and Options[PlotWrapperFn] and finds an ImageSize rule in each, but uses the first one it sees, from opts.
Is that the correct way? It seems weird to pass the option twice and rely on it using the first one.
Another way I figured out is to do:
rules = (# -> OptionValue[#]) & /@ (Options[PlotWrapperFn][[All, 1]]);
Return@Plot[x^2, {x, 0, 5}, Evaluate@rules];
Which basically manually lists the current value of the option, not what was passed to the function (like opts
) or in the default list (like Options[PlotWrapperFn]
).
I'm sure I'm missing something simple. How do people do this?
Plot
options, or will there be other options that do not belong toPlot
? $\endgroup$Plot
within your function. I tried to handle that in my answer below. $\endgroup$