Tag Info

Hot answers tagged

64

The main change since that time seems to be that the modern way of using options is associated with OptionsPattern[] - OptionValue commands. A typical way of defining a function would be: Options[f] = { FirstOption -> 1, SecondOption ->2 } f[x_,y_,opts:OptionsPattern[]]:= Print[{x,y,OptionValue[FirstOption],OptionValue[SecondOption]}] ...


59

The documentation is wrong. It should have been fixed, but AbsoluteOptions does not work with ViewMatrix (on all platforms). M- introduced interactive 3D graphics since V6, and after that getting values through AbsoluteOptions (which is an old function) becomes very tricky since the Kernel (who evaluates the option) cannot fully know what is happening on ...


45

One thing you can do is look for options which appear in a function's Options but do not have a ::usage message. Of course, some of the results actually are documented in the help, they just don't have a usage message. Here's a function to do it: undoc[x_Symbol]:=Select[Options[x],!StringQ@MessageName[Evaluate@First@#,"usage"]&]; undoc[_] = {}; (* e.g. ...


34

It got a bit out of hand, but here's a way to construct a ViewMatrix pair from the triple ViewVector, ViewAngle, and ViewVertical. The left figure is the Graphics3D object using ViewVector, ViewAngle, and ViewVertical and the right is the one using ViewMatrix. If you rotate the left figure or scale it (by dragging the figure while keeping Alt depressed), the ...


34

For this purpose, I wrote a small Symbol Information Palette. This palette let's you quickly look up usages, options and attributes of symbols and was tested on Mac OSX and Linux. Installation The source code is hosted on my GitHub site but to preview or install the palette you only have to evaluate this: Get["http://goo.gl/QPywk"] The link is just ...


33

Here is a practical example from a StackOverflow question. I hope that it gives a good overview of the basic methods. Question What would be the best way to make a function out of the below code ? It would take a dataList as well as some graphical options (such as colors) as arguments and return a customized tabular representation as shown ...


28

I use this function getList[name_String] := Module[{options, idx}, options = Names[name <> "`*"]; options = ToExpression /@ options; options = {#, Options[#]} & /@ options; idx = Range[Length[options]]; options = {#[[1]], TableForm[#[[2]]]} & /@ options; options = Insert[options[[#]], #, 1] & /@ idx; options = ...


27

1. Discovery may be aided by Trace and related tools. I recommend WReach's traceView functions. 2. Simple observation and experimentation. Simple does not imply easy. As an example, to find the answer to "Can the frame border on a BarChart Legend be removed?", I used: BarChart[{{1, 2, 3}}, ChartLegends -> {"A", "B", "C"}] // Trace // InputForm ...


27

I think you can actually see (most of) what Mathematica is doing by using Trace[..., TraceInternal -> True]. For example, Select[Flatten[ Trace[NDSolve[y'[x] == x && y[0] == 0, y, {x, 0, 6}], TraceInternal -> True]], ! FreeQ[#, Method | NDSolve`MethodData] &] shows the DE was evaluated using NDSolve`LSODA and Newton's method. (I ...


26

One way would be to use ColorConvert to convert the RGB or Hue values to gray scale. Here's an example: Plot[{Sin[x], Cos[x], Exp[-x^2], Sinc[π x]}, {x, 0, π}] /. x : _RGBColor | _Hue | _CMYKColor :> ColorConvert[x, "Grayscale"] For 2D plots that accept a ColorFunction, you can simply use GrayLevel to get the plot in grayscale as: DensityPlot[ ...


26

I doubt you can find a chart for all options, but take a look at this: For this and other insights two courses by Yu-Sung are a must (there are notebooks and videos there): Graphics Language Quick Start Visualization: Advanced 3D Graphics The above chart is from the 1st one. The one @Kuba links in the comment to your question is from the 2nd - I ...


25

Mathematica has to be able to tell that the default arguments can't be rules. So, for some special cases, you could do Options[f] = {"g" -> Identity}; f[x_, y_Integer: 2, z_Integer: 3, OptionsPattern[]]:= OptionValue["g"][x + y + z] Testing: f[1, 2, 3, "g" -> (#^2 &)] 36 f[1] 6 f[1, "g" -> (#^2 &)] 36


25

You could plot the curve twice, with two different styles: Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[Thickness[0.03], White], Black}] Changing the background to gray: Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[Thickness[0.03], White], Black}, Background -> Gray]


25

Yes, it is possible: The idea is to look at the underlying cell expressions in the documentation for those string property tables. As I said already in my comment above, basically we have two different situations here: the trend since Mathematica V6 that many options are not symbols any more but rather strings. function arguments, that are given ...


22

I find SetSystemOptions["PackedArrayOptions" -> {"UnpackMessage" -> True}] to be useful: it emits a message when a packed array is unpacked. This may happen automatically, sometimes slowing down things greatly. This is useful in situations like this or this. One way one would find out that such an option exists is "PackedArrayOptions" /. ...


22

Ah well... this is not robust, but probably of educational value and useful as a starting point for other postprocessing needs on Graphics or Graphics3D expressions: p = Plot[Sin[x], {x, 0, 1}] col = Cases[p, _Hue, Infinity][[1]]; Show[p /. col -> Red] Update: As pointed out by @matheorem, Version 10 switched from Hue to RGBColor, so the ...


21

Here is another approach, based on Filling option : Plot[{Sin[x] - 0.02, Sin[x], Sin[x] + 0.02}, {x, 0, 2 Pi}, PlotStyle -> {Gray, Black, Gray}, Filling -> {1 -> {{3}, Yellow}}] One problem may appear here, namely if a given function has a big absolute value of the derivative, then the strip becomes too thin. We can avoid this by ...


20

Edit: better answer below. I voted for Rojo's answer. If for some reason you cannot be that specific about your arguments you might use the converse: nr = Except[_?OptionQ]; f[x_, y : nr : 2, z : nr : 3, OptionsPattern[]] := OptionValue["g"][x + y + z] If for some further reason you need the optional arguments to be rules themselves, you could filter ...


20

A guess My guess is that you have just run into the details of OptionValue implementation, which are also responsible for its "magical" behavior. OptionValue has to somehow know which function it is in, and tracing the execution of f4[] shows that apparently the following expansion is happening before any evaluation is attempted for the r.h.s.: ...


19

Here is another method that I learned through reading Inside the Mathematica Pattern Matcher: Options[f] = {"g" -> Identity}; f[x_, Shortest[y_: 2, 1], Shortest[z_: 3, 2], OptionsPattern[] ] := OptionValue["g"][x + y + z] From the documentation for Shortest: Shortest[p, pri] is given priority pri to be the shortest sequence. Matches for ...


18

I already answered this question on StackOverflow but since old questions can no longer be migrated without undue trouble I shall reproduce my answer here. There are two different categories of graphical objects in a Plot output. The plotted lines of the functions (Sin[x], Cos[x]) and their styles are "hard coded" into Line objects, which Graphics can ...


17

John Fultz posted an answer which had an undocumented option. CellPrint[{ Cell["Click to open the section", "Section", System`WholeCellGroupOpener -> True], TextCell["Some text"]}] This produces a section cell that if you click anywhere on it will open and close the whole section.


17

Let me first answer your second question, since I can only guess about the main question: I also observed that the syntax colouring (version 10, windows 7) suggests that Trace can be used with only two arguments. It's really just the coloring that goes wrong and has nothing to do with functionality. You can see that it is not even related to ...


16

A bit late-to-the-party post, and complementary to the other solutions. Several answers addressed the question quite well IMO. I had my shot on a similar one here, with a solution similar to the one by @Mr.Wizard. But now I just want to stress one subtle point missed by other answers: using OptionQ will leak evaluation for functions which are HoldAll and ...


16

All of the polynomial functions, have an option Modulus which allows you to specify an integer field, like $\mathbb{Z}_5$. In particular, Factor works on your example polynomial Factor[x^2+4, Modulus -> 5] (* (1 + x) (4 + x) *) Additionally, IrreduciblePolynomialQ works to determine irreducibility of $x^2+2 $, as follows IrreduciblePolynomialQ[x^2 + ...


16

The main points of this answer are that,first, it seems rather difficult to have a fully universal mechanism for option-validation, and second, such a mechanism is not currently available in Mathematica on the language level (meaning automation of complete option-checking, including both the option's name and value). In the particular case in question, ...


16

AbsoluteOptions is known as very buggy function and the bug in determining the true PlotRange has very long history... You could try my Ticks-based workaround for getting the complete PlotRange (with PlotRangePadding added): completePlotRange[plot:(_Graphics|_Graphics3D|_Graph)] := Last@ Last@Reap[ Rasterize[ Show[plot, Axes -> True, ...


15

Short answer: You supply a pure function to an option when you want to override the built-in options. In this case, "Diamond" and 0.2 resolve to certain functions or are used as values in certain functions internally which is then used for the respective option. The short names are merely a convenient way for you to remember and enter the option. Longer ...


15

A minor tweak to Markus' method, you may find utility in CapForm: Table[ Plot[{Sin[x], Sin[x]}, {x, 0, 2 Pi}, PlotStyle -> {Directive[AbsoluteThickness[15], CapForm[c], White], Directive[AbsoluteThickness[7], Blue]}, ImageSize -> 400, PlotRangePadding -> 0.2, Frame -> True, Prolog -> ...


15

Extraction of Export options To extract the options for specific image export formats automatically, I really don't know an easy way. What you can do is to use Trace in combination with ExportString to investigate what Mathematica is doing. To make the next thing work you have to call e.g. ExportString["test", "GIF"]; for every format at least once: ...



Only top voted, non community-wiki answers of a minimum length are eligible