3
$\begingroup$

Slightly extending rcollyer's handy helper (originally "BlockOptions") to TemporarilySet system function options via an operator form:

SetAttributes[TemporarilySet, HoldAll];

TemporarilySet[f : {_Symbol, ___?OptionQ | {___?OptionQ}}, body_] :=TemporarilySet[{f}, body]

TemporarilySet[f : {{_Symbol, ___?OptionQ | {___?OptionQ}} ...}, body_] :=
  With[{fcns = f[[All, 1]]}, Internal`InheritedBlock[fcns, SetOptions @@@ f;body]];

TemporarilySet[stgs_] := With[{evalStgs = stgs},
                           Function[{body}, TemporarilySet[evalStgs, body], HoldAll]];

we can set up an environment

  $PlotTheme = "Minimal";
  graphs = {Histogram, BarChart, SmoothHistogram, PieChart, ListLinePlot};
  graphEnv = {#, ImageSize -> Tiny} & /@ graphs;

in which certain graphs are Tiny

 assoc = <|"a" -> 7, b -> 8, "c" -> 9|>;
 Query[graphs]@assoc // TemporarilySet@graphEnv

enter image description here

but we might not want to be restricted to these particular graphs and since they all eventually use Graphics shouldn't we be able to set this at this lower level?

graphEnv = {Graphics, ImageSize -> Tiny};
Query[graphs]@assoc // TemporarilySet@graphEnv

enter image description here

Apparently not?

$\endgroup$
  • $\begingroup$ The other environment setter - $PlotTheme suggests expanding TemporarilySet to include global variables so their effects don't linger ... and/or have $ImageSize as a new global variably to get WL's function count that little higher. $\endgroup$ – Ronald Monson Jul 16 '15 at 7:03
  • $\begingroup$ What is the question here? $\endgroup$ – Mr.Wizard Jul 16 '15 at 7:07
  • $\begingroup$ Why isn't the second graphic tiny as well? i.e. setting the Graphics option is not grabbing. $\endgroup$ – Ronald Monson Jul 16 '15 at 7:09
  • $\begingroup$ I am not sure what you are looking for in "why." This is simply the inheritance/precedence model used. Are you looking for possible reasoning behind that design decision? Or is your interest more pragmatic? $\endgroup$ – Mr.Wizard Jul 16 '15 at 7:16
  • $\begingroup$ Pragmatic, e.g. graphEnv = {{Plot, ImageSize -> Tiny}, {Graphics, ImageSize -> Tiny}} doesn't grab either - I want to set ImageSize->Tiny once, somewhere ... $\endgroup$ – Ronald Monson Jul 16 '15 at 7:21
3
$\begingroup$

The options (including internal defaults) for Plot etc. override the options set for Graphics. For example:

SetOptions[Graphics, ImageSize -> Tiny];

Plot[Sinc[x], {x, 0, 5}]

enter image description here

If you wish to use the Graphics setting try Inherited:

Plot[Sinc[x], {x, 0, 5}, ImageSize -> Inherited]

enter image description here


I want to set ImageSize->Tiny once, somewhere ...

If you want to affect the default image size for multiple Graphics sources you can set it as the Box Options level:

SetOptions[EvaluationNotebook[], GraphicsBoxOptions -> {ImageSize -> Tiny}]

Now both Plot and Graphics in this Notebook render at the Tiny size by default:

Graphics[Circle[]]
Plot[Sinc[x], {x, 0, 5}]

enter image description here

This setting can be made at the Cell, Notebook, or Global level as suits your needs. It can also be used within Style which I believe may be the best answer to your question:

withImageSize[size_] := Style[#, GraphicsBoxOptions -> {ImageSize -> size}] &

Query[graphs]@assoc // withImageSize[Tiny]

enter image description here

Query[graphs]@assoc // withImageSize[Small]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks, yes the Style solution serves my immediate purposes. It does highlight however, that I was using a sledgehammer to crack a nut as the broader utility of TemporarilySet is not tapped into (e.g. using in-built option settings and/or doing so "per function"). As noted, there were some issues with this broader application but will cross that bridge when next seeing it. $\endgroup$ – Ronald Monson Jul 20 '15 at 2:38
  • $\begingroup$ @Ronald Thanks for the Accept. $\endgroup$ – Mr.Wizard Jul 20 '15 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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