is a pattern object that represents a collection of options given as rules, where the values of the options can be accessed using OptionValue.
takes default option values from Options[f].
OptionsPattern[{opt1val1,opt2val2,…}]
uses an explicit list of default option values.
OptionsPattern
is a pattern object that represents a collection of options given as rules, where the values of the options can be accessed using OptionValue.
takes default option values from Options[f].
OptionsPattern[{opt1val1,opt2val2,…}]
uses an explicit list of default option values.
Details
- OptionsPattern matches any sequence or nested list of rules, specified with -> or :>, whose left-hand sides are symbols or strings.
- In OptionsPattern[{spec1,spec2,…}] the speci can be either heads fi or explicit rules opti->vali. Each head is treated as the list of rules obtained from Options[fi].
- OptionsPattern[] uses the default options of the nearest enclosing function.
- OptionsPattern[{}] includes no default options.
Examples
open all close allBasic Examples (3)
Define default option values for the function f:
Options[f] = {a -> a0, b -> b0};Define f, allowing options to be given:
f[x_, OptionsPattern[]] := {x, OptionValue[a]}Use f with an explicit option setting:
f[7, a -> uuu]Use f with options taken to have their default values:
f[7]Define default option values for this assignment without explicitly setting up Options[f]:
f[x_, OptionsPattern[{a -> a0, b -> b0}]] := {x, OptionValue[a]}f[7]Take default option values from Plot:
f[x_, OptionsPattern[Plot]] := {OptionValue[AspectRatio], OptionValue[ImageSize]}Use the defaults from Plot:
f[7]Override one of the defaults from Plot:
f[7, ImageSize -> 20]Scope (4)
Use the options of ArcLength, but override the default value of Method:
f[OptionsPattern[{Method -> "NIntegrate", ArcLength}]] := {OptionValue[Assumptions], OptionValue["Method"]}Explicit option settings are honored:
f[Assumptions -> c > 0, Method -> "Integrate"]f uses the same default as ArcLength for Assumptions, but not Method:
f[]OptionValue[ArcLength, {Assumptions, Method}]Take options from both Plot and Plot3D:
f[OptionsPattern[{Plot, Plot3D}]] := {OptionValue[PlotRange], OptionValue[BoxRatios]}f[PlotRange -> All]For common options, the default is inherited from Plot:
f[]Options[Plot, PlotRange]Options[Plot3D, PlotRange]The function g inherits defaults for common options from Plot3D:
g[OptionsPattern[{Plot3D, Plot}]] := {OptionValue[PlotRange], OptionValue[BoxRatios]}g[]Use OptionsPattern with no default options:
f[x_, OptionsPattern[{}]] := OptionValue[a]Explicit uses of the option are honored:
f[1, a -> x, b -> y]The option name is returned if not explicitly present in the options:
f[7, {b -> y}]OptionsPattern can be used in normal pattern-matching functions like ReplaceAll:
f[7, {a -> x, b -> y}] /. f[_, OptionsPattern[{}]] :> OptionValue[a]Applications (2)
Define the default option values for a function f:
Options[f] = {"Square" -> False}Implement f using a helper function fCore, but still inherit defaults from f:
f[args___] := fCore[f, args]
fCore[f, x_, OptionsPattern[f]] := If[OptionValue["Square"], x ^ 2, x, "BadOptionValue"]The default option value of f is honored:
f[2]An explicit option settings takes precedence:
f[2, "Square" -> True]Define two functions with options:
Options[f] = {a -> 1, b -> 2};
Options[g] = {b -> 3, c -> 4};Define a function h that uses both sets of options, giving preference to the defaults of g:
h[OptionsPattern[{g, f}]] := OptionValue[{a, b, c}]h[]h[a -> 10, b -> 7]Properties & Relations (3)
OptionsPattern matches any sequence of rules or lists of rules:
MatchQ[f[a -> 1, b :> 3], f[OptionsPattern[]]]MatchQ[f[{a -> 1, b :> 3}], f[OptionsPattern[]]]MatchQ[f[{a -> 1, {b :> 3, {c -> 7}}}], f[OptionsPattern[]]]An empty sequence matches OptionsPattern:
MatchQ[f[], f[OptionsPattern[]]]An empty list matches OptionsPattern:
MatchQ[f[{}], f[OptionsPattern[]]]Possible Issues (2)
OptionsPattern will match any set of options:
Options[f] = {a -> 1};
f[opts : OptionsPattern[f]] := {opts, OptionValue[a]}This may cause messages if invalid options are passed:
f[a -> 1, b -> 2]FilterRules can be used to only use options valid for f:
f[opts : OptionsPattern[f]] := {opts, OptionValue[f, FilterRules[Options[f], {opts}], a]}f[a -> 1, b -> 2]Any nesting of empty lists will match OptionsPattern:
MatchQ[f[{}], f[OptionsPattern[]]]MatchQ[f[{{}}], f[OptionsPattern[]]]Related Guides
Related Workflows
- Define a Function with Options
History
Text
Wolfram Research (2007), OptionsPattern, Wolfram Language function, https://reference.wolfram.com/language/ref/OptionsPattern.html.
CMS
Wolfram Language. 2007. "OptionsPattern." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/OptionsPattern.html.
APA
Wolfram Language. (2007). OptionsPattern. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/OptionsPattern.html
BibTeX
@misc{reference.wolfram_2026_optionspattern, author="Wolfram Research", title="{OptionsPattern}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/OptionsPattern.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_optionspattern, organization={Wolfram Research}, title={OptionsPattern}, year={2007}, url={https://reference.wolfram.com/language/ref/OptionsPattern.html}, note=[Accessed: 15-June-2026]}