This is kind of a follow-up to this question.
I want to be able to define a function that takes an option, and then, inside the function, retrieve and test the unevaluated value of the option. For example, I'd like to be able to define the following (this is obviously a silly example, but nonetheless): a function that accepts an expression and an option, Power
, that should be allowed the values one
and two
. It should plot the appropriate power (first or second) of the given expression. So one way of doing this is
SetAttributes[plotIt, HoldAll];
Options[plotIt] = {Power -> one};
plotIt[f_, OptionsPattern[]] := Module[{},
Switch[OptionValue[Power],
one, Plot[f, {x, 3, 5}],
two, Plot[(f)^2, {x, 3, 5}]
]
];
which works just fine, except that what is being tested is the evaluated one
and two
. Thus if those are defined variables in the current environment that happen to have the same value, invocations of this function will always use the first branch of the switch statement.
I've tried various ways to extract the name of the option value, without success. And even if I do so, I'm not sure how to refer to it in the various branches of the switch statement.
Thanks.
Options[plotIt] = {Power -> Left};
is meant to beOptions[plotIt] = {Power -> one};
. Anyway I would just use strings and make my life immeasurably simpler. So instead ofone
you can use"one"
. A lot of the built in options have started using strings. An example. $\endgroup$ – Ajasja Aug 1 '12 at 14:05Power
already has a meaning in Mathematica. It is generally best to choose symbol names which are not in use by the system. $\endgroup$ – Simon Woods Aug 1 '12 at 14:30