Questions tagged [optional-arguments]

Tag for questions about using optional arguments in Mathematica functions.

Filter by
Sorted by
Tagged with
1 vote
0 answers
33 views

Separate definition of custom options and passed options in function definition

Why do these codes do not work? ...
azerbajdzan's user avatar
  • 9,653
0 votes
0 answers
43 views

How to pass on optional arguments, but with different default values?

Main question I want to define a function g that calls a function f. g has optional ...
Learn2Burn's user avatar
4 votes
1 answer
67 views

How to specify the type/Head of the optional named arguments to a function?

I am trying to improve my own dsolver API. Currently it takes optional arguments. Is it possible to specify that an optional argument should say be String only? Or List Only? Or Integer only? and so ...
Nasser's user avatar
  • 137k
4 votes
1 answer
94 views

OptionValue not inserting correct option

I am using OptionsPattern and OptionValue to define some switches for my custom function. Here is a minimal example: ...
shanedrum's user avatar
  • 557
1 vote
1 answer
84 views

Ensure that Options for built-in symbols are the original defaults

I often create symbols (no args) or functions (with args) in which common options (and styles) are set in body and unique opts within args for managing multiple calls to built-in functions. these spit ...
Jules Manson's user avatar
  • 2,397
2 votes
1 answer
83 views

Propper way of getting rid of FilterRules statements when passing options?

When passing options from a function f to a function g in the body of f one can use the <...
Gert's user avatar
  • 1,470
1 vote
1 answer
127 views

Condition (/;) not working correctly on a function with optional arguments and options

I have read this post: How to avoid collision between optional arguments and options, and use Mr.Wizard's method to solve this problem. But when I add conditions to optional arguments, it seems that ...
李子涵's user avatar
  • 316
2 votes
1 answer
73 views

Force only selected optional arguments

Is there a way to force only selected values for function argument? For example for this function: g[x_, y_, a_: ("b" || "c")] := Print[x, y, a] ...
Tigran's user avatar
  • 51
1 vote
0 answers
78 views

Skip middle optional arguments

Is there a way to skip some middle optional arguments? For example for this function: ...
Tigran's user avatar
  • 51
0 votes
0 answers
73 views

How to have Mathematica showing a dropdown list after entering an option and the -> in a function argument [duplicate]

When I enter Plot[Sin[x], {x, 0, 2 Pi}] Mathematica returns, as expected, the expected Sinus wave of one period. Plot has many, many options, when I enter: ...
nilo de roock's user avatar
8 votes
0 answers
73 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
Lukas Lang's user avatar
  • 33.4k
2 votes
0 answers
71 views

How to produce an error message for each argument?

I want to print a helpful message for every wrong input. How do you compactly implement a function with custom pattern constraints, default values and error messages for each argument separately? For ...
Mauricio Fernández's user avatar
0 votes
1 answer
83 views

Why won't Sequence work when wrapping a list of arguments for deletion with Remove?

I wrote a quick and dirty little function freshStart which does a variety of things meant to give me a nearly fresh Kernel without having to ...
Jules Manson's user avatar
  • 2,397
5 votes
1 answer
94 views

Type safety and scoping in OptionsPattern arguments

I am new to OptionsPattern. Consider ...
lineage's user avatar
  • 1,114
1 vote
1 answer
75 views

How to manually create options that can be entered as a sequence or list?

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 ...
WillG's user avatar
  • 854
2 votes
1 answer
101 views

Simplest way to check if optional parameter is omitted?

Suppose I have a function f[x_,y___]:= (code) and I want my code to check whether the parameter y is omitted. What's the easiest ...
WillG's user avatar
  • 854
1 vote
1 answer
43 views

How to create custom NIntegrate with special option behavior?

I'm trying to create a custom version of NIntegrate, called MyNIntegrate, with custom option behavior. In particular, I'd like ...
WillG's user avatar
  • 854
1 vote
0 answers
115 views

How to change behavior of Echo or turn it off without removing it from code?

I wish to use Echo for output of intermediate results if an OptionValue I call ...
Jules Manson's user avatar
  • 2,397
2 votes
2 answers
127 views

Help understanding options on ContourPlot3D

I am creating some figures showing the intersection between two surfaces. I managed to get most of what I want with the following code: ...
Charlie H.'s user avatar
0 votes
1 answer
83 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
lineage's user avatar
  • 1,114
0 votes
1 answer
76 views

Keep irrelevant variables outside function arguments

I want to define several functions of variables x,y say ...
Weather Report's user avatar
1 vote
1 answer
131 views

Default Options not working in user-defined function for SymbolName

Abstract I have a function objectName[symbol] that returns the SymbolName as both a standalone function or when nested inside ...
Jules Manson's user avatar
  • 2,397
3 votes
2 answers
85 views

Setting nested optional argument with a default when unpacking from a given Head

I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example: ...
Joe Bentley's user avatar
1 vote
0 answers
68 views

String arguments break optional argument default values? [duplicate]

Following a tip in this post, I use string parameters to label my functions. For example, instead of f[x]:=x^2, I use ...
WillG's user avatar
  • 854
9 votes
1 answer
139 views

Why Optional in Set is not held by the HoldFirst attribute?

When evaluatingf[x_:x0] := x, x0 will be evaluated, regardless of the HoldAll attribute of <...
bcegkmqs23's user avatar
2 votes
1 answer
94 views

Issue using optional argument before options [duplicate]

I have a problem using optional arguments before an OptionsPattern: ...
M.R.'s user avatar
  • 31.2k
10 votes
2 answers
762 views

Define function that behaves almost identically to Mathematica function

Often I like to define my own functions that are almost exactly the same as Mathematica defined functions, apart from a few tweaks. See this question for example. I want to define them properly so ...
Tom's user avatar
  • 3,366
5 votes
1 answer
89 views

Why is { } interpreted as an option in my function? [closed]

I don't understand the output from: ...
MeMyselfI's user avatar
  • 1,116
0 votes
0 answers
60 views

Optional argument in Set Function [duplicate]

Optional arguments can be defined in SetDelayed (:=) Functions (see f1 below and here and here). However, how can one accomplish this in Set (=) Functions (see f2 and f3 below)? ...
LBogaardt's user avatar
  • 1,585
2 votes
1 answer
96 views

OptionValue and OptionPattern not Changing Values

I am trying to produce a function with a few sets of arguments, one of which can accept options. However it doesn't seem to be responding to the changes that I accept. ...
user avatar
0 votes
0 answers
56 views

Method option of function MatrixRank

By running ??MatrixRank, I see that it has an optional argument named Method. However, this argument is not in the ...
Liam Baker's user avatar
7 votes
0 answers
244 views

OptionsPattern[] Vs. opt___ [closed]

Since version 6 I've been dutifully using OptionsPattern[] instead of the original opts___ for passing options to functions. I ...
M. Decker's user avatar
  • 111
8 votes
2 answers
427 views

Are Mathematica commands always supposed to check that Method -> name is valid?

When calling a Mathematica command which accepts a Method -> name option, I always thought that if the name given is not a ...
Nasser's user avatar
  • 137k
1 vote
1 answer
2k views

Set absolute size of Axis or frame for a plot

I have to publish some work and it requires to meet specific criteria for the image size and plot size. 1) the Area of the figure must be fix 2) the width of the x-axis also must be fix for any plot ...
Jared Lo's user avatar
  • 121
2 votes
0 answers
53 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
Sascha's user avatar
  • 8,399
4 votes
2 answers
115 views

How to prevent an Optional parameter from taking an OptionsPattern parameter without multiple function definitions? [duplicate]

I have function the needs to enable some Optional parameters and an OptionsPattern. The issue I am facing is that the options ...
Edmund's user avatar
  • 41.7k
3 votes
1 answer
56 views

Using other functions' replacement rules

I have a module that uses some default options for NDSolve like so ...
bjorne's user avatar
  • 569
1 vote
1 answer
48 views

Why doesn't OptionValue cooperate with Map? [duplicate]

Consider the following definition: Options[f] = {a -> 1, b -> 2} f[x_,OptionsPattern[]] := {x,OptionValue/@{a,b}} I thought that doing something like ...
user42582's user avatar
  • 4,185
11 votes
1 answer
151 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
Kuba's user avatar
  • 136k
-1 votes
3 answers
125 views

OptionsPattern Confusion

I want to use the whole options pattern as the argument for HTTPRequest's "Query" parameter, but I'm not sure how to use them. When I run the following code: ...
Joe Blow's user avatar
  • 417
5 votes
1 answer
163 views

What is the difference between the second argument of ToString and its option FormatType?

Is there any real difference between ToString[expr, form] and ToString[expr, FormatType→form]? In all my examples, they seem to ...
QuantumDot's user avatar
  • 19.5k
1 vote
0 answers
67 views

What is the best way to pass on options? [duplicate]

Say I have two functions which take the same set of options, e.g. Options[f1] = {a -> 1}; Options[f2] = {a -> 1}; Now, ...
JEM_Mosig's user avatar
  • 2,953
4 votes
2 answers
158 views

How to define an option or an argument whose values are confined

I want to define a function whose arguments or options can only be selected in a range. For example: ...
zchenkui's user avatar
5 votes
0 answers
59 views

Optional in MatchQ [duplicate]

Why isn't the first one True in: ...
Kuba's user avatar
  • 136k
1 vote
0 answers
106 views

Automatic is not always the default option

Contrary to what one might expect, specifying an option to be Automatic sometimes changes the output. For example, by default ...
tparker's user avatar
  • 1,766
10 votes
1 answer
1k views

define a function with optional arguments

I have defined a function as follows: ...
Dimitris's user avatar
  • 4,784
2 votes
2 answers
408 views

Anonymous Function And Optional Arguments [duplicate]

Is there a way to get an optional arguments to work in an anonymous function. Function[{Optional@a},If[a,True,False]]
William's user avatar
  • 7,565
3 votes
2 answers
91 views

How to define a function with an optional value related to the arguments?

It's known that we can easily define a function with optional value which is not related with other arguments, but my question is how can I define a function with an optional value which is really ...
solo joker's user avatar
12 votes
4 answers
639 views

Optional argument that can be completely omitted?

I am writing a simple function return similar words. Clear[similarWords] similarWords[string_]:=Nearest[WordList[],string] I want to add another argument ...
matheorem's user avatar
  • 16.9k
6 votes
0 answers
112 views

Examples of the single dot form of Optional?

I've known of the single dot form of Optional for a while, but never had occasion to actually use it: Are there any good examples of use cases for it other than in relation to ...
M.R.'s user avatar
  • 31.2k