3

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Ronald Monson
  • 6,076
  • 26
  • 46

1 Answers1

3

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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. – Ronald Monson Jul 20 '15 at 02:38
  • @Ronald Thanks for the Accept. – Mr.Wizard Jul 20 '15 at 02:41