Skip to main content
3 of 4
deleted 10 characters in body; edited title
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Options for a function I define that can be passed to functions I call in the definition

I know, this is very close to Functions with Options and/or Function with custom Options and modified Options for built-in Symbols, but I don't get it.

My function looks like this one:

myPlot[data1_, data2_, opts : OptionsPattern[]] :=
 Module[{Rescaled, dataRescaled},
  Rescaled[x_, commonZero_: False] :=
   If[commonZero,
    (* for an common zero line *)
    x - 5,
    (* normal *)
    x
    ];
  dataRescaled = Rescaled[data2];
  ListLinePlot[{data1, dataRescaled},
   FilterRules[{opts}, Options[ListLinePlot]],
   PlotLegends -> {"should not", "be the legend"}]
  ]

And this is how I want to call it:

myPlot[
 Sin[Range[51, 100, .1]],
 Cos[Range[51, 100, .1]] + 5,
 PlotLegends -> Automatic
 ]

enter image description here

But how can I add an option "CommonZero->True"?

EDIT:

I edited my function this way

myPlot[data1_, data2_, 
  opts : OptionsPattern[{ListLinePlot, Rescaled}]] :=
 Module[{Rescaled, dataRescaled},
  Options[Rescaled] = {CommonZero -> False};
  Rescaled[x_, OptionsPattern[]] :=
   If[OptionValue[CommonZero],
    (* for an common zero line *)
    x - 5,
    (* normal *)
    x
    ];
  dataRescaled = 
   Rescaled[data2, CommonZero -> OptionValue[CommonZero]];
  
  ListLinePlot[{data1, dataRescaled},
   FilterRules[{opts}, Options[ListLinePlot]],
   PlotLegends -> {"should not", "be the legend"}]
  ]

But I doubt this is the right or a nice way to do.

Phab
  • 1.6k
  • 9
  • 15