# Making a function with Options, where the option depends on the function variables

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]]]},
{i,1,Length@x}]


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?

• 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] – 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 =
Replace[
OptionValue["SetColor"],
Automatic :> Table[Hue[i/Length@x], {i, 0, Length@x}]]
},
Table[{setcolor[[i]], Sphere[x[[i]]]}, {i, 1, Length@x}]
]

• Did not know about Automatic. Very helpful. – Pana Apr 13 '20 at 4:53
• @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. – Szabolcs Apr 13 '20 at 7:50
• 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. – Pana Apr 14 '20 at 8:46