3
$\begingroup$

I am on Windows 7, tried Mathematica v9 and v11, both have the same issue when screen resolution is set to 150%:

screenshot

EMF exported from a graph will come with white spaces on the bottom and right hand sides, like screen shot below. Other image types (PNG, JPG, BMP, PDF) are exported fine though. How do I work around this?

g = Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}, 
  PlotStyle -> {Thickness[0.0], Thickness[0.01]}]
Export["g-150.emf", g]

enter image description here

$\endgroup$
  • $\begingroup$ Start by providing code to reproduce your problem. $\endgroup$ – MarcoB Mar 8 '17 at 5:41
  • $\begingroup$ Any simple graph will do. The one in picture is from this code: Plot[{Sin[x], Cos[x]}, {x, 0, 2 [Pi]}, PlotStyle -> {Thickness[0.0], Thickness[0.01], AbsoluteThickness[0.0]}] $\endgroup$ – Leonx Mar 8 '17 at 5:57
  • $\begingroup$ And how did you export the graph? From the context menu? Using Export? $\endgroup$ – MarcoB Mar 8 '17 at 6:04
  • $\begingroup$ Apology, full piece of code is this: g = Plot[{Sin[x], Cos[x]}, {x, 0, 2 [Pi]}, PlotStyle -> {Thickness[0.0], Thickness[0.01], AbsoluteThickness[0.0]}] Export["c:\\temp\\g-150.emf", g] $\endgroup$ – Leonx Mar 8 '17 at 6:08
  • 1
    $\begingroup$ Excellent! Now go ahead and edit that code (properly formatted) into the original question. you can always edit your own questions, using the "edit" link on the bottom left of the question itself. If you need formatting help, take a look at mathematica.stackexchange.com/editing-help $\endgroup$ – MarcoB Mar 8 '17 at 6:11
5
$\begingroup$

Update: The problem is solved!

Apart from the three alternative resolutions 100%, 125% and 150% suggested on the right pane, there is the "Set custom text size (DPI)" item on the left pane:

screenshot

When clicking on this item you get the following dialog:

screenshot

Notice the "Use Windows XP style DPI scaling" checkbox - it is the key! But what it does? Citing this article (emphasis is mine),

Un-checking this checkbox causes Windows to use the desktop composition engine to perform DPI scaling for all applications that don’t explicitly mark themselves as DPI aware via manifest. In this case, the composition engine will use your graphics hardware to scale the bitmap output of the window to a larger size, and apply an input transform to any mouse-clicks on the window to route them back to the right place in "real coordinates" for the application. The OS will also virtualize the system DPI for that application, so that it thinks it is running at 96DPI. This applies to most traditional Win32, GDI based applications.

This checkbox is checked by default when you select resolution 100% or 125% on the right, but it is un-checked when you select resolution 150%. Moreover, if you manually check this checkbox for resolution 150%, it will be un-checked automatically! But it will be kept checked if you enter, for example, resolution 160% in the "Scale to this percentage of normal size" combo box of the second dialog. After changing the resolution you need to log off the user and log on the user again for the changes to take effect, and you'll see that the problem with Export to EMF disappeared!

  • A side note: according to the above citation, this checkbox shouldn't have effect on applications which correctly mark themselves as DPI aware via manifest. Therefore Mathematica version 11.0.1 either still isn't DPI aware or has a bug in how it marks itself as DPI aware...

So now we have at least a workaround. But what if you really need resolution exactly 150%? It is possible! By monitoring the system registry I've found that the "Use Windows XP style DPI scaling" checkbox in the second dialog corresponds to the "UseDpiScaling" DWORD key in HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM. The value 0 corresponds to the checkbox being checked, and the value 1 corresponds to the un-checked checkbox.

A word of warning: modifying the system registry is a potentially dangerous operation which may damage your system. It is recommended to backup important data before you proceed.

For changing the value of this key, open the Windows Registry Editor and select HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM key on le left:

screesnhot

Double click on the "UseDpiScaling" DWORD key on the right, enter 0 and press Enter. You'll see the following:

screenshot

Now you need to log off the user and then log on the user again for the changes to take effect. Then check Export to EMF: now generated EMF figure has no white borders when pasted in MS Word:

screenshot

Voilà!

P.S. Related articles:


Original answer

I confirm this problem with Mathematica versions 5.2 (Export as WMF because this version doesn't support EMF export), 8.0.4, 10.4.1 and 11.0.1 on Windows 7 x64 when the system-wide screen resolution is set to 150% on the OS level. I've checked all the ways to produce a metafile (Export, Save Selection As…, Save Graphic As…, FrontEndTokenExecute[EvaluationNotebook[], "SelectionConvert", "Metafile"]) and got identical results: exported metafile has 50% white margin added at the right and at the bottom. Changing FrontEnd option FontProperties -> {"ScreenResolution" -> 72} doesn't affect this behavior. The problem doesn't appear when the system-wide screen resolution is set to 100% or 125%.

As a workaround I can suggest Exporting your plot as SVG or PDF and then using third-party utility like Inkscape (command line solution) for converting it into EMF. Note also that most versions of MS Office support EPS, hence another workaround is to switch to EPS instead of EMF.

I have found that it is possible to cure the exported EMF file using free EMFexplorer utility, but this method works only when the system-wide screen resolution is set to 100% or 125%. This observation indicates that we have a bug not in Mathematica, but in Windows 7 itself. It would be interesting to find out whether the bug is present in other versions of Windows.

Here is the procedure:

  1. Export your EMF (system-wide screen resolution is set to 150%):

    g = Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi}, PlotStyle -> Thickness /@ {0, .01}];
    Export["i.emf", g]
    

    When pasted in MS Word the EMF file has white borders:

    screenshot

  2. On a computer with system-wide screen resolution 100% or 125% open the file in EMFexplorer, go to Document ► More Settings… menu item, set zero margins, and save as EMF again using the File ► Save Page As… menu item:

    screenshot

  3. Now paste new EMF file in MS Word:

    screesnhot

    The figure has no margins!

$\endgroup$
  • $\begingroup$ Excellent research! $\endgroup$ – Leonx Mar 16 '17 at 0:47
  • $\begingroup$ (+1) Thanks for your nice answer. :) However, I am having the same trouble on Windows 10. Can you do an update on this too. :) $\endgroup$ – H. R. Aug 27 at 13:05
  • 1
    $\begingroup$ @H.R. I don't have Windows 10. But it should contain similar settings. $\endgroup$ – Alexey Popkov Aug 27 at 17:44

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.