18
$\begingroup$

Bug introduced in 8.0 or earlier and persisting through 13.2.0 or later


This has irked me for a while as I really feel its a significant shortfall in Mathematica's abilities but maybe I am missing something.

I would like to export simple graphics with shading in a vector graphics format, preferably PDF. This should certainly be possible with the pdf format as I quote from wikipedia (en.wikipedia.org/wiki/Portable_Document_Format#Vector_graphics):

Beginning with PDF 1.3 there is also a shading pattern, which draws continuously varying colors.

Take for example the following simple graphic:

enter image description here

Created with the code

    Graphics[{CapForm["Round"], Thickness[0.2], 
      Line[{{0, 0}, {1, 0}}, VertexColors -> {Red, Green}]}
    ]

When exported as a pdf it becomes:

enter image description here

I think I am correct in stating that Mathematica supports a more recent version of pdf than 1.3 so where is the problem? Is there a workaround? My graphics are still simple 2d lines but such that vector formats are far preferable to rasters.

May thanks for any help offered, I am surprised that I have been unable to find a solution.

EDIT: Two other exporting options which have occurred to me are:

  1. To right click and save as PDF, this produces the same green worm as exporting to pdf.
  2. To right click and print, then save as pdf on the system dialog, this manages to maintain the shading (hurray!) but removes the round capform and leaves the graphic on an entire A4 background...

Thus neither provide satisfactory solutions.

EDIT 2: Not sure if this should be an edit or posted as a reply but it's a bit long for a comment.

Taking on board the comments of @Szabolcs and @Jens I've tried a few more things out. Specifically I have compared exporting the line as shown above, and a line created using the Polygon function instead of Line (similar therefore to @Szabolcs triangle). I have exported both as PDF, EPS (then converted to PDF via terminal) and SVG, in all instances the option "AllowRasterization" -> False is used. What I have found is:

  • Export the Line graphic results in an olive worm, regardless of format. (A Mathematica bug?)
  • Exporting a Polygon graphic as SVG or EPS (then converting to PDF) both appear to work.
  • Exporting a Polygon graphic as PDF produces incorrect results which vary depending on the exact polygon, for example the red-green line (without the round ends as one cannot do this with Polygon) becomes:

enter image description here

whilst @Szabolcs' triangle becomes:

enter image description here

Current Conclusions: My current conclusions therefore are that exporting to PDF is incompletely implemented (and should be better).

And that exporting Line objects is flawed, there is no way to export a Line object with VertexColors as a vector graphic.

So a very limited workaround for the most basic shapes is to recreate them as polygons, export to EPS, then convert to PDF. This however does not allow the use of any curved elements, still unsatisfactory for my purposes. I will consider reporting this as a bug (unless anyone advises me otherwise!).

Thanks for your help so far guys.

$\endgroup$
13
  • $\begingroup$ When I export this, the shading is maintained and the graphics are not rasterized: Graphics[Polygon[{{0, 0}, {1, 0}, {1, 1}}, VertexColors -> {Red, Green, Blue}]]. $\endgroup$
    – Szabolcs
    Oct 18, 2015 at 13:41
  • 2
    $\begingroup$ I would report this to support ... looks very much like a bug ... $\endgroup$
    – Szabolcs
    Oct 18, 2015 at 13:42
  • $\begingroup$ Are you sure about that polygon graphic? When i export it the edges do suggest its a raster, furthermore if I include "AllowRasterization" -> False in the export then I get a uniformly grey triangle. $\endgroup$ Oct 18, 2015 at 14:12
  • $\begingroup$ @Szabolcs example doesn't work for me on Mac OS X. Also, with the OP's code I see the same uniform olive oval in Mathematica 10.3, but also in version 8. So this has never worked, as far as I can tell. $\endgroup$
    – Jens
    Oct 18, 2015 at 17:47
  • $\begingroup$ @Jens This is the exact command I used, also on OS X, 10.3: Export["~/Desktop/test.pdf", Graphics[Polygon[{{0, 0}, {1, 0}, {1, 1}}, VertexColors -> {Red, Green, Blue}]]]. It worked fine. Did you use the same? The OP's example indeed does not work, even though it's quite similar to the Polygon example above. That's why I thought that this might be an actual bug, and not just a missing feature. $\endgroup$
    – Szabolcs
    Oct 18, 2015 at 18:01

1 Answer 1

2
$\begingroup$

The issue still persists through v13.2.0. However, there's the following work-around:

First, create the simple graphic as shown in the OP. Then, right-click and save as pdf. Import the pdf.

gif

Code

SetDirectory@NotebookDirectory[];
test = Graphics[{CapForm["Round"], Thickness[0.2], 
   Line[{{0, 0}, {1, 0}}, VertexColors -> {Red, Green}]}]
Export["pdf.pdf", test];
Import["pdf.pdf"]
Import["rightclick.pdf"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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