Mathematica Asked on January 31, 2021
I am trying to implement custom options in my code manually, rather than using the built-in Options
tools. So I have functions that look like f[x_,y_,opts___]
where opts
is intended to be a (possibly empty) list or sequence of rules.
I’d like to be able to enter opts
as a sequence or list, but I’m having a very hard time getting my code to cover both cases. How can I program my functions so that opts
is converted to a list of rules, regardless of whether they are fed into the function as a list or sequence?
OptionsPattern[]
does allow entering opts
as a sequence or list:
Examples:
ClearAll[f]
f[x_, opts : OptionsPattern[]] := Plot[Sin[x t], {t, 0, Pi}, opts]
Row[{f[5, PlotStyle -> Red, Frame -> True, ImageSize -> 300],
f[5, {PlotStyle -> Red, Frame -> True, ImageSize -> 300}],
f[5, PlotStyle -> Red, {Frame -> True}, {{{ImageSize -> 300}}}]},
Spacer[10]]
ClearAll[f2]
f2[x_, opts : OptionsPattern[]] := Framed @ Style[x, opts]
Row[{f2["ABC",
FontColor -> Red, FontSize -> 62, FontFamily -> "French Script MT"],
f2["ABC",
{FontColor -> Red, FontSize -> 62, FontFamily -> "French Script MT"}],
f2["ABC",
FontColor -> Red, {{{FontSize -> 62, {{{{ FontFamily -> "French Script MT"}}}}}}}]},
Spacer[10]]
Answered by kglr on January 31, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP