# how to include more options for built-in options?

I can a self-defined function for plotting. There is a built-in option AspectRatio. I want to include a few more choices for convenience, eg.

ratio1 = (yrange[]-yrange[])/(xrange[]-xrange[])
ratio2 = 2*(yrange[]-yrange[])/(xrange[]-xrange[])
ratio3 = (yrange[]-yrange[])/(xrange[]-xrange[])/3


other than the standard choices of Automatic and Full. How to do that so that AspectRatio->ratio1 becomes a valid input?

• in your case it will be 1, 2 and 1/3 right? – Kuba Sep 10 '16 at 9:36
• Can you be more specific on what exactly is not working? AspectRatio->ratio1 works just fine for me, given some arbitrary ranges. – Andreas Sep 10 '16 at 9:50
• the three ratios are just examples (for course I can just type 1, 2 1/3). I mean I have already calculated a few more complicated ratios that will depend on a few parameters of the input function. I don't want to explicitly calculate them every time I call my self-defined plotting function, and so I want to include them in the optional arguments 'AspectRatio' so that when I just type in 'ratio1' it will automatically calculates that particular ratio inside the function itself. – Physicist Sep 10 '16 at 10:02
• In short, I want to put the above codes inside the function, and I can just choose which ratio I want by specifying the argument of 'AspectRatio', but currently it only accepts the built-in Automatic' and Full, something I want to extend. – Physicist Sep 10 '16 at 10:03

There are probably a lot of ways to do this, I will just demonstrate a simple, straight forward ways with which you might experiment.

First we set up a global variable that assigns aspect ratio functions:

$aspectRatioFunctions = Function[ func, Switch[ func, "ratio2", Function[ {p1,p2}, 2 p1/p2 ], "ratio3", Function[ {p1,p2}, p1 / (3 p2) ], _ , Function[ {p1, p2}, p1/p2 ] (* ratio1 as Default *) ] ];  We can now use this in a simple graphics function or whatever custom function you want to build upon: Options[ myGraphics ] = { "AspectRatioFunction" -> "ratio1" }; myGraphics[ graphics_List, aspectPars_ , opts : OptionsPattern[{ myGraphics, Graphics }] ] := With[ { aspectRatio = Apply[$aspectRatioFunctions@OptionValue["AspectRatioFunction"],
aspectPars
]
},
Graphics[
graphics,
Evaluate@FilterRules[ {opts}, Options[Graphics] ], (* more robust *)
AspectRatio -> aspectRatio
]
]

gr = { Red, Line[{{1, 1}, {2, 2}}] }

myGraphics[ gr, { 1, 1 }, AspectRatioFunction -> "ratio2", Imagesize -> Tiny ] • This is merely a toy example to demonstrate the principle, of course. – gwr Sep 10 '16 at 13:02
• Note, that using FilterRules makes this more robust; the Evaluate is not needed here but may be for functions like Plot with HoldAll` and the like. – gwr Sep 10 '16 at 13:19
• @Physicist Would your maybe care to accept? Thank you. ;-) – gwr Sep 20 '16 at 9:13