9
$\begingroup$

Graphics[{Circle[]}, ImageSize -> Automatic] creates a figure which is 360 pixels wide (same as Medium), if it fits in the notebook window. Otherwise, its size is reduced to fit.

Is it possible to have a size specification which is similarly capped at the notebook width, but has a default size different from 360 pixels?

Graphics[{Circle[]}, ImageSize -> UpTo[500]] does not do this.

The use case is having a large figure on screen, but making sure it fits on the page when printing.


Is it possible to get it done without Dynamic? If not, what is the best way with Dynamic? The following uses the window width instead of the actual available space:

Graphics[Circle[], 
 ImageSize -> Dynamic@Min[500, First@CurrentValue[WindowSize]]]

I am looking for solutions which work just like ImageSize -> Automatic, except that the default size is something else than 360 pixels. I strongly prefer solutions that do not require a running kernel.

$\endgroup$
  • $\begingroup$ ImageResize[Graphics[{Circle[]}], 500] $\endgroup$ – J42161217 Apr 18 '17 at 11:42
  • $\begingroup$ @Jenny_mathy That will first rasterize the figure, then resize it with low quality. If we want to rasterize, it would be better to use Rasterize[fig, ImageSize -> 500]. But I do not want to rasterize it. $\endgroup$ – Szabolcs Apr 18 '17 at 11:58
  • $\begingroup$ I take it that Full is too big? "When the cell is printed, ImageSize->Full corresponds to the full width of the content area of the page." $\endgroup$ – Alan Apr 18 '17 at 13:09
  • $\begingroup$ @Alan Yes. It may cause the bottom of the figure to be cut off. Even if it doesn't, after a certain point further magnification is not a benefit. I would rather see several small plots above each other than a single huge one. However, 360 pixels is a bit too small for my current application. It causes labels in a BarChart to overlap (even after 90 deg rotation). $\endgroup$ – Szabolcs Apr 18 '17 at 13:21
  • $\begingroup$ There is not serious problem of course. But if there is a way to do this, it would be nice. $\endgroup$ – Szabolcs Apr 18 '17 at 13:22
8
+150
$\begingroup$

I'm not sure it is what you want, but have you tried

CurrentValue[EvaluationNotebook[], "GraphicsBoxOptionsImageSizeRaw"] = 500

This instantly resizes all graphics inside a notebook to 500 pixels as long as your notebook window is large enough.

As @Kuba pointed out, this can be used as option for Graphics

Graphics[{Circle[]}, 
  ImageSize -> Automatic, 
  BaseStyle -> "GraphicsBoxOptionsImageSizeRaw" -> 200]

Another possibility due to @CarlWoll is

Graphics[Circle[], ImageSizeRaw -> {500}]

How I found it

Initially, I was sure I had seen an option for that so I looked through

AbsoluteOptions[$Notebooks]

It turned out that what I had in mind was the option "DefaultNewGraphics" which is something else. My next thought was that it is very likely that this is exposed through CurrentValue. We have a question about all possible current values which led me, only the devil knows how, to this pastebin. A quick scan revealed the "GraphicsBoxOptionsImageSizeRaw" setting.

$\endgroup$
  • $\begingroup$ Almost. I wonder if this is settable at the level of Graphics. It seems to be settable in BoxData as GraphicsBoxOptions->{ImageSizeRaw->130}. This is definitely useful in practice, though not exactly what I asked in the question. $\endgroup$ – Szabolcs May 9 '17 at 14:16
  • $\begingroup$ Well, @Kuba just gave the answer. Can you include that in the answer? And who shall I give the bounty to now? :) $\endgroup$ – Szabolcs May 9 '17 at 14:17
  • $\begingroup$ @Szabolcs Already on it. $\endgroup$ – halirutan May 9 '17 at 14:18
  • 1
    $\begingroup$ An alternate version is: Graphics[Circle[], BaseStyle -> {GraphicsBoxOptions -> {ImageSizeRaw -> {700}}}]. Notice the use of {700} instead of 700. $\endgroup$ – Carl Woll May 9 '17 at 14:37
  • 3
    $\begingroup$ Or even Graphics[Circle[], ImageSizeRaw->{500}] $\endgroup$ – Carl Woll May 9 '17 at 15:55
5
$\begingroup$

This is not a general solution but an improvement for Dynamic approach. It will run completely FrontEnd side:

Graphics[Circle[]
  , ImageSize -> Dynamic @ If[
        FEPrivate`Less[CurrentValue[{WindowSize, 1}] - 100, 500]
      , Full
      , 500
      , 500
    ]
]

-100 is unfortuantely manual, will try to get margins+toolbars width somehow.

$\endgroup$
  • $\begingroup$ Looks like you need First@CurrentValue["DocumentAndWindowSize"]. $\endgroup$ – Alexey Popkov May 10 '17 at 6:54
  • $\begingroup$ @AlexeyPopkov looks like this + CellMargins should be enough. Having trouble combining them though :/ $\endgroup$ – Kuba May 10 '17 at 11:31

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.