MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: default options, OptionsPattern[], and FilterRules[]


don't you want to filter the Options of Plot and look what options of
myplot can be given to Plot too? And then you give to Plot either the
standart-value or your option to myplot or the value which is given in 
the function-call.

Options[myplot] = {PlotRange -> {{-5, 5}, {-5, 5}}};
myplot[F_, OptionsPattern[]] :=
  opts = Evaluate[(First@# -> OptionValue[First@#]) & /@ 
     FilterRules[Options[Plot], First /@ Options[myplot]]];
  Plot[F, {x, -1, 1}, Evaluate[Sequence @@ opts]]

when you know that all your options can be given to Plot, that you can

Options[myplot] = {PlotRange -> {{-5, 5}, {-5, 5}}, 
   PlotStyle -> Red};
myplot[F_, opts___Rule] :=
  stdopts = 
   Select[Options[myplot], ! MemberQ[First /@ {opts}, First@#] &];
  Print[Join[{opts}, Options[myplot]]];
  Plot[F, {x, -1, 1}, 
   Evaluate[Sequence @@ Join[{opts}, Options[myplot]]]]


On Wed, 2010-01-13 at 06:00 -0500, Roman wrote:
> dear group,
> I am trying to set up a plotting function which has a different
> default behavior than the standard one. Here's a simple example:
> Options[myplot] = {PlotRange -> {-5, 5}};
> myplot[F_, opts : OptionsPattern[]] :=
>  Plot[F, {x, -1, 1}, Evaluate[FilterRules[{opts}, Options[Plot]]]]
> What I would like to achieve with this is that when I don't specify
> the PlotRange option, as in
>    myplot[Sin[x]]
> then I would get a plot with a default plot range of {-5,5}.
> Unfortunately this does not work, since the call to FilterRules[] does
> not access the default rules defined in Options[myplot], but only
> those options explicitly specified in opts (i.e. none at all in this
> call example).
> Since this seems like such a standard use of the Options framework of
> Mathematica, I would expect there to be pre-defined functionality for
> what I am trying to achieve. Of course I can manually merge the
> contents of the opts variable with those of Options[myplot] and
> eliminate duplicates, but I'd like to avoid such hacks if possible.
> Do you know a simple solution for this problem?
> Thanks!
> Roman.

  • Prev by Date: Re: disable canceling
  • Next by Date: Re: default options, OptionsPattern[], and FilterRules[]
  • Previous by thread: default options, OptionsPattern[], and FilterRules[]
  • Next by thread: Re: default options, OptionsPattern[], and FilterRules[]