2
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Are you only going to use Plot options, or will there be other options that do not belong to Plot? $\endgroup$ – Jason B. Aug 24 '16 at 17:33
  • $\begingroup$ @JasonB, let's definitely assume that there will be other options that don't apply to Plot[]. $\endgroup$ – YungHummmma Aug 24 '16 at 17:44
  • $\begingroup$ Okay, I think this may be a duplicate of this question, but maybe not. Here there is the complication that you want to define default options for Plot within your function. I tried to handle that in my answer below. $\endgroup$ – Jason B. Aug 24 '16 at 17:52
2
$\begingroup$

What I like to take advantage of is that you can pass the same option multiple times, and only the first OptionValue is used. So you can include the default in your plot command, but precede it by opts:

Options[PlotWrapperFn2] = {"TitleSize" -> 24};
PlotWrapperFn2[pTitle_, 
  opts : OptionsPattern[{Plot, PlotWrapperFn2}]] := (Print@
   Style[pTitle, OptionValue["TitleSize"]];
  Return@Plot[x^2, {x, 0, 5}, 
    Evaluate[FilterRules[{opts}, Options[Plot]]], ImageSize -> 100];)

So if opts contains a ImageSize option, it will override the default

PlotWrapperFn2["hello"]
PlotWrapperFn2["hello", ImageSize -> 400, "TitleSize" -> 13]

Mathematica graphics

I also used the formatting for the options pattern described in Leonid's answer here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.