Questions tagged [optional-arguments]

Tag for questions about using optional arguments in Mathematica functions.

Filter by
Sorted by
Tagged with
1 vote
1 answer
63 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 <...
user avatar
  • 928
1 vote
1 answer
117 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 ...
user avatar
  • 306
2 votes
1 answer
48 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] ...
user avatar
  • 51
1 vote
0 answers
67 views

Skip middle optional arguments

Is there a way to skip some middle optional arguments? For example for this function: ...
user avatar
  • 51
0 votes
0 answers
44 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: ...
user avatar
7 votes
0 answers
68 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: ...
user avatar
  • 23.3k
2 votes
0 answers
61 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 ...
user avatar
0 votes
1 answer
77 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 ...
user avatar
  • 1,787
5 votes
1 answer
64 views

Type safety and scoping in OptionsPattern arguments

I am new to OptionsPattern. Consider ...
user avatar
  • 902
1 vote
1 answer
61 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 ...
user avatar
  • 699
2 votes
1 answer
61 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 ...
user avatar
  • 699
1 vote
1 answer
40 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 ...
user avatar
  • 699
1 vote
0 answers
64 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 ...
user avatar
  • 1,787
2 votes
2 answers
80 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: ...
user avatar
0 votes
1 answer
75 views

passing splice instructions

Consider some passing splice instructions using start,stop,step pattern ...
user avatar
  • 902
0 votes
1 answer
59 views

Keep irrelevant variables outside function arguments

I want to define several functions of variables x,y say ...
user avatar
1 vote
1 answer
74 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 ...
user avatar
  • 1,787
3 votes
2 answers
72 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: ...
user avatar
1 vote
0 answers
51 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 ...
user avatar
  • 699
9 votes
1 answer
132 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 <...
user avatar
2 votes
1 answer
90 views

Issue using optional argument before options [duplicate]

I have a problem using optional arguments before an OptionsPattern: ...
user avatar
  • 30.3k
10 votes
2 answers
752 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 ...
user avatar
  • 3,286
5 votes
1 answer
88 views

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

I don't understand the output from: ...
user avatar
  • 1,094
0 votes
0 answers
58 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)? ...
user avatar
  • 1,535
2 votes
1 answer
62 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
43 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 ...
user avatar
7 votes
0 answers
178 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 ...
user avatar
  • 111
8 votes
2 answers
422 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 ...
user avatar
  • 108k
1 vote
1 answer
1k 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 ...
user avatar
  • 121
2 votes
0 answers
46 views

Scoping of OptionValue for inner functions in closures

Assume a function f is a closure over an inner function g. Both f and ...
user avatar
  • 8,259
4 votes
2 answers
106 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 ...
user avatar
  • 39.3k
3 votes
1 answer
55 views

Using other functions' replacement rules

I have a module that uses some default options for NDSolve like so ...
user avatar
  • 559
1 vote
1 answer
43 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 ...
user avatar
  • 3,980
11 votes
1 answer
133 views

Except @ OptionsPattern[] affects OptionValue

The background This example works as expected, small function with a special case rule: ...
user avatar
  • 132k
-1 votes
3 answers
110 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: ...
user avatar
  • 397
5 votes
1 answer
159 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 ...
user avatar
1 vote
0 answers
63 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, ...
user avatar
  • 2,791
4 votes
2 answers
154 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: ...
user avatar
5 votes
0 answers
57 views

Optional in MatchQ [duplicate]

Why isn't the first one True in: ...
user avatar
  • 132k
1 vote
0 answers
91 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 ...
user avatar
  • 1,724
10 votes
1 answer
1k views

define a function with optional arguments

I have defined a function as follows: ...
user avatar
  • 4,714
2 votes
2 answers
389 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]]
user avatar
  • 7,399
3 votes
2 answers
78 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 ...
user avatar
12 votes
4 answers
630 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 ...
user avatar
  • 15.6k
6 votes
0 answers
106 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 ...
user avatar
  • 30.3k
2 votes
0 answers
106 views

How to create a function with option that shows dropdownlist like ( for example ) Manipulate with ControlPlacement? [duplicate]

Consider the Mathematica function Manipulate where you assign the placement of the controls with the option parameter ...
user avatar
14 votes
1 answer
258 views

Pattern matching with List and Optional

Bug introduced in 8.0.4 or earlier and persists through 11.1.1 Thank you for bringing this to our attention. I have filed a report with our developers. Why does this ...
user avatar
  • 5,559
2 votes
1 answer
84 views

Is this the correct method for using optional arguments?

I've read this page but I'm still kind of confused about something. Here's a simple example function, where it simply plots a function. If you give it the ImageSize option, it should use that, but ...
user avatar
  • 3,002
8 votes
2 answers
108 views

How to write a `HeldOptional` variant of `Optional` that does not evaluate its second argument?

How to specify optional arguments that take functional values made me wonder: Can we come up with a variant of Optional that allows to do the following: ...
user avatar
  • 5,559
9 votes
3 answers
3k views

Functions with both optional arguments and options [duplicate]

I have the following codes: ...
user avatar
  • 927