It commonly happens that one wants to define some function foo which in turn calls some other function (e.g., Plot) that can take a vast variety of optional arguments, and one would like to arrange things so that the caller of foo can pass optional parameters intended for the second function (e.g., Plot) among the arguments to foo. The latter is expected to simply pass those arguments right through. The pattern for doing this, according to the documentation, is something like:

foo[x_, y_, z_, opts:OptionsPattern[]] := 
   Plot[..., Evaluate[FilterRules[{opts}, Options[Plot]]]]

But now suppose that foo in fact makes two calls to Plot (for example), and that one wants to provide the caller the opportunity to pass separate sets of optional parameters for the two calls to Plot. (In general, these two parameter sets would differ.)

How can one retool foo so that it can accept 2 sets of optional parameters? Also, how would one actually invoke the function thus defined, with two different, non-empty sets of optional parameters?

  • 3
    $\begingroup$ Why not use "Plot1" -> {options to first plot} and "Plot2" -> {options to second plot} as the options for foo? Then you can just do Plot[..., OptionValue["Plot1"]] (use FilterRules if you must... I leave it out for functions that only I'll be using). $\endgroup$
    – rm -rf
    Jul 21, 2013 at 3:16
  • $\begingroup$ @rm-rf youtube.com/watch?v=iZs-e_x0PGM $\endgroup$ Jul 21, 2013 at 5:01
  • $\begingroup$ kjo, I added an answer with an example. If this does not satisfy please tell me why and I shall try again. I left out the FilterRules part to keep things simple and because I'm not sure how you would intend to use that with separate option sections anyway. $\endgroup$
    – Mr.Wizard
    Jul 21, 2013 at 6:30
  • $\begingroup$ Seems to be a duplicate of this. At least,my answer there describes what I would personally do in this case. $\endgroup$ Jul 21, 2013 at 9:41
  • $\begingroup$ @rm-rf: I'm having a hard time understanding your suggestion; if you wouldn't mind posting a more explicit example I'd appreciate it. $\endgroup$
    – kjo
    Jul 21, 2013 at 17:08

1 Answer 1


I propose using two separate OptionsPattern[] expressions, and placing the options for the second call in a list following the options for the first call (or anything else).

foo[x_, y_, z_, opts1 : OptionsPattern[], PatternSequence[] | {opts2 : OptionsPattern[]}] :=
    Plot[Sin[x a], {a, y, z}, opts1],
    Plot[Cos[x a], {a, y, z}, opts2]

foo[3.7, 0, 2 Pi]
foo[3.7, 0, 2 Pi, Ticks -> False]
foo[3.7, 0, 2 Pi, {PlotStyle -> Red}]
foo[3.7, 0, 2 Pi, Ticks -> False, {PlotStyle -> Red}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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