# How can I define a function taking two distinguishable sets of options?

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?

-
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). –  rm -rf Jul 21 '13 at 3:16
–  belisarius Jul 21 '13 at 5:01
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. –  Mr.Wizard Jul 21 '13 at 6:30
Seems to be a duplicate of this. At least,my answer there describes what I would personally do in this case. –  Leonid Shifrin Jul 21 '13 at 9:41
@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. –  kjo Jul 21 '13 at 17:08

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[]}] :=
Grid[{{
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}]


-