This should be quick to an expert, but I'm relatively new at defining functions with options. Here is a schematic of what I've tried, I'll explain after showing the code:
MyPlotFunction[params_, optionalparameter_List:{1,2,3}, opts:OptionsPattern[]]:=
Plot [ stuff, {x,0,1}, Evaluate@FilterRules[{opts},Options@Plot]];
Options[MyPlotFunction] = { PlotRange->{-5,5}, Frame->True, ... other plot options};
There are four slight subtleties:
But what I have above doesn't work. The default options I set are ignored, yet they appear in the ??MyPlotFunction
information for my function. I'll give examples if you guys can't spot the error yet.
Edit: Examples that doesn't work:
SimplePlot[t_,opts:OptionsPattern[{PlotRange->{-4,4},Frame->True}]]:=
Plot[2x+t,{x,0,1},opts];
Fails, the default option is ignored.
SimplePlot[t_,opts:OptionPattern[]]:=
Plot[2x+t],{x,0,1},opts];
Options[SimplePlot] = {PlotRange->{-4,4},Frame->True};
Fails, the default option is ignored.
SimplePlot[t_,opts__:{PlotRange->{-4,4},Frame->True}]:=
Plot[2x+t,{x,0,1},opts];
Default options work with a bare call, but if one of these options or any other plot option is overridden the remaining defaults are lost.
OptionsPattern[]
only catches the options that are passed in, so you need to explicitly include any non-default option settings, say by using something like:
FilterRules[{opts, Options[MyPlotFunction]}, Options@Plot]
Here's a simple example:
Options[MyPlotFunction] = {PlotRange -> {-5, 5}, Frame -> True};
MyPlotFunction[params_, optionalparameter_List: {1, 2, 3},
opts : OptionsPattern[MyPlotFunction]] :=
Plot[optionalparameter, {x, 0, 1},
Evaluate@FilterRules[{opts, Options[MyPlotFunction]}, Options@Plot]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With