Exporting Grid as PDF resizes the graphic. How to fix it?

I am attempting to export some figures and plots for a publication, and I decided to export the figures as the size they need to be without rescaling them. This would ensure that fonts and linewidths are all consistent between the figures.

I also wish to export the figures as PDF, since that is a vectorized format.

However, I have come across an issue. Some of my plots are wrapped in a Grid. I have discovered that these plots when rendered as a PDF are smaller than an equivalent without the Grid. This issue does not occur when I use PNG format - the Grid and no-Grid plots are the same scale in PNG, but different scales in PDF. I note that using ImageSizeMultipliers (as described here) has no effect.

The following code shows my issue with a minimal working example:

square1 = Graphics[Rectangle[], ImageSize -> 200]
square2 = Grid[{{square1}}]
Export["square1.pdf", square1];
Export["square2.pdf", square2];
Export["square1.png", square1];
Export["square2.png", square2];


(The four exported figures, rendered on the same page and labeled.)

Because the rescaling depends on the export format, this implies it is something wrong with the export format rather than the Grid or Graphics specifically.

Is there any way I can make Grid export as a PDF without it being rescaled so that it matches the scale of my other graphics?

If it is relevant, I am using Mathematica version 11.2.0.0 on Windows 10.

• You may use GraphicsGrid instead of Grid, it keeps image size. Strange, but in help on Grid one can read: "Grid will not change the size of graphics or other objects that have explicit ImageSize settings."
– Alx
Oct 1 '19 at 8:51
• It may be this, please test it: stackoverflow.com/q/6093559/695132 . Evaluate SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"] then try to export again. The setting will persist until you restart the notebook interface. Oct 1 '19 at 10:40 • @Szabolcs I'll test that tomorrow when I get back to work. Although doing the same thing from $FrontEnd did not seem to help (but I'll verify when I am able). Oct 1 '19 at 11:05
• @BBeast If you set it on $FrontEnd, then the setting will be permanent. If you set it on $FrontEndSession, it will be cleared when the front end is restarted. Otherwise, there is no difference. Oct 1 '19 at 11:06
• I just tested it, and it does work. Oct 1 '19 at 11:07

This has been a long-term annoyance when exporting graphics from Mathematica.

The root cause is that non-Graphics notebook objects are exported in the "Printout" environment, which downscales to 80% by default.

It is discussed here and in the MaTeX tutorial titled "Preparing Figures to Size".

The symptom

If expr is not Graphics or Graphics3D, then Export["file.pdf", expr] will be downscaled to 80% of the original size.

Note that anything with a legend has head Legended, so it used to be downscaled too in earlier versions. This seems to have been fixed specifically for Legended in version 12.0.

Before version 12.0, this was exported at normal size:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}]


This was exported at 80% size:

Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"]


Now this is fixed with Legended objects, but it still persists with things like Row, Column, Grid, Framed, etc. (which could be wrapping graphics).

The workaround

Set Mathematica to "print" in the "Working" environment instead of the "Printing" environment.

This can be done using

SetOptions[\$FrontEndSession, PrintingStyleEnvironment -> "Working"]


The setting will be cleared once the front end is restarted (it is not sufficient to restart the kernel).

Notes

Note that there are two effects from Row/Column/Grid.

One is that all graphics with a "soft" size specification such as ImageSize -> Medium are downscaled even on-screen to fit the Grid better. This can be prevented by using an exact size specification such as ImageSize -> 200 (as you did). Medium is normally of size ImageSize -> 360.

The other, separate effect is that the are downscaled only when exported to PDF/PostScript or printed. This we work around by setting PrintingStyleEnvironment -> "Working".

• Is the down-scaling from "Printout" consistently by 80%? In case I decide I like the aesthetic of Printout better than Working. Oct 1 '19 at 13:34
• @BBeast I am not sure what the exact ratio is. I wrote 80% from memory. It may not be correct. Oct 1 '19 at 14:19
• @BBeast Downscaling does not change aesthetics, but it does mess up sizes. If I set 12 point fonts in my graphics, I want them to be the same size as the 12 point fonts in a document I included the graphics in. If you want to create professional-looking documents with consistent font sizes (especially in LaTeX), I recommend the "Preparing Figures to Size" tutorial in the MaTeX documentation. You might find some inspiration there. Oct 1 '19 at 14:21
• @BBeast The reason for the downscaling is that when converting an entire notebook to PDF (File -> Save As...), or when printing a notebook to paper, everything would look too large if kept at normal sizes. For printing, downscaling makes perfect sense. For exporting a single small notebook element (e.g.a 2x2 grid of graphics), it doesn't really make sense. But one can always make up edge cases which are inbetween these two extremes, and it's unclear if downscaling is useful or not ... Oct 1 '19 at 14:23
• This works. The Working environment was giving me thicker grid and frame lines than the Printout environment, but I could fix that by explicitly specifying the line thickness. Thank you. Oct 2 '19 at 5:25