3
$\begingroup$

When looking at the internals of graphics expressions we often see that some options like PlotRange, Frame, Ticks, PlotRangePadding etc. are repeated several times in the list, and sometimes even have different values. For example:

gr = LogLogPlot[x^2 + x^3, {x, 1, 100}];
Cases[Options[gr], _[Ticks, _]]
Cases[Options[gr], _[DisplayFunction, _]]
{Ticks -> {Charting`ScaledTicks["Log", {Log, Exp}, "Nice", 
    WorkingPrecision -> MachinePrecision, RotateLabel -> 0], 
   Charting`ScaledTicks["Log", {Log, Exp}, "Nice", 
    WorkingPrecision -> MachinePrecision, RotateLabel -> 0]}, 
 Ticks -> {Automatic, Automatic}}

{DisplayFunction -> Identity, DisplayFunction -> Identity, 
 DisplayFunction -> Identity, DisplayFunction -> Identity, 
 DisplayFunction -> Identity, DisplayFunction -> Identity, 
 DisplayFunction :> Identity}

In the above example, the option Ticks appears two times with different values, and the option DisplayFunction appears 7 (!) times with identical values.

When inspecting the low-level structure of Mathematica graphics, such duplicated options litter the source code and often make it difficult to find out which value of duplicated option actually "works".

Is there a safe way to remove all the trash from the list of options, leaving only those option specifications which actually work? I know that we can obtain actual value for every particular option by specifying it explicitly in the second argument of Options, but I wish to leave only the options which are already present in the expression.

A super-requirement would be the ability to remove non-actual options: for example, if we have set Axes -> False, then options like AxesOrigin, AxesStyle, Ticks do nothing and we can safely remove them from the expression source code for the purposes of readability.


UPDATE

I just found that Options has a bug: it extracts options from the first argument of Graphics too, for example:

gr = Graphics[{Ticks -> Automatic, AspectRatio -> 1}, 
  {Axes -> True}, {{Ticks -> {Automatic, None}}}, Ticks -> Automatic, AspectRatio -> .2]
Options[gr, {Ticks, AspectRatio}]

graphics

{Ticks -> Automatic, AspectRatio -> 1}

As one can see, Options takes the requested option values from the first argument of Graphics - where they are ignored by the renderer!

$\endgroup$

1 Answer 1

1
$\begingroup$

For the easiest part my first attempt is:

Clear[simplifyOptions]
simplifyOptions[(h : Graphics | Graphics3D)[first_, rest__]] := 
 h[first, DeleteDuplicatesBy[First[#, #] &]@Flatten[{rest}]]

It completely avoids both the bug in Options and its check-up for the correctness of the options list (I do not wish to fix wrong options, since it is intended for visual inspection of the source code).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.