Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I have written a function Fun as below:

Fun[x_Symbol, xrange_, imageSize_, axeslabel_] :=
 Plot[x^2, {x, xrange[[1]], xrange[[2]]}, 
   ImageSize -> imageSize, 
   AxesLabel -> axeslabel]

Fun[x, {-1, 2}, 400, {"x", "y"}]

enter image description here

However, I'd like to rewrite it to use OptionsPattern

RewriteFun[x_Symbol, OptionsPattern[Plot]] := 
  Plot[x^2, {x, -1, 2}, 
    ImageSize -> OptionValue[ImageSize], 
    AxesLabel -> OptionValue[AxesLabel]]

RewriteFun[x, ImageSize -> 400, AxesLabel -> {"x", "y"}]

enter image description here

I have two problems.

  • I don't know how to deal with an option like {x, xmin, xmax}.

  • I don't how to get the kind of syntax coloring the varible x has in Plot[x^2, {x, -1, 2}].

enter image description here

So my question is: How do I revise the RewriteFun to solve my problems?

share|improve this question

marked as duplicate by Michael E2, Ajasja, m_goldberg, Yves Klett, RunnyKine Aug 1 '14 at 15:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

An argument of the form {x, xmin, xmax} isn't a option for Plot. It is Plot's required second argument. You must add a required second argument to your function to handle it. – m_goldberg Aug 1 '14 at 12:50
(1) See 1446 and 54956. (2) Presumably you mean the argument {x, xmin, xmax}? – Michael E2 Aug 1 '14 at 13:28

2 Answers 2

up vote 4 down vote accepted

Here's my interpretation of the question:

SetAttributes[RewriteFun, HoldAll];
Options[RewriteFun] = Options[Plot];
   RewriteFun] = {"ArgumentsPattern" -> {{_, _, _}, 
     OptionsPattern[Plot]}, "LocalVariables" -> {"Plot", {1}}};
RewriteFun[dom : {x_, _, _}, OptionsPattern[]] := 
 Plot[x^2, dom, ImageSize -> OptionValue[ImageSize], 
  AxesLabel -> OptionValue[AxesLabel]]

RewriteFun[{t, -2, 1}, ImageSize -> 150, AxesLabel -> {"x", "y"}]

Mathematica graphics

share|improve this answer

You could do the following:

Options[fun] = {ImageSize -> 400, AxesLabel -> {"x", "y"}, xmin -> -1, xmax -> 2};

fun[x_Symbol, OptionsPattern[]] :=
 Plot[x^2, {x, OptionValue@xmin, OptionValue@xmax}, 
  ImageSize -> OptionValue@ImageSize, 
  AxesLabel -> OptionValue@AxesLabel]

fun[z, xmin -> -2, AxesLabel -> {"z", "y"}]

enter image description here

You could even turn the symbol (or the whole function) into an option:

Options[fun] = {ImageSize -> 400, AxesLabel -> {"x", "y"}, symbol -> x, xmin -> -1, xmax -> 2};

fun[OptionsPattern[]] :=
 Block[{sym = OptionValue@symbol},
  Plot[sym^2, {sym, OptionValue@xmin, OptionValue@xmax},
   ImageSize -> OptionValue@ImageSize,
   AxesLabel -> OptionValue@AxesLabel]]
share|improve this answer
@Öskå Quotation: "I don't know how to deal with an option like {x, xmin, xmax}." – eldo Aug 1 '14 at 13:15

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