Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
3  
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
    
@rm-rf youtube.com/watch?v=iZs-e_x0PGM –  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
show 1 more comment

1 Answer

up vote 3 down vote accepted

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}]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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