Lets say as a (dumb) minimal example you had the following:

Options[my$plot] = {"SetColor" -> (*...don't know how to do this...*)};

my$plot[x_,OptionsPattern[]]:=Table[{OptionValue["SetColor"][[i]], Sphere[x[[i]]]},

and I want to have a default set of colors if not provided, lets say:

(*default of "SetColors" something like:*) Table[Hue[i/Length@x],{i,0,Length@x}]

How can you achieve such behaviour?

  • $\begingroup$ Make the option into a function that depends on the arg or on the index, e.g. "SetColor"->ColorData[97] and then MapIndexed[{OptionValue["SetColor"][#2[[1]]], Sphere[#]}&, x] $\endgroup$
    – b3m2a1
    Apr 13 '20 at 4:37

Normally one uses Automatic as the default option setting, then computes the actual value within the function.

Options[my$plot] = {"SetColor" -> Automatic};

my$plot[x_, OptionsPattern[]] := 
 With[{setcolor = 
           Automatic :> Table[Hue[i/Length@x], {i, 0, Length@x}]]
  Table[{setcolor[[i]], Sphere[x[[i]]]}, {i, 1, Length@x}]
  • $\begingroup$ Did not know about Automatic. Very helpful. $\endgroup$
    – Pana
    Apr 13 '20 at 4:53
  • $\begingroup$ @Pana Automatic doesn't really do anything here. It could be any other expression in principle. It is merely a convention that when the default option value is computed automatically based on something else, we use Automatic. $\endgroup$
    – Szabolcs
    Apr 13 '20 at 7:50
  • $\begingroup$ Thank you for the clarification. I realised this later when I used this in my actual notebook, and was thinking using specifically Automatic must be a convention for clarity among different users. $\endgroup$
    – Pana
    Apr 14 '20 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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