12

How do you combine OptionsPatterns?

For example, I would like to have a function that can take both options that I specify, and options for all graphs:

Options[f]={"a"->b} + OptionsPattern[LogPlot]
f[...,OptionsPattern[]] := LogPlot[..., ImageSize->OptionsValue[ImageSize]]

A related question, how do I remove options from an OptionsPattern, and pass on the remainder to another function.

For example:

f[...,OptionsPattern[LogPlot]] := LogPlot[..., (* pass on all the options EXCEPT Frame *)]

Thanks for the help.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Andrew Spott
  • 1,581
  • 15
  • 22

2 Answers2

15

This information is given in the tutorial Setting Up Functions with Optional Arguments. Just catch the options given to your function in a variable and use FilterRules

odeplot[de_, y_, {x_, x0_, x1_}, opts : OptionsPattern[]] := 
 Module[{sol},
  sol = NDSolve[de, y, {x, x0, x1}, 
    FilterRules[{opts}, Options[NDSolve]]];
  If[Head[sol] === NDSolve,
   $Failed,
   Plot[Evaluate[y /. sol], {x, x0, x1}, 
    Evaluate[FilterRules[{opts}, Options[Plot]]]]
   ]
  ]
halirutan
  • 112,764
  • 7
  • 263
  • 474
12

For your "related question", of passing all options to LogPlot other than Frame, just set the option to the value you want, and place this first in the function call. The first occurrence of the option is used, so any use of it in opts is redundant and ignored.

f[data_, opts:OptionsPattern[]]:= LogPlot[data, Frame->False, 
  Evaluate[FilterRules[{opts},Options[LogPlot]]] ]
Verbeia
  • 34,233
  • 9
  • 109
  • 224