0

Consider the function

fTest[b_] := Plot[a Sin[x] + b ,{x, -4, 4}], 

where a is some real constant.

I can set some standard value for a by Options[fTest] = {a -> 4} and define fTest as:

fTest[b_, OptionsPattern[]] := Plot[OptionValue[a] Sin[x] + b, {x, -4, 4}]

Now fTest should also accept the usual options of Plot.

I tried

Options[fTest] = {a -> 4, Options[Plot]} // Flatten;
fTest[b_, opts : OptionsPattern[]] := 
  Plot[OptionValue[a] Sin[x] + b, {x, -4, 4}, opts]

which failed.

A workaround could be achieved via:

fTest[b_, a_: 4, opts : OptionsPattern[]] := 
  Plot[a*Sin[x] + b, {x, -4, 4}, opts]

This works, but I would favor a solution where the option is of the form a -> value

So how can I combine the options?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
ctrl
  • 123
  • 1
  • 5
  • 4
    You need to remove the option a->4 from the options that are fed to Plot. The easiest way is to use Evaluate @ FilterRules[{opts}, Plot] instead of opts in your Plot. – Carl Woll Apr 03 '18 at 18:16
  • @CarlWoll, thank you for your answer, it works. I will accept it, if you are willing to post it as an answer. – ctrl Apr 04 '18 at 10:13

0 Answers0